-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgh-downloader.sh
312 lines (262 loc) · 7.72 KB
/
gh-downloader.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
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#!/bin/sh
#
# Simple utility to download files from GitHub Repository.
#
readonly progname=$(basename $0)
# Display help message
getHelp() {
cat << USAGE >&2
Usage: $progname [user repo] [OPTIONS]
-u | --user Github user olding the repository
-r | --repo Github repository
-T | --token Github token
-t | --tag tag to download (Use latest to download the latest tag)
-p | --path Path containing the file to download
-b | --branch Branch containing the file to download
-f | --file File from tag or path to download
-o | --output Output file or folder
Alternatively, you can specify the user and the repo in the right order.
Examples:
* Download the latest version of wait-host.sh from bash-suite/wait-host and save the file to /usr/sbin/wait-host
$progname -u bash-suite -r wait-host -t latest -f wait-host.sh -o /usr/sbin/wait-host
* Download the latest version of wait-host.sh from the master branch of bash-suite/wait-host
* and save the file to /usr/sbin/wait-host
$progname -u bash-suite -r wait-host -p ./ -b master -f wait-host.sh -o /usr/sbin/wait-host
If no file is specified when using the path option, the content of the folder will be downloaded
USAGE
}
# Get input parameters
while [ $# -gt 0 ]; do
#echo $1 $2
case "$1" in
[!-]* )
[ -n "$user" -a -z "$repo" ] && repo=$1
[ -z "$user" ] && user=$1
shift 1
;;
-u|--user)
user="$2"
[ -z "$user" ] && break
shift 2
;;
-u=*|--user=*)
user=$(printf "%s" "$1" | cut -d = -f 2)
[ -z "$user" ] && break
shift 1
;;
-r|--repo)
repo="$2"
[ -z "$repo" ] && break
shift 2
;;
-r=*|--repo=*)
repo=$(printf "%s" "$1" | cut -d = -f 2)
[ -z "$repo" ] && break
shift 1
;;
-T|--token)
token="$2"
shift 2
;;
-T=*|--token=*)
token=$(printf "%s" "$1" | cut -d = -f 2)
shift 1
;;
-t|--tag)
tag="$2"
shift 2
;;
-t=*|--tag=*)
tag=$(printf "%s" "$1" | cut -d = -f 2)
shift 1
;;
-p|--path)
path="$2"
shift 2
;;
-p=*|--path=*)
path=$(printf "%s" "$1" | cut -d = -f 2)
shift 1
;;
-b|--branch)
branch="$2"
shift 2
;;
-b=*|--branch=*)
branch=$(printf "%s" "$1" | cut -d = -f 2)
shift 1
;;
-f|--file)
file="$2"
shift 2
;;
-f=*|--file=*)
file=$(printf "%s" "$1" | cut -d = -f 2)
shift 1
;;
-o|--output)
output="$2"
shift 2
;;
-o=*|--output=*)
output=$(printf "%s" "$1" | cut -d = -f 2)
shift 1
;;
--help)
getHelp
exit 0
;;
*)
echo "Invalid argument '$1'. Use --help to see the valid options"
exit 1
;;
esac
done
# check for user and repo
if [ -z "$user" -o -z "$repo" ]; then
echo "Invalid user or repo. Use --help to see the valid options."
exit 2
fi
# Check installed softwares
cmdCurl=$(command -v curl >/dev/null 2>&1; echo $?)
cmdJq=$(command -v jq >/dev/null 2>&1; echo $?)
# curl must be installed: https://curl.haxx.se/
if [ $cmdCurl -ne 0 ]; then
echo "In order to use '$progname' curl must be installed" 1>&2
exit 1
fi
# js must be installed: https://stedolan.github.io/jq/
if [ $cmdJq -ne 0 ]; then
echo "In order to use '$progname' jq must be installed" >&2
exit 1
fi
updateOutput() {
# Default output to current directory
output=${output:='.'}
# Remove multiple /
output=$(echo $output | tr -s /)
# Make sure folder exist
if [ "${output: -1}" = "/" ]; then
mkdir -p $output
else
# Make sure folder exist
mkdir -p $(dirname $output)
fi
# if ouput is a folder
if [ "${output: -1}" = "/" -a -n "$file" ]; then
#
echo "$output$file"
else
#
echo $output
fi
}
updateTag() {
if [ "$tag" = "latest" ]; then
echo $(fromApi "releases/latest" | jq -r ".tag_name")
else
echo $tag
fi
}
updateBranch() {
if [ -z "$branch" ]; then
echo $(fromApi "" | jq -r ".default_branch")
else
echo $branch
fi
}
fromApi() {
# Github API
api='https://api.github.com'
#
curl ${token:+ -H "Authorization: token $token"} \
${HTTP_PROXY:+ -x $HTTP_PROXY} \
-s \
$api/repos/$user/$repo${1:+/"$1"}
}
# ##############################################################################
# Download file from url
# $1: url of the file
# $2: output of the file
# If the output is a directory, the filename is extracted from the url
# ##############################################################################
downloadFile() {
# Get the url file to download
# and the output
if [ $# -ne 2 ]; then
return 1
fi
_url=$1 # Temporary url
_out=$2 # Temporary output
if [ "${_out: -1}" = "/" ]; then
_out=$(echo $_out/${_url##*/} | tr -s /)
fi
echo "Downloading ${_out#$output}..."
curl "$_url" \
${HTTP_PROXY:+ -x $HTTP_PROXY} \
-s \
-L \
--output "$_out"
}
# Update tag version
[ -n "$tag" ] && tag=$(updateTag)
# Update Branch
[ -z "$tag" ] && branch=$(updateBranch)
# Download file from tag/release
if [ -n "$tag" -a -n "$file" ]; then
echo "Download $file from $tag"
# Output
out=$(updateOutput)
# Url to downloadFile
url=$(fromApi "releases" | jq -r ". | map(select(.tag_name == \"$tag\"))[0].assets | map(select(.name == \"$file\"))[0] | .browser_download_url")
# Download
downloadFile "$url" "$out"
# Download all files from tag/release
elif [ -n "$tag" ]; then
echo "Download all files from $tag"
# Output
out=$(updateOutput)
# Url to download
urls=$(fromApi "releases" | jq -r ". | map(select(.tag_name == \"$tag\"))[0].assets | map(.browser_download_url)[]")
# Download
for _url in $urls; do
downloadFile "$_url" "$out" &
done
wait
# Download file from branch
elif [ -n "$file" ]; then
echo "Download $file from $branch"
# Output
out=$(updateOutput)
# Url to downloadFile
url="https://raw.githubusercontent.com/$user/$repo/$branch/${path:+"$path"/}$file"
# Download
downloadFile "$url" "$out"
# Download all files from branch
else
[ -n "$path" ] && echo "Download all files from $branch/$path" || echo "Download all files from $branch"
# Output
out=$(updateOutput)
# json data from github API
json=$(fromApi "git/trees/$branch?recursive=1")
# Create folder structure
dirs=$(echo $json | jq -r ". | .tree | map(select(.type == \"tree\"))[] | select(.path | startswith(\"$path\")) | .path")
for _dir in $dirs; do
# remove path from dir
_dir=$(echo $out/${_dir#$path} | tr -s /)
#create folders
mkdir -p "$_dir" &
done
wait
# Download files
files=$(echo $json | jq -r ". | .tree | map(select(.type != \"tree\"))[] | select(.path | startswith(\"$path\")) | .path")
for _file in $files; do
# Get file name and url
url="https://raw.githubusercontent.com/$user/$repo/$branch/$_file"
# remove path from file
_file=$(echo $out/${_file#$path} | tr -s /)
# Download
downloadFile "$url" "$_file" &
done
wait
fi