-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsetup-from-github-release
executable file
·244 lines (197 loc) · 5.34 KB
/
setup-from-github-release
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#!/usr/bin/env bash
set -euo pipefail
command_exists() {
type "${1}" >/dev/null 2>&1
}
select_github_release() {
local -r repo="${1}"
local -n return_variable="${2}"
local api_url="https://api.github.com/repos/${repo}/releases"
local api_response="$(curl -s "${api_url}")"
local releases=()
local -A asset_urls_by_release=()
for encoded_payload in $(echo "${api_response}" | jq -r '.[] | { prerelease, tag_name, assets: (.assets | map(.browser_download_url)) } | @base64'); do
local payload="$(echo "\"${encoded_payload}\"" | jq -r '. | @base64d')"
local release="$(echo "${payload}" | jq -r '"\( if .prerelease then "🚀" else "📦" end ) \( .tag_name )"')"
local asset_urls=($(echo "${payload}" | jq -r '.assets | .[]'))
releases+=("${release}")
asset_urls_by_release["${release}"]="${asset_urls[@]}"
done
local -A asset_url_by_name=()
echo "Select Release:"
PS3='> '
select release in "${releases[@]}"; do
if [[ -n "${release}" ]]; then
local asset_urls=(${asset_urls_by_release[${release}]})
if [[ "${#asset_urls[@]}" = "0" ]]; then
echo "No assets available!"
else
for asset_url in "${asset_urls[@]}"; do
asset_url_by_name["$(basename "${asset_url}")"]="${asset_url}"
done
break
fi
else
echo "Invalid selection!"
fi
done
local asset_url
echo "Select Asset: "
PS3='> '
select asset in "${!asset_url_by_name[@]}"; do
if [[ -n "${asset}" ]]; then
asset_url="${asset_url_by_name[${asset}]}"
break
else
echo "Invalid selection!"
fi
done
return_variable="${asset_url}"
}
pick_file() {
local -r directory="${1}"
local -n return_variable="${2}"
local -r files=($(find "${directory}" -type f))
echo "Pick the executable binary:"
PS3='> '
select file in "${files[@]}"; do
if [[ -f "${file}" ]]; then
return_variable="${file}"
break
fi
done
}
use_binary() {
local -r filepath="${1}"
local -r filename="$(basename "${filepath}")"
chmod u+x "${filepath}"
sudo mv "${filepath}" "/usr/local/bin/${filename}"
}
handle_archive() {
local -r filepath="${1}"
local -r directory="/tmp/setup-from-github-release"
rm -rf "${directory}"
mkdir -p "${directory}"
local -r filename="$(basename "${filepath}")"
echo ""
echo "Extracting: ${filename}"
echo ""
case "${filename}" in
*.zip)
unzip -o -q "${filepath}" -d "${directory}"
;;
*.tar.gz)
tar -xzf "${filepath}" -C "${directory}"
;;
*)
echo "Unsupported filetype!"
exit 1
esac
local file
pick_file "${directory}" file
use_binary "${file}"
}
manual_handle() {
local -r filepath="${1}"
local -r directory="$(dirname "${filepath}")"
local filename="$(basename "${filepath}")"
local -A operations=()
operations['Rename']='rename'
operations['Use as Binary']='use_as_binary'
operations['Nothing']='nothing'
echo "What do you want to do with the file: ${filename}"
PS3="Operation > "
select operation in "${!operations[@]}"; do
if [[ -n "${operation}" ]]; then
case "${operations[${operation}]}" in
rename)
local new_filename
read -p "New Name [${filename}]: " new_filename
new_filename="${new_filename:-"${filename}"}"
mv "${directory}/${filename}" "${directory}/${new_filename}"
filename="${new_filename}"
;;
use_as_binary)
use_binary "${directory}/${filename}"
break
;;
nothing)
exit 1
;;
esac
else
echo "Invalid selection!"
fi
done
}
handle_downloaded_package() {
local -r filename="${1}"
case "${filename}" in
*.zip)
handle_archive "${filepath}"
;;
*.tar.gz)
handle_archive "${filepath}"
;;
*.deb)
sudo dpkg -i "${filepath}"
;;
*)
manual_handle "${filepath}"
;;
esac
}
download_package() {
local -r url="${1}"
local -r filepath="${2}"
wget --continue --quiet --show-progress --timestamping "${url}" -O "${filepath}"
}
setup_from_github_release_interactive() {
local -r repo="${1}"
local url
select_github_release "${repo}" url
local -r filename="$(basename "${url}")"
local -r filepath="/tmp/${filename}"
echo ""
echo "Downloading: ${url}"
echo ""
download_package "${url}" "${filepath}"
echo ""
echo "Installing..."
echo ""
handle_downloaded_package "${filename}"
echo ""
echo "Done!"
echo ""
}
setup_from_github_release_non_interactive() {
local -r repo="${1}"
local -r url_pattern="${2}"
local -r url=$(curl -s "https://api.github.com/repos/${repo}/releases/latest" | jq -r --arg pattern "${url_pattern}" '.assets | map(.browser_download_url) | map(select(. | test($pattern)))[0]')
local -r filename="$(basename "${url}")"
local -r extension="${filename##*.}"
local -r filepath="/tmp/${filename}"
echo ""
echo "Downloading: ${url}"
echo ""
download_package "${url}" "${filepath}"
echo ""
echo "Installing..."
echo ""
handle_downloaded_package "${filename}"
echo ""
echo "Done!"
echo ""
}
if ! command_exists jq; then
echo "Missing dependency: jq"
exit 1
fi
if [[ "$#" == "1" ]]; then
setup_from_github_release_interactive "$@"
elif [[ "$#" == "2" ]]; then
setup_from_github_release_non_interactive "$@"
else
echo "Sorry mate! Something went wrong!"
exit 1
fi