forked from babashka/babashka
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install
executable file
·181 lines (162 loc) · 4.78 KB
/
install
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
#!/usr/bin/env bash
set -euo pipefail
version=""
checksum=""
static_binary="false"
default_install_dir="/usr/local/bin"
install_dir="$default_install_dir"
download_dir=""
dev_build=""
print_help() {
echo "Installs latest (or specific) version of babashka. Installation directory defaults to /usr/local/bin."
echo -e
echo "Usage:"
echo "install [--dir <dir>] [--download-dir <download-dir>] [--version <version>] [--checksum <checksum>] [--static]"
echo -e
echo "Defaults:"
echo " * Installation directory: ${default_install_dir}"
echo " * Download directory: temporary"
if [[ -z "$checksum" ]]; then
echo " * Checksum: no"
else
echo " * Checksum: ${checksum}"
fi
echo " * Static binary: ${static_binary}"
echo " * Version: <Latest release on github>"
exit 1
}
while [[ $# -gt 0 ]]
do
key="$1"
case "$key" in
--dir)
install_dir="$2"
shift
shift
;;
--download-dir)
download_dir="$2"
shift
shift
;;
--version)
version="$2"
shift
shift
;;
--checksum)
checksum="$2"
shift
shift
;;
--static)
static_binary="true"
shift
;;
--dev-build)
dev_build="true"
shift
;;
*) # unknown option
print_help
shift
;;
esac
done
if [[ -z "$download_dir" ]]; then
download_dir="$(mktemp -d)"
trap 'rm -rf "$download_dir"' EXIT
fi
if [[ "$checksum" != "" ]] && [[ "$version" == "" ]]; then
>&2 echo "Options --checksum and --version should be provided together!"
exit 1
fi
if [[ "$version" == "" ]]; then
if [[ "$dev_build" == "true" ]]; then
version="$(curl -sL https://raw.githubusercontent.com/babashka/babashka/master/resources/BABASHKA_VERSION)"
else
version="$(curl -sL https://raw.githubusercontent.com/babashka/babashka/master/resources/BABASHKA_RELEASED_VERSION)"
fi
fi
case "$(uname -s)" in
Linux*) platform=linux;;
Darwin*) platform=macos;;
esac
# Ugly ugly conversion of version to a comparable number
IFS='.' read -ra VER <<< "${version//-SNAPSHOT/}"
vernum=$(printf "%03d%03d%03d" "${VER[0]}" "${VER[1]}" "${VER[2]}")
case "$(uname -m)" in
aarch64) arch=aarch64;;
arm64) if [[ 10#$vernum -le 10#000008002 ]]; then
arch="amd64"
else
arch="aarch64"
fi
;;
*) arch=amd64
# always use static image on linux
if [[ "$platform" == "linux" ]]; then
static_binary="true"
fi
;;
esac
if [[ 10#$vernum -le 10#000002013 ]]; then
ext="zip"
util="$(which unzip) -qqo"
else
ext="tar.gz"
util="$(which tar) -zxf"
fi
case "$platform-$static_binary" in
linux-true) filename="babashka-$version-$platform-$arch-static."$ext
;;
*-true) >&2 echo "Static binaries are only available in Linux platform! Using the non-static one..."
filename="babashka-$version-$platform-$arch."$ext
;;
*) filename="babashka-$version-$platform-$arch."$ext
;;
esac
if [[ "$version" == *-SNAPSHOT ]]
then
repo="babashka-dev-builds"
else
repo="babashka"
fi
download_url="https://github.com/babashka/$repo/releases/download/v$version/$filename"
# macOS only have shasum available by default
# Some Linux distros (RHEL-like) only have sha256sum available by default (others have both)
if command -v sha256sum >/dev/null; then
sha256sum_cmd="sha256sum"
elif command -v shasum >/dev/null; then
sha256sum_cmd="shasum -a 256"
else
>&2 echo "Either 'sha256sum' or 'shasum' needs to be on PATH for '--checksum' flag!"
>&2 echo "Exiting..."
exit 1
fi
# Running this part in a subshell so when it finishes we go back to the previous directory
mkdir -p "$download_dir" && (
cd "$download_dir"
echo -e "Downloading $download_url to $download_dir"
curl -o "$filename" -sL "$download_url"
if [[ -n "$checksum" ]]; then
if ! echo "$checksum *$filename" | $sha256sum_cmd --check --status; then
>&2 echo "Failed checksum on $filename"
>&2 echo "Got: $(shasum -a 256 "$filename" | cut -d' ' -f1)"
>&2 echo "Expected: $checksum"
exit 1
fi
fi
$util "$filename"
rm -f "$filename"
)
if [[ "$download_dir" != "$install_dir" ]]
then
mkdir -p "$install_dir"
if [ -f "$install_dir/bb" ]; then
echo "Moving $install_dir/bb to $install_dir/bb.old"
mv -f "$install_dir/bb" "$install_dir/bb.old"
fi
mv -f "$download_dir/bb" "$install_dir/bb"
fi
echo "Successfully installed bb in $install_dir"