-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
270 lines (221 loc) · 7.86 KB
/
install.sh
File metadata and controls
270 lines (221 loc) · 7.86 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
#!/bin/sh
# DevTrail CLI installer — https://github.com/StrangeDaysTech/devtrail
#
# Usage:
# curl -fsSL https://raw.githubusercontent.com/StrangeDaysTech/devtrail/main/install.sh | sh
# curl -fsSL ... | sh -s -- --tag v2.0.0 --to ~/.local/bin
#
# Compatible with bash, zsh, dash, and POSIX sh.
set -eu
REPO="StrangeDaysTech/devtrail"
BINARY="devtrail"
# ── Helpers ──────────────────────────────────────────────────────────────
say() {
printf 'devtrail-install: %s\n' "$*"
}
err() {
say "ERROR: $*" >&2
exit 1
}
need() {
if ! command -v "$1" >/dev/null 2>&1; then
err "$1 is required but not found in PATH"
fi
}
download() {
_url="$1"
_out="$2"
_auth_header=""
if [ -n "${GITHUB_TOKEN:-}" ]; then
_auth_header="Authorization: token ${GITHUB_TOKEN}"
fi
if command -v curl >/dev/null 2>&1; then
if [ -n "$_auth_header" ]; then
curl -fsSL -H "$_auth_header" -o "$_out" "$_url"
else
curl -fsSL -o "$_out" "$_url"
fi
elif command -v wget >/dev/null 2>&1; then
if [ -n "$_auth_header" ]; then
wget -q --header="$_auth_header" -O "$_out" "$_url"
else
wget -q -O "$_out" "$_url"
fi
else
err "curl or wget is required to download files"
fi
}
usage() {
cat <<EOF
DevTrail CLI installer
USAGE:
install.sh [OPTIONS]
OPTIONS:
--tag <TAG> Install a specific version (e.g. v2.0.0). Default: latest
--to <DIR> Installation directory. Default: ~/.local/bin (or /usr/local/bin with sudo)
--help Show this help message
ENVIRONMENT:
GITHUB_TOKEN GitHub API token to avoid rate limits
EXAMPLES:
curl -fsSL https://raw.githubusercontent.com/StrangeDaysTech/devtrail/main/install.sh | sh
curl -fsSL ... | sh -s -- --tag v2.0.0
curl -fsSL ... | sh -s -- --to /usr/local/bin
EOF
}
# ── Parse arguments ──────────────────────────────────────────────────────
TAG=""
INSTALL_DIR=""
while [ $# -gt 0 ]; do
case "$1" in
--tag)
[ $# -ge 2 ] || err "--tag requires a value"
TAG="$2"
shift 2
;;
--to)
[ $# -ge 2 ] || err "--to requires a value"
INSTALL_DIR="$2"
shift 2
;;
--help)
usage
exit 0
;;
*)
err "unknown option: $1 (use --help for usage)"
;;
esac
done
# ── Detect platform ─────────────────────────────────────────────────────
detect_target() {
_os="$(uname -s)"
_arch="$(uname -m)"
case "$_os" in
Linux|linux) _os_part="unknown-linux-gnu" ;;
Darwin|darwin) _os_part="apple-darwin" ;;
*) err "unsupported OS: $_os" ;;
esac
case "$_arch" in
x86_64|amd64) _arch_part="x86_64" ;;
aarch64|arm64) _arch_part="aarch64" ;;
*) err "unsupported architecture: $_arch" ;;
esac
# Only supported combinations
case "${_arch_part}-${_os_part}" in
x86_64-unknown-linux-gnu) ;;
x86_64-apple-darwin) ;;
aarch64-apple-darwin) ;;
*) err "unsupported platform: ${_os}/${_arch} (target: ${_arch_part}-${_os_part})" ;;
esac
TARGET="${_arch_part}-${_os_part}"
}
# ── Resolve install directory ────────────────────────────────────────────
resolve_install_dir() {
if [ -n "$INSTALL_DIR" ]; then
return
fi
if [ "$(id -u)" -eq 0 ]; then
INSTALL_DIR="/usr/local/bin"
else
INSTALL_DIR="${HOME}/.local/bin"
fi
}
# ── Get latest tag ───────────────────────────────────────────────────────
get_latest_tag() {
_api_url="https://api.github.com/repos/${REPO}/releases/latest"
_tmp_json="${TMPDIR_CLEANUP}/release.json"
say "fetching latest release info..."
if ! download "$_api_url" "$_tmp_json" 2>/dev/null; then
echo ""
echo " Failed to fetch release info from GitHub API." >&2
echo " This may be due to rate limiting." >&2
echo " Set GITHUB_TOKEN to authenticate, or use --tag to specify a version." >&2
echo "" >&2
err "could not determine latest version"
fi
# Extract tag_name from JSON without jq (POSIX-compatible)
TAG=$(sed -n 's/.*"tag_name"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p' "$_tmp_json" | head -1)
if [ -z "$TAG" ]; then
err "could not parse latest release tag from GitHub API response"
fi
say "latest version: ${TAG}"
}
# ── Main ─────────────────────────────────────────────────────────────────
main() {
detect_target
resolve_install_dir
# Check dependencies
if ! command -v curl >/dev/null 2>&1 && ! command -v wget >/dev/null 2>&1; then
err "curl or wget is required"
fi
need tar
# Create temp directory with cleanup trap
TMPDIR_CLEANUP="$(mktemp -d)"
trap 'rm -rf "$TMPDIR_CLEANUP"' EXIT
# Get version
if [ -z "$TAG" ]; then
get_latest_tag
else
say "using specified version: ${TAG}"
fi
# Strip leading 'v' for the version in asset name (asset uses v-prefix)
VERSION_NUM="${TAG#v}"
# Build asset name and URL
ASSET="devtrail-cli-v${VERSION_NUM}-${TARGET}.tar.gz"
URL="https://github.com/${REPO}/releases/download/${TAG}/${ASSET}"
say "downloading ${ASSET}..."
ARCHIVE="${TMPDIR_CLEANUP}/${ASSET}"
if ! download "$URL" "$ARCHIVE"; then
echo "" >&2
echo " Download failed. Possible causes:" >&2
echo " - Version ${TAG} does not exist" >&2
echo " - No binary available for ${TARGET}" >&2
echo " - Network connectivity issue" >&2
echo "" >&2
err "failed to download ${URL}"
fi
# Extract binary
say "extracting ${BINARY}..."
tar xzf "$ARCHIVE" -C "$TMPDIR_CLEANUP"
if [ ! -f "${TMPDIR_CLEANUP}/${BINARY}" ]; then
err "binary '${BINARY}' not found in archive"
fi
# Install
mkdir -p "$INSTALL_DIR"
if ! cp "${TMPDIR_CLEANUP}/${BINARY}" "${INSTALL_DIR}/${BINARY}" 2>/dev/null; then
echo "" >&2
echo " Permission denied writing to ${INSTALL_DIR}." >&2
echo " Try one of:" >&2
echo " sudo sh install.sh --to /usr/local/bin" >&2
echo " sh install.sh --to ~/.local/bin" >&2
echo "" >&2
err "failed to install binary to ${INSTALL_DIR}"
fi
chmod +x "${INSTALL_DIR}/${BINARY}"
say "installed ${BINARY} to ${INSTALL_DIR}/${BINARY}"
# Verify
if "${INSTALL_DIR}/${BINARY}" --version >/dev/null 2>&1; then
_version=$("${INSTALL_DIR}/${BINARY}" --version)
say "verified: ${_version}"
else
say "warning: could not verify installation (binary may not run on this platform)"
fi
# PATH check
case ":${PATH}:" in
*":${INSTALL_DIR}:"*)
;;
*)
echo ""
echo " ${INSTALL_DIR} is not in your PATH."
echo " Add it by running:"
echo ""
echo " export PATH=\"${INSTALL_DIR}:\$PATH\""
echo ""
echo " To make it permanent, add that line to your shell profile:"
echo " ~/.bashrc, ~/.zshrc, or ~/.profile"
echo ""
;;
esac
say "done!"
}
main