-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgh-release-downloader
executable file
·151 lines (138 loc) · 3.32 KB
/
gh-release-downloader
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
#!/usr/bin/env bash
releases=$(mktemp)
maybe_trace() {
if [ -n "$trace" ]; then
set -x
fi
}
list_releases() {
releases_url="https://api.github.com/repos/$repo/releases"
if [ -n "$version" ]; then
releases_url="$releases_url/tags/$version"
fi
curl -H "$AUTHORIZATION_HEADER" -q -s -L "$releases_url" -o "$releases"
}
find_artifact() {
if [ -z "$version" ]; then
version_selector='.[0]'
fi
if [ -z "$os_re" ]; then
os=$(echo ${os:-$(uname -s)}|tr A-Z a-z)
case $os in
darwin)
os_re='[Dd]arwin|[Mm][Aa][Cc][Oo][Ss]'
;;
linux)
os_re='[Ll]inux'
;;
windows*|CYGWIN*)
os_re='[Ww]in(?:dows|)'
;;
esac
fi
if [ -z "$arch_re" ]; then
arch=$(echo ${arch:-$(uname -m)}|tr A-Z a-z)
case $arch in
arm64|aarch64)
arch_re='[Aa]rm64|[Aa]arch64';;
amd64|x86_64)
arch_re='[Aa]md64|[Xx]86[-_]64';;
i686|i386|x86)
arch_re='(?:i[3-6]|x)86';;
esac
fi
jq -r '['"$version_selector"'.assets[] | select (.name | test("'"$os_re"'") and test("'"$arch_re"'")) | .browser_download_url // empty] | .[-1]' "$releases"
}
select_artifact() {
artifact=$(find_artifact)
if [ -z "$artifact" ]; then
echo "$0 could not find asset from '$releases_url' (saved as '$releases') matching OS ('$os_re') and ARCH ('$arch_re')" >&2
exit 1
fi
}
set_up_auth() {
if [ -n "$GH_TOKEN" ]; then
export AUTHORIZATION_HEADER="Authorization: token $GH_TOKEN"
else
export AUTHORIZATION_HEADER='X-No-Authorization: Sorry About That'
fi
}
download_artifact() {
temp_file=$(mktemp)
curl -H "$AUTHORIZATION_HEADER" -o "$temp_file" -q -s -L "$artifact"
echo "url=$artifact" >> "$GITHUB_OUTPUT"
}
select_file() {
(
if [ -n "$file_re" ]; then
grep -E "$file_re"
else
cat -
fi
) | tail -1
}
strip_zip_file_size_date_and_time_from_directory_listing() {
perl -pe 's/^\s*\d+\s+\d+-\d+-\d+\s+\d+:\d+\s+//'
}
get_destination() {
destination="${destination:-$GITHUB_WORKSPACE}"
(
cd "$GITHUB_WORKSPACE"
mkdir -p "$(dirname "$destination")"
)
echo "path=$destination" >> "$GITHUB_OUTPUT"
}
move_and_cleanup_selected_file() {
get_destination
if [ -n "$selected_file" ] && [ -e "$selected_file" ]; then
selected_file="$(pwd)/$selected_file"
cd "$GITHUB_WORKSPACE"
mv "$selected_file" "$destination" &&
rm "$temp_file"
else
mkdir -p "$destination"
rsync . "$destination"
find . -mindepth 1 -delete
fi
}
maybe_work_in_scratch_area() {
if [ -n "$selected_file" ]; then
cd $(mktemp -d)
fi
}
maybe_extract_artifact() {
case "$artifact" in
*.tar.gz|*.tgz)
selected_file=$(
tar tzf "$temp_file" |
select_file
)
maybe_work_in_scratch_area
tar zxf "$temp_file" "$selected_file"
move_and_cleanup_selected_file
;;
*.zip)
selected_file=$(
unzip -l "$temp_file" |
select_file |
strip_zip_file_size_date_and_time_from_directory_listing
)
maybe_work_in_scratch_area
unzip "$temp_file" "$selected_file"
move_and_cleanup_selected_file
;;
*)
: binary
get_destination
mv "$temp_file" "$destination"
esac
if [ -n "$destination" ] && [ -f "$destination" ]; then
chmod +x "$destination"
fi
}
maybe_trace
set_up_auth
list_releases
select_artifact
download_artifact
maybe_extract_artifact