-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuaws
executable file
·229 lines (206 loc) · 4.82 KB
/
uaws
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#!/bin/sh
set -eu
arch=
case $(uname -m) in
x86_64)
arch=amd64
;;
arm64)
arch=arm64
;;
aarch64)
arch=arm64
;;
*)
echo "error: unsupported arch $(uname -m)" >&2
exit 1
;;
esac
os=
ext=
case $(uname -s) in
Linux)
os=linux
;;
Darwin)
os=darwin
;;
*)
echo "error: unsupported OS $(uname -s)" >&2
exit 1
;;
esac
bin_dir="${HOME}/.local/bin"
lib_dir="${HOME}/.local/lib/uawscli"
if [ $(basename $0) = "uaws" ] && [ $0 != "./uaws" ]
then
# Resolve install path if installed.
bin_dir=$(cd $(dirname $0) && pwd)
lib_dir=$(cd ${bin_dir}/.. && pwd)/lib/uawscli
fi
get_download_urls() {
version=$1
if [ -z "${GITHUB_TOKEN:-${TRAVIS_BOT_GITHUB_TOKEN:-}}" ]
then
# Fallback to use download URL list on gh-pages if token is not available.
# API access from CI services usually fails due to rate limit.
curl -sSL --retry 4 \
https://seqsense.github.io/uawscli/releases/${version}
exit 0
fi
api_auth="-H \"Authorization: token ${GITHUB_TOKEN:-${TRAVIS_BOT_GITHUB_TOKEN}}\""
gh_api_base=${GITHUB_API_URL_BASE:-https://api.github.com}
slug=seqsense/uawscli
ep=latest
if [ ${version} != "latest" ]
then
ep=tags/${version}
fi
eval curl \
${api_auth} \
-sSL --retry 4 \
${gh_api_base}/repos/${slug}/releases/${ep} \
| sed -n 's/.*"browser_download_url":[ \t\r\n]*"\([^"]*\)"/\1/p'
}
uaws_install() {
mkdir -p ${lib_dir}
mkdir -p ${bin_dir}
urls=$(get_download_urls $1 | grep -e "_${os}_${arch}${ext}$" || true)
if [ -z "${urls}" ]
then
echo "error: failed to fetch released binary list" >&2
exit 1
fi
shift
for query in $@
do
found=false
for url in ${urls}
do
cmd=$(echo ${url} | sed -n "s|.*/\([^/]*\)_[0-9\.]\{1,\}_${os}_${arch}${ext}$|\1|p")
if echo ${query} | grep -q -e "^${cmd}"
then
file=$(echo ${url} | sed -n 's|.*/\([^/]\{1,\}\)$|\1|p')
if [ ! -f ${lib_dir}/${file} ]
then
echo "info: installing ${url} ${cmd} ${file}" >&2
curl -sSL ${url} -f -o ${lib_dir}/${file}
chmod +x ${lib_dir}/${file}
rm -f ${bin_dir}/${cmd}
ln -s ${lib_dir}/${file} ${bin_dir}/${cmd}
else
echo "info: ${url} is up-to-date" >&2
fi
found=true
break
fi
done
if ! ${found}
then
echo "error: ${query} not found" >&2
exit 1
fi
done
}
collect_installed_cmds() {
ls -1 ${lib_dir} 2> /dev/null \
| sed "s/\(..*\)_[0-9\.]\{1,\}_${os}_${arch}${ext}/\1/" \
| sort \
| uniq || true
}
installed_cmds=$(collect_installed_cmds)
case ${1:-} in
upgrade)
uaws_install latest ${installed_cmds}
exit 0
;;
get_download_urls)
shift
get_download_urls ${1:-latest}
exit 0
;;
install)
shift
install_targets=
install_prefix="${HOME}/.local"
version=latest
while [ $# -gt 0 ]
do
case $1 in
-v)
shift
if [ $# -lt 1 ]
then
echo "error: -v must be followed by a version" >&2
exit 1
fi
version=$1
;;
-i)
shift
if [ $# -lt 1 ]
then
echo "error: -i must be followed by a path" >&2
exit 1
fi
install_prefix=$1
;;
-*)
echo "error: unknown option $1" >&2
exit 1
;;
*)
install_targets="${install_targets} uaws-$1"
;;
esac
shift
done
if [ -z "${install_targets}" ]
then
echo "usage: $(basename $0) install [option...] install_targets" >&2
echo "option:" >&2
echo " -v VERSION install specific VERSION of uawscli" >&2
echo " -i DIR install prefix DIR (default: ${install_prefix})" >&2
exit 1
fi
bin_dir=${install_prefix}/bin
lib_dir=${install_prefix}/lib/uawscli
uaws_install ${version} ${install_targets}
exit 0
;;
"")
echo "usage: $(basename $0) sub-command ..." >&2
echo "installed sub-commands:" >&2
echo ${installed_cmds} | xargs -n1 echo " "
exit 0
;;
esac
for i in 1 2
do
selected_cmd=
for cmd in ${installed_cmds}
do
nquery=1
query="uaws-$@"
while echo ${query} | grep -q -e "^\S\+ "
do
query=$(echo ${query} | sed "s/ /-/")
nquery=$(expr ${nquery} + 1)
if echo ${query} | grep -q -e "^${cmd}"
then
selected_cmd=${cmd}
shift ${nquery}
break
fi
done
done
if [ -z "${selected_cmd}" ]
then
echo "warn: requested command is not installed" >&2
# not found, try to install
uaws_install latest uaws-$(echo $@ | sed "s/ /-/g" | head -n1)
installed_cmds=$(collect_installed_cmds)
continue
fi
exec $(ls -1 ${lib_dir}/${selected_cmd}* | sort -rV | head -n1) "$@"
done