-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathinstall.sh
More file actions
237 lines (196 loc) · 8.07 KB
/
Copy pathinstall.sh
File metadata and controls
237 lines (196 loc) · 8.07 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
#!/bin/sh
set -eu
REPO="harness/cli"
INSTALL_DIR="${HARNESS_INSTALL_DIR:-$HOME/.local/bin}"
USER_OVERRIDE="${HARNESS_INSTALL_DIR:+yes}"
NONINTERACTIVE="${HARNESS_NONINTERACTIVE:-}"
NO_VERIFY="${HARNESS_NO_VERIFY:-}"
CORE_ONLY="${HARNESS_CORE_ONLY:-}"
# ── helpers ────────────────────────────────────────────────────────────────────
info() { printf ' \033[34m•\033[0m %s\n' "$*"; }
success() { printf ' \033[32m✓\033[0m %s\n' "$*"; }
warn() { printf ' \033[33m!\033[0m %s\n' "$*"; }
error() { printf ' \033[31m✗\033[0m %s\n' "$*" >&2; exit 1; }
is_interactive() {
[ -n "$NONINTERACTIVE" ] && return 1
{ true </dev/tty; } 2>/dev/null
}
confirm() {
local prompt="$1"
local answer
printf ' \033[34m?\033[0m %s [Y/n] ' "$prompt"
read -r answer </dev/tty
case "$answer" in
[nN]*) return 1 ;;
*) return 0 ;;
esac
}
# ── argument parsing ───────────────────────────────────────────────────────────
parse_args() {
while [ $# -gt 0 ]; do
case "$1" in
--non-interactive) NONINTERACTIVE=1 ; shift ;;
--no-verify) NO_VERIFY=1 ; shift ;;
--core) CORE_ONLY=1 ; shift ;;
--install-dir) INSTALL_DIR="$2" ; USER_OVERRIDE=yes ; shift 2 ;;
--install-dir=*) INSTALL_DIR="${1#*=}" ; USER_OVERRIDE=yes ; shift ;;
*) error "Unknown option: $1" ;;
esac
done
}
# ── platform detection ─────────────────────────────────────────────────────────
detect_platform() {
local os arch
case "$(uname -s)" in
Darwin) os="darwin" ;;
Linux) os="linux" ;;
*) error "Unsupported OS: $(uname -s)" ;;
esac
case "$(uname -m)" in
x86_64) arch="amd64" ;;
arm64 | aarch64) arch="arm64" ;;
*) error "Unsupported architecture: $(uname -m)" ;;
esac
echo "${os}_${arch}"
}
detect_shell_rc() {
local shell_name
shell_name="$(basename "${SHELL:-bash}")"
case "$shell_name" in
zsh) echo "$HOME/.zshrc" ;;
bash) echo "$HOME/.bashrc" ;;
fish) echo "$HOME/.config/fish/config.fish" ;;
*) echo "$HOME/.bashrc" ;; # safe fallback
esac
}
# ── download ───────────────────────────────────────────────────────────────────
latest_version() {
curl -fsSL "https://api.github.com/repos/${REPO}/releases/latest" \
| grep '"tag_name"' \
| sed 's/.*"tag_name": *"\([^"]*\)".*/\1/'
}
sha256_file() {
if command -v sha256sum >/dev/null 2>&1; then
sha256sum "$1" | cut -d' ' -f1
elif command -v shasum >/dev/null 2>&1; then
shasum -a 256 "$1" | cut -d' ' -f1
else
error "No sha256sum or shasum command found — cannot verify download. Pass --no-verify to skip verification."
fi
}
download_and_install() {
local version="$1"
local platform="$2"
local dest="$3"
local ver="${version#v}"
local pkg_name="harness-core_${ver}_${platform}"
local url="https://github.com/${REPO}/releases/download/${version}/${pkg_name}.tar.gz"
local checksum_url="https://github.com/${REPO}/releases/download/${version}/harness_${ver}_checksums.txt"
local tmp
tmp="$(mktemp -d)"
[ -z "$tmp" ] && error "Failed to create temporary directory"
info "Downloading ${pkg_name} $version ($platform)..."
curl -fsSL "$url" -o "$tmp/harness.tar.gz"
if [ -n "$NO_VERIFY" ]; then
warn "Skipping checksum verification (--no-verify)"
else
info "Verifying checksum..."
curl -fsSL "$checksum_url" -o "$tmp/checksums.txt"
local expected actual
expected="$(grep "${pkg_name}.tar.gz" "$tmp/checksums.txt" | cut -d' ' -f1)"
[ -z "$expected" ] && error "Checksum entry not found for ${pkg_name}.tar.gz"
actual="$(sha256_file "$tmp/harness.tar.gz")"
[ "$actual" = "$expected" ] || error "Checksum mismatch — download may be corrupted"
fi
tar -xzf "$tmp/harness.tar.gz" -C "$tmp"
mv "$tmp/harness" "$dest/harness"
chmod +x "$dest/harness"
success "Installed harness $version to $dest/harness"
# A failure here still leaves a working core, so warn instead of aborting.
if [ -z "$CORE_ONLY" ]; then
local module
for module in har migrate; do
info "Installing $module module..."
if "$dest/harness" install module "$module" >/dev/null 2>&1; then
success "Installed $module module to ~/.harness/bin"
else
warn "Could not install the $module module — run 'harness install module $module' to retry"
fi
done
fi
rm -rf "$tmp"
}
# ── shell config ───────────────────────────────────────────────────────────────
shell_config_block() {
local shell_name
shell_name="$(basename "${SHELL:-bash}")"
printf '# <HarnessCLI>\n'
if [ "$shell_name" = "fish" ]; then
printf 'fish_add_path "$HOME/.local/bin"\n'
printf 'harness completion fish | source\n'
else
printf 'export PATH="$HOME/.local/bin:$PATH"\n'
printf 'source <(harness completion %s)\n' "$shell_name"
fi
printf '# </HarnessCLI>\n'
}
patch_shell_rc() {
local rc="$1"
touch "$rc"
printf '\n%s\n\n' "$(shell_config_block)" >> "$rc"
}
already_patched() {
local rc="$1"
grep -q '<HarnessCLI>' "$rc" 2>/dev/null
}
# ── main ───────────────────────────────────────────────────────────────────────
main() {
parse_args "$@"
printf '\n \033[1mHarness CLI installer\033[0m\n\n'
local platform
local version
platform="$(detect_platform)"
version="$(latest_version)"
[ -z "$version" ] && error "Could not determine latest version"
# create install dir if needed
mkdir -p "$INSTALL_DIR"
[ -d "$INSTALL_DIR" ] || error "$INSTALL_DIR is not a directory"
# install binaries
download_and_install "$version" "$platform" "$INSTALL_DIR"
HARNESS_INSTALL_TYPE=script "$INSTALL_DIR/harness" --post-install >/dev/null 2>&1 || true
local patched_rc=""
# shell config — only if interactive and user didn't override install dir
if is_interactive && [ -z "$USER_OVERRIDE" ]; then
local rc
local rc_name
rc="$(detect_shell_rc)"
rc_name="$(basename "$rc")"
if already_patched "$rc"; then
info "Shell config already set up in ~/$rc_name, skipping"
else
printf '\n'
info "Would you like us to update ~/$rc_name?"
info " - Add ~/.local/bin to PATH"
info " - Add shell completions"
printf '\n'
if confirm "Update ~/$rc_name"; then
patch_shell_rc "$rc"
patched_rc="$rc_name"
success "Updated ~/$rc_name"
else
printf '\n'
info "To set up manually, add to ~/$rc_name:"
printf '\n%s\n' "$(shell_config_block)"
fi
fi
fi
printf '\n'
if command -v harness >/dev/null 2>&1; then
success "Done! Run 'harness version' to verify."
elif [ -n "$patched_rc" ]; then
success "Done! Run 'source ~/$patched_rc' then 'harness version' to verify."
else
success "Done! Add $INSTALL_DIR to your PATH then run 'harness version' to verify."
fi
}
main "$@"