-
Notifications
You must be signed in to change notification settings - Fork 1
/
install.sh
executable file
·105 lines (93 loc) · 2.4 KB
/
install.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
#!/bin/sh
set -eu
# Use a main function so that a partial download doesn't execute half a script.
main() {
repository="rosszurowski/tandem"
releases_url="https://github.com/${repository}/releases"
bin_name="tandem"
destination="$(pwd)"
# Step 1: parse incoming args to determine the destination to install to.
for i in "$@"; do
case $i in
-d=*|--dest=*)
destination="${i#*=}"
shift # past argument=value
;;
*)
# unknown option
;;
esac
done
# Step 2: get the lowercased OS type, either `darwin` or `linux`.
os=$(uname -s | awk '{print tolower($0)}')
case ${os} in
"darwin" | "linux" )
# do nothing
;;
*)
echo ""
echo "Error: unsupported OS '${os}'"
echo "You can try manually downloading binaries here:"
echo "${releases_url}"
echo ""
exit 1
;;
esac
# Step 3: get the OS architecture.
arch=$(uname -m)
case ${arch} in
"x86_64" | "amd64" )
arch="amd64"
;;
"i386" | "i486" | "i586")
arch="386"
;;
"aarch64" | "arm64" | "arm")
arch="arm64"
;;
*)
echo ""
echo "Error: unsupported architecture '${arch}'"
echo "You can try manually downloading binaries here:"
echo "${releases_url}"
echo ""
exit 1
;;
esac
# Step 4: find the URL to download from the GitHub releases API.
asset_uri=$(
curl -H "Accept: application/vnd.github.v3+json" \
-sSf "https://api.github.com/repos/${repository}/releases/latest" |
grep '"browser_download_url"' |
grep -o "http[^\"]*" |
grep "_${os}_${arch}.tar.gz$" |
head -n 1
)
if [ -z "$asset_uri" ]; then
echo ""
echo "Error fetching ${bin_name} download URL"
echo "You can try downloading binaries here:"
echo "${releases_url}"
echo ""
exit 1
fi
latest_version=$(
echo "$asset_uri" |
grep -o "download/[^_]*" |
grep -o "[[:digit:]][^/]*" |
head -n 1
)
tmp_dir="$(mktemp -d)"
tmp_file="${tmp_dir}/${bin_name}.tmp.tar.gz"
echo "Downloading ${bin_name} v${latest_version} from GitHub..."
curl -fsSL "$asset_uri" -o "${tmp_file}"
if [ -n "${destination}" ]; then
mkdir -p "${destination}"
tar -xz -f "${tmp_file}" -C "${destination}" "${bin_name}"
else
tar -xz -f "${tmp_file}" "${bin_name}"
fi
rm -f "${tmp_file}"
echo "Installed ${bin_name} v${latest_version} to ${destination}"
}
main "$@"