-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathinstall.sh
executable file
·169 lines (144 loc) · 3.46 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
#!/bin/bash
set -eu
function get_os() {
local os=$(uname -s)
case "${os}" in
Linux)
echo "linux"
;;
FreeBSD)
echo "freebsd"
;;
Darwin)
echo "darwin"
;;
MINGW* | MSYS* | CYGWIN*)
echo "windows"
;;
*)
printf "goup: unrecognized OS: $os" >&2
exit 1
;;
esac
}
function get_arch() {
local os=$(uname -s)
local arch=$(uname -m)
case ${arch} in
"x86_64" | "x86-64" | "x64" | "amd64")
echo "x86_64"
;;
"i386" | "i486" | "i586" | "i686" | "i786" | "x86")
echo "i386"
;;
"aarch64" | "arm64")
echo "aarch64"
;;
"xscale" | "arm" | "armv6l" | "armv7l" | "armv8l")
echo "arm"
;;
"s390x")
echo "s390x"
;;
*)
printf "goup: unrecognized ARCH: $arch" >&2
exit 1
;;
esac
}
function get_dld() {
local dld
if check_cmd curl; then
dld=curl
elif check_cmd wget; then
dld=wget
else
printf "goup: need curl or wget" >&2
exit 1
fi
echo $dld
}
need_cmd() {
if ! check_cmd "$1"; then
printf "goup: need '$1' (command not found)" >&2
exit 1
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: $*"
exit 1
fi
}
# This is just for indicating that commands' results are being
# intentionally ignored. Usually, because it's being executed
# as part of error handling.
ignore() {
"$@"
}
function get_target() {
local os=$(get_os)
local arch=$(get_arch)
case "${os}" in
linux)
echo "goup-${arch}-unknown-${os}-musl.tar.gz"
;;
freeBSD)
echo "goup-${arch}-unknown-${os}.tar.gz"
;;
darwin)
echo "goup-${arch}-apple-${os}.tar.gz"
;;
MINGW* | MSYS* | CYGWIN*)
echo "goup-${arch}-pc-${os}-msvc.zip"
;;
*)
printf "goup: unrecognized OS and ARCH: $os $arch" >&2
exit 1
;;
esac
}
function main() {
need_cmd uname
need_cmd mktemp
need_cmd mkdir
need_cmd chmod
need_cmd rm
need_cmd rmdir
local _target=$(get_target)
local dld=$(get_dld)
local _url="https://github.com/thinkgos/goup-rs/releases/latest/download/${_target}"
local GOUP_HOME=${GOUP_HOME:-$HOME/.goup}
local GOUP_BIN_DIR="${GOUP_HOME}/bin"
local GOUP_BIN_FILE="${GOUP_HOME}/bin/goup"
local _dir
if ! _dir="$(ensure mktemp -d)"; then
# Because the previous command ran in a subshell, we must manually
# propagate exit status.
exit 1
fi
local _target_file="${_dir}/${_target}"
ensure mkdir -p ${GOUP_HOME}
ensure mkdir -p ${GOUP_BIN_DIR}
echo "[1/3] Download goup..."
if [ "dld" = curl ]; then
curl -s -S -f --progress-bar -L "${_url}" -o "${_target_file}"
else
wget -q "${_url}" -P "${_dir}"
fi
echo "[2/3] Install goup to the ${GOUP_BIN_DIR}"
ensure tar -xz -f "${_target_file}" -C "${GOUP_BIN_DIR}"
ensure chmod u+x "${GOUP_BIN_FILE}"
ensure "${GOUP_BIN_FILE}" init
ignore rm "$_target_file"
ignore rmdir "$_dir"
echo "[3/3] Please add '. "${GOUP_HOME}/env"' to your shell environment!!"
echo " And then try to run 'goup --version'"
}
main