-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·254 lines (224 loc) · 5.57 KB
/
install.sh
File metadata and controls
executable file
·254 lines (224 loc) · 5.57 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
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
#!/usr/bin/env bash
set -euo pipefail
APP_NAME="weightless"
DEFAULT_REPO="needle-tools/weightless"
DEFAULT_VERSION="latest"
INSTALL_DIR="${HOME}/.local/bin"
REPO="${DEFAULT_REPO}"
VERSION="${DEFAULT_VERSION}"
BUILD_FROM_SOURCE="false"
usage() {
cat <<EOF
${APP_NAME} installer
Usage:
./install.sh
./install.sh -s 1.0.2
./install.sh --prefix /usr/local/bin
./install.sh --build-from-source
Options:
-s, --version VERSION install a specific release version or 'latest'
-p, --prefix DIR install binary into DIR
--repo OWNER/REPO GitHub repository to install from
--build-from-source build from the current checkout instead of downloading a release
-h, --help show this help
EOF
}
while [[ $# -gt 0 ]]; do
case "$1" in
-s|--version)
VERSION="${2:-}"
if [[ -z "${VERSION}" ]]; then
echo "missing value for $1" >&2
exit 1
fi
shift 2
;;
-p|--prefix)
INSTALL_DIR="${2:-}"
if [[ -z "${INSTALL_DIR}" ]]; then
echo "missing value for $1" >&2
exit 1
fi
shift 2
;;
--repo)
REPO="${2:-}"
if [[ -z "${REPO}" ]]; then
echo "missing value for $1" >&2
exit 1
fi
shift 2
;;
--build-from-source)
BUILD_FROM_SOURCE="true"
shift
;;
-h|--help)
usage
exit 0
;;
*)
echo "unknown option: $1" >&2
usage >&2
exit 1
;;
esac
done
require_cmd() {
if ! command -v "$1" >/dev/null 2>&1; then
echo "missing required command: $1" >&2
exit 1
fi
}
detect_os() {
case "$(uname -s)" in
Darwin) echo "darwin" ;;
Linux) echo "linux" ;;
MINGW*|MSYS*|CYGWIN*) echo "windows" ;;
*)
echo "unsupported operating system: $(uname -s)" >&2
exit 1
;;
esac
}
detect_arch() {
case "$(uname -m)" in
x86_64|amd64) echo "amd64" ;;
arm64|aarch64) echo "arm64" ;;
*)
echo "unsupported architecture: $(uname -m)" >&2
exit 1
;;
esac
}
normalize_tag() {
local value="$1"
if [[ "${value}" == "latest" ]]; then
printf "latest\n"
return 0
fi
value="${value#v}"
printf "v%s\n" "${value}"
}
release_version_from_tag() {
local tag="$1"
printf "%s\n" "${tag#v}"
}
fetch_latest_tag() {
require_cmd curl
curl -fsSL "https://api.github.com/repos/${REPO}/releases/latest" |
sed -n 's/.*"tag_name"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p' |
head -n1
}
download() {
local url="$1"
local out="$2"
curl -fsSL --retry 3 --connect-timeout 10 -o "${out}" "${url}"
}
checksum_file() {
local file="$1"
if command -v shasum >/dev/null 2>&1; then
shasum -a 256 "${file}" | awk '{print $1}'
return 0
fi
if command -v sha256sum >/dev/null 2>&1; then
sha256sum "${file}" | awk '{print $1}'
return 0
fi
if command -v openssl >/dev/null 2>&1; then
openssl dgst -sha256 "${file}" | awk '{print $NF}'
return 0
fi
return 1
}
verify_checksum() {
local archive="$1"
local checksums="$2"
local asset="$3"
local expected
expected="$(awk -v name="${asset}" '$2 == name { print $1 }' "${checksums}")"
if [[ -z "${expected}" ]]; then
echo "warning: checksum for ${asset} not found, skipping verification" >&2
return 0
fi
local actual
if ! actual="$(checksum_file "${archive}")"; then
echo "warning: no checksum tool available, skipping verification" >&2
return 0
fi
if [[ "${actual}" != "${expected}" ]]; then
echo "checksum mismatch for ${asset}" >&2
echo "expected: ${expected}" >&2
echo "actual: ${actual}" >&2
exit 1
fi
}
build_from_source() {
require_cmd go
local script_dir
script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
(
cd "${script_dir}"
go build -o "${TMP_DIR}/${APP_NAME}" .
)
}
download_release_binary() {
require_cmd curl
local os arch tag version asset ext base_url archive checksum_url checksum_file_path
os="$(detect_os)"
arch="$(detect_arch)"
tag="$(normalize_tag "${VERSION}")"
if [[ "${tag}" == "latest" ]]; then
tag="$(fetch_latest_tag)"
if [[ -z "${tag}" ]]; then
echo "failed to determine latest release tag from ${REPO}" >&2
exit 1
fi
fi
version="$(release_version_from_tag "${tag}")"
if [[ "${os}" == "windows" ]]; then
ext="zip"
else
ext="tar.gz"
fi
asset="${APP_NAME}_${version}_${os}_${arch}.${ext}"
base_url="https://github.com/${REPO}/releases/download/${tag}"
archive="${TMP_DIR}/${asset}"
checksum_file_path="${TMP_DIR}/checksums.txt"
checksum_url="${base_url}/checksums.txt"
echo "Downloading ${APP_NAME} ${tag} for ${os}/${arch}..."
download "${base_url}/${asset}" "${archive}"
download "${checksum_url}" "${checksum_file_path}"
verify_checksum "${archive}" "${checksum_file_path}" "${asset}"
if [[ "${ext}" == "zip" ]]; then
require_cmd unzip
unzip -q "${archive}" -d "${TMP_DIR}"
else
tar -xzf "${archive}" -C "${TMP_DIR}"
fi
}
TMP_DIR="$(mktemp -d)"
trap 'rm -rf "${TMP_DIR}"' EXIT
mkdir -p "${INSTALL_DIR}"
if [[ "${BUILD_FROM_SOURCE}" == "true" ]]; then
echo "Building ${APP_NAME} from source..."
build_from_source
else
download_release_binary
fi
install -m 0755 "${TMP_DIR}/${APP_NAME}" "${INSTALL_DIR}/${APP_NAME}"
echo "Installed ${APP_NAME} to ${INSTALL_DIR}/${APP_NAME}"
echo
echo "Run:"
echo " ${APP_NAME}"
echo " ${APP_NAME} --json"
echo " ${APP_NAME} --version"
case ":${PATH}:" in
*":${INSTALL_DIR}:"*) ;;
*)
echo
echo "Note: ${INSTALL_DIR} is not currently on your PATH."
echo "Add this to your shell profile:"
echo " export PATH=\"${INSTALL_DIR}:\$PATH\""
;;
esac