Skip to content
This repository was archived by the owner on Nov 26, 2024. It is now read-only.

Commit 9db9d9a

Browse files
Begikfrapin
andauthored
Add goreleaser (#12)
* add goreleaser config Co-authored-by: kfrapin <kevin.frapin@gmail.com>
1 parent ccf371b commit 9db9d9a

4 files changed

Lines changed: 212 additions & 53 deletions

File tree

.github/workflows/release.yml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
name: goreleaser
2+
3+
on:
4+
push:
5+
# run only against tags
6+
tags:
7+
- v*
8+
9+
# permissions:
10+
# contents: write
11+
# # packages: write
12+
# # issues: write
13+
14+
jobs:
15+
goreleaser:
16+
runs-on: ubuntu-latest
17+
steps:
18+
- name: Checkout
19+
uses: actions/checkout@v3
20+
with:
21+
fetch-depth: 0
22+
- name: Fetch all tags
23+
run: git fetch --force --tags
24+
- name: Set up Go
25+
uses: actions/setup-go@v3
26+
with:
27+
go-version: 1.18
28+
- name: Run GoReleaser
29+
uses: goreleaser/goreleaser-action@v2
30+
with:
31+
# either 'goreleaser' (default) or 'goreleaser-pro'
32+
distribution: goreleaser
33+
version: latest
34+
args: release --rm-dist
35+
env:
36+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
37+
SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK }}
38+
HOMEBREW_TAP_GITHUB_TOKEN: ${{ secrets.BOT_ACCESS_TOKEN }}
39+
# Your GoReleaser Pro key, if you are using the 'goreleaser-pro' distribution
40+
# GORELEASER_KEY: ${{ secrets.GORELEASER_KEY }}

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
/build/
22
/.vscode/
3+
4+
dist/

.goreleaser.yaml

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
before:
2+
hooks:
3+
- go mod tidy
4+
5+
# Set GITHUB_TOKEN
6+
# Build go binaries for specified OS/ARCH
7+
builds:
8+
- skip: false
9+
env:
10+
- CGO_ENABLED=0
11+
goos:
12+
- linux
13+
- windows
14+
- darwin
15+
ldflags:
16+
- -X main.Version={{.Version}} -s -f
17+
18+
# Create/Update Github release
19+
release:
20+
disable: false
21+
prerelease: auto
22+
mode: keep-existing
23+
extra_files:
24+
- glob: ./install
25+
26+
# Update homebrew formula
27+
brews:
28+
- tap:
29+
owner: intercloud
30+
name: homebrew-tap
31+
token: '{{ .Env.HOMEBREW_TAP_GITHUB_TOKEN }}'
32+
license: MPL
33+
folder: Formula
34+
# Uncomment for private repositoris
35+
# download_strategy: GitHubPrivateRepositoryReleaseDownloadStrategy
36+
# custom_require: lib/custom_download_strategy
37+
description: 'Gobinsec'
38+
homepage: 'https://github.com/intercloud/gobinsec'
39+
test: |
40+
system "#{bin}/gobinsec -version"
41+
install: |
42+
bin.install "gobinsec"
43+
# Archive all attachements
44+
archives:
45+
- name_template: '{{ .ProjectName }}_{{ .Os }}_{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}'
46+
format_overrides:
47+
- goos: windows
48+
format: zip
49+
replacements:
50+
386: i386
51+
amd64: x86_64
52+
53+
# Generate checksums file
54+
checksum:
55+
name_template: 'checksums.txt'
56+
57+
# Build in snapshot `goreleaser --snapshot``
58+
snapshot:
59+
name_template: '{{ incpatch .Version }}-next'
60+
61+
# Create a changelog
62+
changelog:
63+
use: github
64+
sort: asc
65+
filters:
66+
exclude:
67+
- '^docs:'
68+
- '^test:'
69+
- '^ci:'
70+
- '^draft'
71+
- '^wip'
72+
73+
# Send announce
74+
announce:
75+
slack:
76+
enabled: true
77+
channel: '#console-dev'
78+
username: Github Release

install

Lines changed: 92 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -4,76 +4,115 @@
44
#
55
# $ sh -c "$(curl -L https://github.com/intercloud/gobinsec/releases/latest/download/install)"
66
#
7-
# or (if you don't have curl installed):
8-
#
9-
# $ sh -c "$(wget -O - https://github.com/intercloud/gobinsec/releases/latest/download/install)"
107

118
set -e
129

13-
NAME="gobinsec"
10+
log() {
11+
echo "$@"
12+
}
1413

15-
# get OS and ARCH and build binary name
16-
os=`uname | tr '[:upper:]' '[:lower:]'`
17-
arch=`uname -m`
14+
logF() {
15+
echo >&2 "$@"
16+
exit 1
17+
}
18+
19+
repo="gobinsec"
20+
owner="intercloud"
21+
binary="${repo}"
22+
23+
get_os() {
24+
echo $(uname -s | awk '{print tolower($0)}')
25+
}
1826

19-
if [ "$arch" = "i386" ]; then
20-
arch="386"
21-
elif [ "$arch" = "x86_64" ]; then
22-
arch="amd64"
27+
get_arch() {
28+
a=$(uname -m)
29+
case ${a} in
30+
"x86_64" | "amd64")
31+
echo "x86_64"
32+
;;
33+
"i386" | "i486" | "i586")
34+
echo "i386"
35+
;;
36+
*)
37+
echo ${NIL}
38+
;;
39+
esac
40+
}
41+
42+
if [ -z "$githubUrl" ]; then
43+
githubUrl="https://github.com"
2344
fi
2445

25-
echo "os: ${os}"
26-
echo "arch: ${arch}"
46+
# parse flag
47+
for i in "$@"; do
48+
case $i in
49+
-v=* | --version=*)
50+
version="${i#*=}"
51+
shift
52+
;;
53+
*)
54+
# unknown option
55+
;;
56+
esac
57+
done
58+
59+
if [ -z "$version" ]; then
60+
version="latest"
61+
fi
2762

28-
binary="${NAME}-${os}-${arch}"
63+
# get OS and ARCH and build binary name
64+
os=$(get_os)
65+
arch=$(get_arch)
66+
67+
log "os: ${os}"
68+
log "arch: ${arch}"
69+
log "version: ${version:-latest}"
70+
71+
fileName="${binary}_${os}_${arch}.tar.gz"
2972

3073
# set default installation directory
31-
if [ -d "/opt/local/bin" ]
32-
then
33-
DEFAULT_DIR="/opt/local/bin"
34-
elif [ -d "/opt/bin" ]
35-
then
36-
DEFAULT_DIR="/opt/bin"
37-
elif [ -d "/usr/local/bin" ]
38-
then
39-
DEFAULT_DIR="/usr/local/bin"
40-
elif [ -d "/usr/bin" ]
41-
then
42-
DEFAULT_DIR="/usr/bin"
74+
if [ -d "/opt/local/bin" ]; then
75+
executableFolder="/opt/local/bin"
76+
elif [ -d "/opt/bin" ]; then
77+
executableFolder="/opt/bin"
78+
elif [ -d "/usr/local/bin" ]; then
79+
executableFolder="/usr/local/bin"
80+
elif [ -d "/usr/bin" ]; then
81+
executableFolder="/usr/bin"
4382
else
44-
DEFAULT_DIR="/bin"
83+
executableFolder="/bin"
4584
fi
4685

47-
# select command to download binary
48-
if hash curl 2>/dev/null
49-
then
50-
command="curl -L -o"
51-
elif hash wget 2>/dev/null
52-
then
53-
command="wget -O"
54-
else
55-
echo "You must install curl or wget to run this installation script"
56-
exit 1
57-
fi
86+
# check command availability
87+
command -v curl >/dev/null 2>&1 || { logF "curl is required"; }
88+
89+
# Set URI to download
90+
assetUri="${githubUrl}/${owner}/${repo}/releases/download/${version}/${fileName}"
91+
if [ "${version}" == "latest" ]; then
5892

59-
# download binary in /tmp/${NAME} and make it executable
60-
${command} /tmp/${NAME} https://github.com/intercloud/${NAME}/releases/latest/download/${binary}
61-
chmod +x /tmp/${NAME}
93+
asset=$(
94+
curl -sSf ${githubUrl}/${owner}/${repo}/releases |
95+
grep -o "${owner}/${repo}/releases/download/.*/${fileName}" |
96+
head -n 1
97+
)
6298

63-
# prompt for installation directory
64-
read -p "Installation directory [${DEFAULT_DIR}]? " directory
65-
if [ -z "$directory" ]
66-
then
67-
directory=${DEFAULT_DIR}
99+
assetUri="${githubUrl}/${asset}"
68100
fi
69101

70-
# copy binary to installation directory
71-
if [ -w "${directory}" ]
72-
then
73-
mv /tmp/${NAME} ${directory}
102+
downloadFolder="/tmp/${binary}"
103+
mkdir -p ${downloadFolder}
104+
downloadedFile="${downloadFolder}/${fileName}"
105+
106+
log "[1/2] Download ${assetUri}"
107+
curl -sS --fail --location --output "${downloadedFile}" "${assetUri}"
108+
109+
log "[2/2] Install ${binary} to '${executableFolder}'"
110+
if [ -w "${directory}" ]; then
111+
tar -xz -f ${downloadedFile} -C ${executableFolder}
112+
chmod +x ${executableFolder}/${binary}
74113
else
75-
sudo mv /tmp/${NAME} ${directory}
76-
sudo chown root: ${directory}/${NAME}
114+
sudo tar -xz -f ${downloadedFile} -C ${executableFolder}
115+
sudo chmod +x ${executableFolder}/${binary}
77116
fi
78117

79-
echo "${NAME} installed in '${directory}' directory"
118+
echo "${binary} installed in '${executableFolder}' directory"

0 commit comments

Comments
 (0)