Repository navigation
Expand file tree
/
Copy pathget-github-release
More file actions
executable file
·167 lines (137 loc) · 4.03 KB
/
Copy pathget-github-release
File metadata and controls
executable file
·167 lines (137 loc) · 4.03 KB
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
#!/usr/bin/env bash
#
# Downloads binary releases from GitHub
#
die() {
echo "Error:" "$@" >&2
exit 1
}
show_help() {
cat <<EOF
Usage: $script_name [options] REPOSITORY ARCHIVE_REGEX BINARY_WILDCARD DESTINATION
optional arguments:
--force force update
--changelog URL changelog URL
--service NAME systemd service to restart
-h, --help show this help message and exit
EOF
}
parse_args() {
eval set -- "$@"
local pull=1
while [ $# -gt 0 ]; do
case "$1" in
-f|--force)
force=1
;;
--changelog)
shift
changelog_url="$1"
;;
-s|--service)
shift
services+=("$1")
;;
-h|--help)
show_help
exit 0
;;
--)
shift
break
;;
(*)
die "Unrecognized option: $1."
;;
esac
shift
done
if [ $# -lt 3 ]; then
die "Got $# arguments when 3 is expected."
fi
repository="$1"; shift
archive_regex="$1"; shift
binary_wildcard="$1"; shift
destination="$(realpath "$1")"; shift
[ -n "$changelog_url" ] || changelog_url="https://github.com/$repository/releases"
}
upgrade-binary() {
local release_info
local version
local archive_name
local decompress_arg
local destination_dir
local binary_name
release_info="$(curl --silent --fail "https://api.github.com/repos/$repository/releases/latest")"
version="$(jq -r .tag_name <<< "$release_info")"
version="${version#v}"
version="$(cut -d ' ' -f 1 <<< "$version")"
archive_name="$(jq -r ".assets[].name | select(. | test(\"${archive_regex//\\/\\\\}\"))" <<< "$release_info")"
destination_dir="$(dirname "$destination")"
binary_name="$(basename "$destination")"
if [ -z "$version" -o -z "$archive_name" -o "$(wc -l <<< "$archive_name")" -ne 1 ]; then
echo "Unable to find the latest release." >&2
exit 1
elif [ "$version" = "$current_version" -a "$force" -ne 1 ]; then
echo "$binary_name is already up-to-date."
return 0
fi
case "$archive_name" in
*.tar.gz)
decompress_arg="-z"
;;
*.tar.bz2)
decompress_arg="-j"
;;
*.tar.xz)
decompress_arg="--xz"
;;
*)
die "Unsupported archive type: $archive_name"
;;
esac
local url="https://github.com/$repository/releases/latest/download/$archive_name"
if [ -z "$current_version" ]; then
echo "Installing $binary_name..."
else
echo "Upgrading $binary_name $current_version -> $version ($changelog_url)..."
fi
curl --fail --location "$url" \
| tar -C "$destination_dir" -x "$decompress_arg" --wildcards "$binary_wildcard" --transform "s/.*/$binary_name.new/" \
--no-same-owner --no-same-permissions
mv "$destination.new" "$destination"
restart-services
}
restart-services() {
local action
local service
local systemctl_args=()
[ "${#services[@]}" -ne 0 ] || return 0
[ "$(id -u)" -eq 0 ] || systemctl_args+=("--user")
for service in "${services[@]}"; do
echo "Restarting $service..."
systemctl "${systemctl_args[@]}" restart "$service"
done
sleep 1
for service in "${services[@]}"; do
systemctl "${systemctl_args[@]}" status "$service"
done
}
set -eu
set -o pipefail
script_name="$0"
changelog_url=""
services=()
force=0
args="$(getopt -n "$0" -o fs:h -l force,changelog:,service:,help -- "$@")"
parse_args "${args[@]}"
current_version=""
if [ -e "$destination" ]; then
current_version="$(
{ "$destination" --version 2>/dev/null || :;} | head -n1 \
| sed -r 's/^(.* )?([Vv]ersion [^ ]+)( .*)$/\2/' \
| sed -r 's/^.*[- ]v([0-9]+\.[0-9]+(\.[0-9]+)?)[- ].*$/\1/' \
| sed -r 's/^.* (.+?)$/\1/'
)"
fi
upgrade-binary