Repository navigation
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·248 lines (209 loc) · 6.25 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·248 lines (209 loc) · 6.25 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
#!/usr/bin/env bash
set -euo pipefail
REPO="cloudapp3/vminfo"
BINARY_NAME="vminfo"
INSTALL_DIR="${VMINFO_INSTALL_DIR:-}"
VERSION=""
SKIP_VERIFY=0
usage() {
cat <<'EOF'
Install vminfo from GitHub Releases.
Usage:
install.sh [--version vX.Y.Z] [--dir PATH] [--skip-verify]
Options:
--version <tag> Install a specific release tag. Defaults to the latest release.
--dir <path> Install directory. Auto-detected in order: /usr/local/bin,
~/.local/bin, ~/bin. Override with VMINFO_INSTALL_DIR env var.
--skip-verify Skip SHA-256 checksum verification.
-h, --help Show this help message.
Examples:
curl -fsSL https://raw.githubusercontent.com/cloudapp3/vminfo/main/install.sh | sudo bash -s -- --dir /usr/local/bin
curl -fsSL https://raw.githubusercontent.com/cloudapp3/vminfo/main/install.sh | bash
curl -fsSL https://raw.githubusercontent.com/cloudapp3/vminfo/main/install.sh | bash -s -- --version v0.1.0
EOF
}
log() {
printf '%s\n' "$*" >&2
}
die() {
log "Error: $*"
exit 1
}
require_cmd() {
command -v "$1" >/dev/null 2>&1 || die "required command not found: $1"
}
path_has_dir() (
dir="$1"
while [ "$dir" != "/" ] && [ "${dir%/}" != "$dir" ]; do
dir="${dir%/}"
done
[ -n "$dir" ] || return 1
IFS=:
for path_dir in ${PATH:-}; do
while [ "$path_dir" != "/" ] && [ "${path_dir%/}" != "$path_dir" ]; do
path_dir="${path_dir%/}"
done
[ "$path_dir" = "$dir" ] && return 0
done
return 1
)
print_path_hint() {
local install_dir="$1"
local target_path="$2"
if path_has_dir "$install_dir"; then
log "Verify the installation with:"
log " ${BINARY_NAME} version"
return
fi
log "Warning: ${install_dir} is not in your PATH, so running \"${BINARY_NAME}\" may fail."
log ""
log "You can run it directly with:"
log " ${target_path} version"
log ""
if [ "$install_dir" != "/usr/local/bin" ]; then
log "To make \"${BINARY_NAME}\" available globally, either create a symlink:"
log " sudo ln -sf ${target_path} /usr/local/bin/${BINARY_NAME}"
log ""
fi
log "Or add ${install_dir} to your shell PATH:"
log " export PATH=\"${install_dir}:\$PATH\""
}
while [ "$#" -gt 0 ]; do
case "$1" in
--version)
shift
[ "$#" -gt 0 ] || die "missing value for --version"
VERSION="$1"
;;
--version=*)
VERSION="${1#*=}"
;;
--dir)
shift
[ "$#" -gt 0 ] || die "missing value for --dir"
INSTALL_DIR="$1"
;;
--dir=*)
INSTALL_DIR="${1#*=}"
;;
--skip-verify)
SKIP_VERIFY=1
;;
-h|--help)
usage
exit 0
;;
*)
die "unknown argument: $1"
;;
esac
shift
done
require_cmd curl
require_cmd tar
require_cmd uname
require_cmd mktemp
# Auto-detect install directory if not explicitly set
if [ -z "$INSTALL_DIR" ]; then
for d in /usr/local/bin "$HOME/.local/bin" "$HOME/bin"; do
if [ -w "$d" ] 2>/dev/null || [ -w "$(dirname "$d")" ] 2>/dev/null; then
INSTALL_DIR="$d"
break
fi
done
INSTALL_DIR="${INSTALL_DIR:-$HOME/.local/bin}"
fi
case "$(uname -s)" in
Linux) OS="linux" ;;
Darwin) OS="darwin" ;;
*)
die "unsupported operating system: $(uname -s)"
;;
esac
case "$(uname -m)" in
x86_64|amd64) ARCH="amd64" ;;
aarch64|arm64) ARCH="arm64" ;;
*)
die "unsupported architecture: $(uname -m)"
;;
esac
TOKEN="${GITHUB_TOKEN:-${GH_TOKEN:-}}"
CURL_ARGS=(-fsSL)
if [ -n "${TOKEN}" ]; then
CURL_ARGS+=(
-H "Authorization: Bearer ${TOKEN}"
-H "Accept: application/vnd.github+json"
-H "X-GitHub-Api-Version: 2022-11-28"
)
fi
curl_text() {
curl "${CURL_ARGS[@]}" "$1"
}
curl_download() {
local url="$1"
local output="$2"
curl "${CURL_ARGS[@]}" -o "$output" "$url"
}
if [ -z "$VERSION" ]; then
log "Resolving latest release from ${REPO}..."
VERSION="$(
curl_text "https://api.github.com/repos/${REPO}/releases/latest" \
| sed -n 's/^[[:space:]]*"tag_name":[[:space:]]*"\([^"]*\)".*/\1/p' \
| head -n 1
)"
[ -n "$VERSION" ] || die "failed to resolve latest release tag"
fi
case "$VERSION" in
v*) ;;
*) VERSION="v${VERSION}" ;;
esac
VERSION_NO_V="${VERSION#v}"
ARCHIVE_NAME="${BINARY_NAME}-${VERSION_NO_V}-${OS}-${ARCH}.tar.gz"
BASE_URL="https://github.com/${REPO}/releases/download/${VERSION}"
ARCHIVE_URL="${BASE_URL}/${ARCHIVE_NAME}"
CHECKSUMS_URL="${BASE_URL}/checksums.txt"
TMPDIR="$(mktemp -d)"
trap 'rm -rf "$TMPDIR"' EXIT
ARCHIVE_PATH="${TMPDIR}/${ARCHIVE_NAME}"
CHECKSUMS_PATH="${TMPDIR}/checksums.txt"
log "Downloading ${ARCHIVE_NAME}..."
curl_download "$ARCHIVE_URL" "$ARCHIVE_PATH" || die "failed to download ${ARCHIVE_URL}"
if [ "$SKIP_VERIFY" -eq 0 ]; then
log "Downloading checksums.txt..."
curl_download "$CHECKSUMS_URL" "$CHECKSUMS_PATH" || die "failed to download ${CHECKSUMS_URL}"
EXPECTED_SUM="$(
awk -v name="$ARCHIVE_NAME" '$2 == name { print $1 }' "$CHECKSUMS_PATH"
)"
[ -n "$EXPECTED_SUM" ] || die "checksum entry not found for ${ARCHIVE_NAME}"
if command -v sha256sum >/dev/null 2>&1; then
ACTUAL_SUM="$(sha256sum "$ARCHIVE_PATH" | awk '{print $1}')"
elif command -v shasum >/dev/null 2>&1; then
ACTUAL_SUM="$(shasum -a 256 "$ARCHIVE_PATH" | awk '{print $1}')"
else
die "checksum verification requested but neither sha256sum nor shasum is available"
fi
[ "$ACTUAL_SUM" = "$EXPECTED_SUM" ] || die "checksum mismatch for ${ARCHIVE_NAME}"
log "Checksum verified."
else
log "Skipping checksum verification."
fi
log "Extracting archive..."
tar -xzf "$ARCHIVE_PATH" -C "$TMPDIR"
BINARY_PATH="${TMPDIR}/${BINARY_NAME}"
if [ ! -f "$BINARY_PATH" ]; then
BINARY_PATH="$(find "$TMPDIR" -maxdepth 2 -type f -name "$BINARY_NAME" | head -n 1 || true)"
fi
[ -n "${BINARY_PATH}" ] && [ -f "$BINARY_PATH" ] || die "failed to find ${BINARY_NAME} in archive"
if [ ! -d "$INSTALL_DIR" ]; then
mkdir -p "$INSTALL_DIR" 2>/dev/null || die "cannot create install directory: ${INSTALL_DIR}"
fi
[ -w "$INSTALL_DIR" ] || die "install directory is not writable: ${INSTALL_DIR}"
TARGET_PATH="${INSTALL_DIR}/${BINARY_NAME}"
if command -v install >/dev/null 2>&1; then
install -m 0755 "$BINARY_PATH" "$TARGET_PATH"
else
cp "$BINARY_PATH" "$TARGET_PATH"
chmod 0755 "$TARGET_PATH"
fi
log "Installed ${BINARY_NAME} ${VERSION} to ${TARGET_PATH}"
print_path_hint "$INSTALL_DIR" "$TARGET_PATH"