-
Notifications
You must be signed in to change notification settings - Fork 48
/
install.sh
executable file
·187 lines (151 loc) · 3.56 KB
/
install.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#!/bin/sh
# shellcheck shell=dash
# This is just a little script that can be downloaded from the internet to
# install goup. It just does platform detection, downloads the installer
# and runs it.
# It runs on Unix shells like {a,ba,da,k,z}sh. It uses the common `local`
# extension. Note: Most shells limit `local` to 1 var per line, contra bash.
set -u
GOUP_UPDATE_ROOT="${GOUP_UPDATE_ROOT:-https://github.com/owenthereal/goup/releases/latest/download}"
main() {
downloader --check
need_cmd uname
need_cmd mkdir
need_cmd chmod
get_architecture || return 1
local _arch="$RETVAL"
local _ext=""
case "$_arch" in
*windows*)
_ext=".exe"
;;
esac
local _url="${GOUP_UPDATE_ROOT}/${_arch}${_ext}"
local _dir="$HOME/.go/bin"
local _file="${_dir}/goup${_ext}"
ensure mkdir -p "$_dir"
ensure downloader "$_url" "$_file"
ensure chmod u+x "$_file"
if [ ! -x "$_file" ]; then
printf '%s\n' "Cannot execute $_file." 1>&2
printf '%s\n' "Please copy the file to a location where you can execute binaries and run ./goup${_ext} init." 1>&2
exit 1
fi
local _is_tty=true
for f in "$@"
do
if [ "$f" = "--skip-prompt" ]; then
_is_tty=false
fi
done
if [ "$_is_tty" = true ]; then
ignore "$_file" init "$@"</dev/tty
else
ignore "$_file" init "$@"
fi
local _retval=$?
return "$_retval"
}
# This is just for indicating that commands' results are being
# intentionally ignored. Usually, because it's being executed
# as part of error handling.
ignore() {
"$@"
}
need_cmd() {
if ! check_cmd "$1"; then
err "need '$1' (command not found)"
fi
}
check_cmd() {
command -v "$1" > /dev/null 2>&1
}
# Run a command that should never fail. If the command fails execution
# will immediately terminate with an error showing the failing
# command.
ensure() {
if ! "$@"; then err "command failed: $*"; fi
}
say() {
printf 'goup: %s\n' "$1"
}
err() {
say "$1" >&2
exit 1
}
# This wraps curl or wget. Try curl first, if not installed,
# use wget instead.
downloader() {
local _dld
if check_cmd curl; then
_dld=curl
elif check_cmd wget; then
_dld=wget
else
_dld='curl or wget' # to be used in error message of need_cmd
fi
if [ "$1" = --check ]; then
need_cmd "$_dld"
elif [ "$_dld" = curl ]; then
if [ -z "$2" ]; then
curl --silent --show-error --fail --location "$1"
else
curl --silent --show-error --fail --location "$1" --output "$2"
fi
elif [ "$_dld" = wget ]; then
if [ -z "$2" ]; then
wget "$1"
else
wget "$1" -O "$2"
fi
else
err "Unknown downloader" # should not reach here
fi
}
get_architecture() {
local _ostype _cputype _arch
_ostype="$(uname -s)"
_cputype="$(uname -m)"
if [ "$_ostype" = Darwin ] && [ "$_cputype" = i386 ]; then
# Darwin `uname -m` lies
if sysctl hw.optional.x86_64 | grep -q ': 1'; then
_cputype=x86_64
fi
fi
case "$_ostype" in
Linux)
_ostype=linux
;;
FreeBSD)
_ostype=freebsd
;;
Darwin)
_ostype=darwin
;;
MINGW* | MSYS* | CYGWIN*)
_ostype=windows
;;
*)
err "unrecognized OS type: $_ostype"
;;
esac
case "$_cputype" in
i386 | i486 | i686 | i786 | x86)
_cputype=386
;;
xscale | arm | armv6l | armv7l |armv8l)
_cputype=arm
;;
aarch64 | arm64)
_cputype=arm64
;;
x86_64 | x86-64 | x64 | amd64)
_cputype=amd64
;;
*)
err "unknown CPU type: $_cputype"
esac
_arch="${_ostype}-${_cputype}"
RETVAL="$_arch"
}
main "$@" || exit 1