-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·321 lines (264 loc) · 10.7 KB
/
install.sh
File metadata and controls
executable file
·321 lines (264 loc) · 10.7 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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#!/usr/bin/env bash
# Copyright 2025 Infoblox Inc.
# SPDX-License-Identifier: Apache-2.0
#
# Standalone installer for the apx CLI.
# Downloads a pre-built binary from GitHub Releases, verifies its SHA-256
# checksum, and installs it to a configurable directory.
#
# Usage:
# curl -fsSL https://raw.githubusercontent.com/infobloxopen/apx/main/install.sh | bash
# curl -fsSL ... | VERSION=1.2.3 bash
# curl -fsSL ... | INSTALL_DIR=/usr/local/bin bash
#
# Environment variables:
# VERSION - Pin a specific release (default: latest)
# INSTALL_DIR - Where to place the binary (default: ~/.local/bin)
# GITHUB_TOKEN - Optional token for GitHub API (avoids rate limits)
# NO_COLOR - Disable colored output when set
set -euo pipefail
# ── Globals ──────────────────────────────────────────────────────────────────
REPO_OWNER="infobloxopen"
REPO_NAME="apx"
BINARY_NAME="apx"
GITHUB_BASE="https://github.com/${REPO_OWNER}/${REPO_NAME}"
API_BASE="https://api.github.com/repos/${REPO_OWNER}/${REPO_NAME}"
# ── Colors ───────────────────────────────────────────────────────────────────
setup_colors() {
if [[ -n "${NO_COLOR:-}" ]] || [[ ! -t 1 ]]; then
RED=""
GREEN=""
YELLOW=""
BLUE=""
BOLD=""
RESET=""
else
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
BOLD='\033[1m'
RESET='\033[0m'
fi
}
# ── Logging ──────────────────────────────────────────────────────────────────
info() { printf "%b\n" "${BLUE}info${RESET} $*"; }
ok() { printf "%b\n" "${GREEN} ok${RESET} $*"; }
warn() { printf "%b\n" "${YELLOW}warn${RESET} $*" >&2; }
error() { printf "%b\n" "${RED}error${RESET} $*" >&2; }
die() { error "$@"; exit 1; }
# ── Prerequisite checks ─────────────────────────────────────────────────────
need_cmd() {
if ! command -v "$1" &>/dev/null; then
die "Required command '${BOLD}$1${RESET}' not found. Please install it and retry."
fi
}
check_prerequisites() {
need_cmd curl
need_cmd tar
need_cmd uname
# At least one SHA-256 tool must be available.
if command -v sha256sum &>/dev/null; then
SHA_CMD="sha256sum"
elif command -v shasum &>/dev/null; then
SHA_CMD="shasum -a 256"
else
die "Neither ${BOLD}sha256sum${RESET} nor ${BOLD}shasum${RESET} found. Cannot verify checksums."
fi
}
# ── Platform detection ───────────────────────────────────────────────────────
detect_os() {
local os
os="$(uname -s)"
case "$os" in
Linux*) echo "linux" ;;
Darwin*) echo "darwin" ;;
MINGW*|MSYS*|CYGWIN*)
echo "windows" ;;
*)
die "Unsupported operating system: ${BOLD}$os${RESET}" ;;
esac
}
detect_arch() {
local arch
arch="$(uname -m)"
case "$arch" in
x86_64|amd64) echo "amd64" ;;
arm64|aarch64) echo "arm64" ;;
*)
die "Unsupported architecture: ${BOLD}$arch${RESET}" ;;
esac
}
# ── Version resolution ───────────────────────────────────────────────────────
# Resolve the latest release tag from GitHub.
# Strategy 1: redirect-based (no API rate limit).
# Strategy 2: GitHub REST API (fallback).
resolve_latest_version() {
local version=""
local auth_header=()
if [[ -n "${GITHUB_TOKEN:-}" ]]; then
auth_header=(-H "Authorization: token ${GITHUB_TOKEN}")
fi
# Strategy 1 – follow redirect on /releases/latest and grab tag from URL.
version=$(curl -fsSI "${auth_header[@]+"${auth_header[@]}"}" \
"${GITHUB_BASE}/releases/latest" 2>/dev/null \
| grep -i '^location:' \
| sed -E 's|.*/tag/v?||;s/[[:space:]]//g') || true
if [[ -n "$version" ]]; then
echo "$version"
return
fi
# Strategy 2 – GitHub API.
version=$(curl -fsSL "${auth_header[@]+"${auth_header[@]}"}" \
"${API_BASE}/releases/latest" 2>/dev/null \
| grep '"tag_name":' \
| sed -E 's/.*"v?([^"]+)".*/\1/') || true
if [[ -n "$version" ]]; then
echo "$version"
return
fi
die "Could not determine the latest release version.\n" \
" Set ${BOLD}VERSION${RESET} explicitly or provide ${BOLD}GITHUB_TOKEN${RESET} to avoid rate limits."
}
# ── Download helpers ─────────────────────────────────────────────────────────
download() {
local url="$1" dest="$2"
local auth_header=()
if [[ -n "${GITHUB_TOKEN:-}" ]]; then
auth_header=(-H "Authorization: token ${GITHUB_TOKEN}")
fi
local http_code
http_code=$(curl -fsSL "${auth_header[@]+"${auth_header[@]}"}" \
-w "%{http_code}" -o "$dest" "$url") || true
if [[ "$http_code" -lt 200 || "$http_code" -ge 300 ]]; then
die "Download failed (HTTP $http_code): $url"
fi
}
# ── Checksum verification ───────────────────────────────────────────────────
verify_checksum() {
local archive="$1" checksums_file="$2"
local archive_name
archive_name="$(basename "$archive")"
local expected
expected=$(grep "${archive_name}" "$checksums_file" | awk '{print $1}')
if [[ -z "$expected" ]]; then
die "No checksum found for ${BOLD}${archive_name}${RESET} in checksums.txt"
fi
local actual
actual=$($SHA_CMD "$archive" | awk '{print $1}')
if [[ "$expected" != "$actual" ]]; then
error "Checksum mismatch for ${BOLD}${archive_name}${RESET}"
error " expected: $expected"
error " actual: $actual"
die "The downloaded file may be corrupted or tampered with."
fi
}
# ── PATH helper ──────────────────────────────────────────────────────────────
ensure_in_path() {
local dir="$1"
if [[ ":${PATH}:" == *":${dir}:"* ]]; then
return
fi
warn "${BOLD}${dir}${RESET} is not in your PATH."
local shell_name
shell_name="$(basename "${SHELL:-/bin/sh}")"
local rc_file=""
# shellcheck disable=SC2088 # Tildes are intentional — shown to user as typed paths
case "$shell_name" in
bash) rc_file="~/.bashrc" ;;
zsh) rc_file="~/.zshrc" ;;
fish) rc_file="~/.config/fish/config.fish" ;;
esac
if [[ -n "$rc_file" ]]; then
warn "Add it by running:"
if [[ "$shell_name" == "fish" ]]; then
warn " fish_add_path ${dir}"
else
warn " echo 'export PATH=\"${dir}:\$PATH\"' >> ${rc_file}"
fi
else
warn "Add ${dir} to your shell's configuration file."
fi
}
# ── Main ─────────────────────────────────────────────────────────────────────
main() {
setup_colors
printf "%b\n" "${BOLD}apx installer${RESET}"
echo ""
check_prerequisites
# ── Platform ──
local os arch
os="$(detect_os)"
arch="$(detect_arch)"
info "Platform: ${BOLD}${os}/${arch}${RESET}"
# ── Version ──
local version="${VERSION:-}"
if [[ -z "$version" ]]; then
info "Resolving latest version…"
version="$(resolve_latest_version)"
fi
# Strip leading 'v' if present for archive naming.
version="${version#v}"
info "Version: ${BOLD}v${version}${RESET}"
# ── Archive naming ──
local ext="tar.gz"
if [[ "$os" == "windows" ]]; then
ext="zip"
fi
local archive_name="${BINARY_NAME}_${version}_${os}_${arch}.${ext}"
local release_tag="v${version}"
local base_url="${GITHUB_BASE}/releases/download/${release_tag}"
# ── Temp directory ──
local tmpdir
tmpdir="$(mktemp -d)"
trap 'rm -rf "$tmpdir"' EXIT
# ── Download archive + checksums ──
info "Downloading ${BOLD}${archive_name}${RESET}…"
download "${base_url}/${archive_name}" "${tmpdir}/${archive_name}"
info "Downloading ${BOLD}checksums.txt${RESET}…"
download "${base_url}/checksums.txt" "${tmpdir}/checksums.txt"
# ── Verify integrity ──
info "Verifying checksum…"
verify_checksum "${tmpdir}/${archive_name}" "${tmpdir}/checksums.txt"
ok "Checksum verified"
# ── Extract ──
info "Extracting…"
if [[ "$ext" == "tar.gz" ]]; then
tar -xzf "${tmpdir}/${archive_name}" -C "$tmpdir"
else
# Windows zip — requires unzip.
need_cmd unzip
unzip -oq "${tmpdir}/${archive_name}" -d "$tmpdir"
fi
if [[ ! -f "${tmpdir}/${BINARY_NAME}" ]]; then
die "Expected binary '${BINARY_NAME}' not found in archive."
fi
# ── Install ──
local install_dir="${INSTALL_DIR:-${HOME}/.local/bin}"
mkdir -p "$install_dir"
local dest="${install_dir}/${BINARY_NAME}"
# If the target requires elevated privileges, try sudo.
if [[ -w "$install_dir" ]]; then
mv "${tmpdir}/${BINARY_NAME}" "$dest"
chmod +x "$dest"
else
warn "${BOLD}${install_dir}${RESET} is not writable — using sudo."
sudo mv "${tmpdir}/${BINARY_NAME}" "$dest"
sudo chmod +x "$dest"
fi
ok "Installed ${BOLD}${BINARY_NAME}${RESET} to ${BOLD}${dest}${RESET}"
# ── PATH check ──
ensure_in_path "$install_dir"
# ── Verify installation ──
echo ""
if command -v "$BINARY_NAME" &>/dev/null; then
info "Verify: $("$BINARY_NAME" --version 2>/dev/null || echo "${BINARY_NAME} v${version}")"
else
warn "Run '${BINARY_NAME} --version' after updating your PATH to verify."
fi
echo ""
printf "%b\n" "${GREEN}${BOLD}apx v${version}${RESET}${GREEN} installed successfully.${RESET}"
}
# Wrap everything in main so the entire script is parsed before execution.
# This is critical for `curl | bash` usage.
main "$@"