forked from gruntwork-io/fetch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.sh
80 lines (72 loc) · 1.8 KB
/
build.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# vim: et sr sw=4 ts=4 smartindent syntax=sh:
setup_fetch() {
export GOPATH=${GOPATH:-/go} GOBIN=/usr/local/go/bin
export LGOBIN=$GOBIN PATH=$PATH:$GOBIN
export __WD=$GOPATH/src/github.com/opsgang/fetch
export PATH=$GOBIN:$PATH
export __V=${FETCH_VERSION:-v1.0.0}
get_glide || return 1
}
wd() {
[[ -z "$__WD" ]] && echo "Run setup_*() first" && return 1
[[ "$PWD" != "$__WD" ]] && cd $__WD
return 0
}
build_linux() {
(
_build
)
}
build_macos() {
(
export GOOS=darwin GOARCH=amd64
_build
)
}
get_glide() {
(
echo $GOPATH
command -v glide >/dev/null && return 0
wd || return 1
if on_alpine && not_root
then
curl https://glide.sh/get | su-exec root sh || exit 1
su-exec root glide install
elif not_root
then
curl https://glide.sh/get | sudo -E sh || exit 1
sudo -E GOPATH=$GOPATH glide install
else
curl https://glide.sh/get | sh || exit 1
glide install
fi
)
}
# ... account for building on alpine
_build() {
local fl="-w -extldflags \"-static\" -X main.VERSION=$__V -X main.TIMESTAMP=build-$(date '+%Y%m%d%H%M%S')"
(
wd || return 1
export CGO_ENABLED=0
if on_alpine && not_root
then
su-exec root go build --ldflags "$fl" -o $GOBIN/ghfetch .
elif not_root
then
sudo -E go build --ldflags "$fl" -o $GOBIN/ghfetch .
else
go build --ldflags "$fl" -o $GOBIN/ghfetch .
fi
)
}
# if on alpine we need to use su-exec root to build successfully.
on_alpine() {
(
. /etc/os-release 2>/dev/null || exit 1
[[ "$NAME" =~ Alpine ]] && exit 0
exit 1
)
}
not_root() {
[[ "$(id -u)" -ne 0 ]]
}