-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·149 lines (129 loc) · 4.18 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·149 lines (129 loc) · 4.18 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
#!/bin/sh
# install.sh — download and install the latest ana release.
#
# Usage:
# curl -fsSL https://raw.githubusercontent.com/highperformance-tech/ana-cli/main/install.sh | sh
#
# Environment overrides:
# INSTALL_DIR Where to place the ana binary (default: /usr/local/bin).
# ANA_VERSION Pin a specific release tag (default: latest).
#
# Requires: curl (or wget), tar, sha256sum or shasum, uname.
# Windows is not supported — download the .zip from the releases page instead.
set -eu
REPO="highperformance-tech/ana-cli"
INSTALL_DIR="${INSTALL_DIR:-/usr/local/bin}"
ANA_VERSION="${ANA_VERSION:-latest}"
log() {
printf '==> %s\n' "$*"
}
die() {
printf 'error: %s\n' "$*" >&2
exit 1
}
detect_os() {
case "$(uname -s)" in
Darwin) echo darwin ;;
Linux) echo linux ;;
MINGW*|MSYS*|CYGWIN*)
die "install.sh does not support Windows — download the .zip from https://github.com/${REPO}/releases"
;;
*) die "unsupported OS: $(uname -s)" ;;
esac
}
detect_arch() {
case "$(uname -m)" in
x86_64|amd64) echo amd64 ;;
arm64|aarch64) echo arm64 ;;
*) die "unsupported architecture: $(uname -m)" ;;
esac
}
fetch() {
# fetch URL -> stdout. Prefers curl; falls back to wget.
if command -v curl >/dev/null 2>&1; then
curl -fsSL "$1"
elif command -v wget >/dev/null 2>&1; then
wget -qO- "$1"
else
die "curl or wget required"
fi
}
fetch_file() {
# fetch_file URL DEST. Downloads to DEST, failing on HTTP errors.
if command -v curl >/dev/null 2>&1; then
curl -fsSL -o "$2" "$1"
elif command -v wget >/dev/null 2>&1; then
wget -q -O "$2" "$1"
else
die "curl or wget required"
fi
}
resolve_version() {
if [ "$ANA_VERSION" != "latest" ]; then
echo "$ANA_VERSION"
return
fi
# The /releases/latest endpoint returns a JSON blob whose "tag_name" is
# the release tag (e.g. v0.1.0). We use a minimal sed/awk pipeline to
# avoid a jq dependency.
tag=$(fetch "https://api.github.com/repos/${REPO}/releases/latest" \
| sed -n 's/.*"tag_name": *"\([^"]*\)".*/\1/p' \
| head -n1)
[ -n "$tag" ] || die "could not resolve latest release tag"
echo "$tag"
}
verify_checksum() {
# verify_checksum ARCHIVE_PATH CHECKSUMS_PATH
# Prefixed names avoid clobbering caller-scope `archive` in POSIX sh
# (no `local` keyword, so function vars are global by default).
_vc_archive="$1"
_vc_checksums="$2"
_vc_name=$(basename "$_vc_archive")
_vc_expected=$(grep " $_vc_name\$" "$_vc_checksums" | awk '{print $1}')
[ -n "$_vc_expected" ] || die "no checksum entry for $_vc_name"
if command -v sha256sum >/dev/null 2>&1; then
_vc_actual=$(sha256sum "$_vc_archive" | awk '{print $1}')
elif command -v shasum >/dev/null 2>&1; then
_vc_actual=$(shasum -a 256 "$_vc_archive" | awk '{print $1}')
else
die "sha256sum or shasum required"
fi
if [ "$_vc_expected" != "$_vc_actual" ]; then
die "checksum mismatch: expected $_vc_expected, got $_vc_actual"
fi
}
main() {
os=$(detect_os)
arch=$(detect_arch)
tag=$(resolve_version)
# GoReleaser strips the leading v from {{ .Version }} when templating the
# archive name, so a v0.1.0 tag produces ana_0.1.0_linux_amd64.tar.gz.
version="${tag#v}"
archive="ana_${version}_${os}_${arch}.tar.gz"
base="https://github.com/${REPO}/releases/download/${tag}"
log "installing ana ${tag} for ${os}/${arch}"
tmpdir=$(mktemp -d)
trap 'rm -rf "$tmpdir"' EXIT
log "downloading ${archive}"
fetch_file "${base}/${archive}" "${tmpdir}/${archive}"
log "downloading checksums.txt"
fetch_file "${base}/checksums.txt" "${tmpdir}/checksums.txt"
log "verifying checksum"
verify_checksum "${tmpdir}/${archive}" "${tmpdir}/checksums.txt"
log "extracting"
tar -xzf "${tmpdir}/${archive}" -C "$tmpdir" ana
# Install target may require sudo when INSTALL_DIR is not user-writable.
if [ -w "$INSTALL_DIR" ]; then
install_cmd=""
elif command -v sudo >/dev/null 2>&1; then
install_cmd="sudo"
log "using sudo to install to $INSTALL_DIR"
else
die "$INSTALL_DIR is not writable and sudo is unavailable; set INSTALL_DIR to a writable path"
fi
mkdir -p "$INSTALL_DIR" 2>/dev/null || $install_cmd mkdir -p "$INSTALL_DIR"
$install_cmd install -m 0755 "${tmpdir}/ana" "${INSTALL_DIR}/ana"
log "installed to ${INSTALL_DIR}/ana"
"${INSTALL_DIR}/ana" --version || true
}
main "$@"