-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·210 lines (171 loc) · 5.67 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·210 lines (171 loc) · 5.67 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
#!/usr/bin/env sh
# agentwise installer
# Usage: curl -sSf https://raw.githubusercontent.com/brandonwise/agentwise/main/install.sh | sh
set -eu
REPO="brandonwise/agentwise"
BINARY="agentwise"
GITHUB_API="https://api.github.com/repos/${REPO}"
GITHUB_RELEASE_BASE="https://github.com/${REPO}/releases/download"
say() {
printf '%s\n' "$*"
}
err() {
printf 'agentwise install error: %s\n' "$*" >&2
}
die() {
err "$1"
exit 1
}
need_cmd() {
command -v "$1" >/dev/null 2>&1 || die "Missing required command: $1"
}
sha256_file() {
file="$1"
if command -v shasum >/dev/null 2>&1; then
shasum -a 256 "$file" | awk '{print $1}'
return
fi
if command -v sha256sum >/dev/null 2>&1; then
sha256sum "$file" | awk '{print $1}'
return
fi
if command -v openssl >/dev/null 2>&1; then
openssl dgst -sha256 "$file" | awk '{print $NF}'
return
fi
die "No SHA256 tool found (need shasum, sha256sum, or openssl)."
}
detect_target() {
os_raw=$(uname -s 2>/dev/null | tr '[:upper:]' '[:lower:]')
arch_raw=$(uname -m 2>/dev/null)
case "$os_raw" in
linux)
os="linux"
;;
darwin)
os="macos"
;;
*)
die "Unsupported OS '${os_raw}'. This installer supports Linux and macOS only."
;;
esac
case "$arch_raw" in
x86_64|amd64)
arch="amd64"
;;
aarch64|arm64)
arch="arm64"
;;
*)
die "Unsupported architecture '${arch_raw}'. Supported architectures: amd64, arm64."
;;
esac
TARGET="${os}-${arch}"
}
resolve_version() {
if [ -n "${AGENTWISE_VERSION:-}" ]; then
case "$AGENTWISE_VERSION" in
v*)
RELEASE_TAG="$AGENTWISE_VERSION"
VERSION="${AGENTWISE_VERSION#v}"
;;
*)
RELEASE_TAG="v${AGENTWISE_VERSION}"
VERSION="$AGENTWISE_VERSION"
;;
esac
return
fi
release_json=$(curl -fsSL "${GITHUB_API}/releases/latest") || die "Failed to fetch latest release metadata from GitHub."
RELEASE_TAG=$(printf '%s' "$release_json" | sed -n 's/.*"tag_name"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p' | head -n 1)
[ -n "$RELEASE_TAG" ] || die "Could not determine the latest release tag."
VERSION="${RELEASE_TAG#v}"
}
choose_install_dir() {
INSTALL_DIR_CHOSEN="${INSTALL_DIR:-}"
USE_SUDO=0
if [ -z "$INSTALL_DIR_CHOSEN" ]; then
if [ -w "/usr/local/bin" ]; then
INSTALL_DIR_CHOSEN="/usr/local/bin"
elif [ -d "/usr/local/bin" ] && command -v sudo >/dev/null 2>&1; then
INSTALL_DIR_CHOSEN="/usr/local/bin"
USE_SUDO=1
else
INSTALL_DIR_CHOSEN="${HOME}/.local/bin"
fi
fi
if [ "$INSTALL_DIR_CHOSEN" != "/usr/local/bin" ] && [ -d "$INSTALL_DIR_CHOSEN" ] && [ ! -w "$INSTALL_DIR_CHOSEN" ] && command -v sudo >/dev/null 2>&1; then
USE_SUDO=1
fi
}
prepare_install_dir() {
if [ "$USE_SUDO" -eq 1 ]; then
sudo mkdir -p "$INSTALL_DIR_CHOSEN" || die "Failed to create install directory '${INSTALL_DIR_CHOSEN}'."
else
mkdir -p "$INSTALL_DIR_CHOSEN" || die "Failed to create install directory '${INSTALL_DIR_CHOSEN}'."
fi
}
install_binary() {
src="$1"
dest="$2"
if [ "$USE_SUDO" -eq 1 ]; then
if command -v install >/dev/null 2>&1; then
sudo install -m 0755 "$src" "$dest" || die "Failed to install binary to '${dest}'."
else
sudo cp "$src" "$dest" || die "Failed to copy binary to '${dest}'."
sudo chmod 0755 "$dest" || die "Failed to set executable permissions on '${dest}'."
fi
else
if command -v install >/dev/null 2>&1; then
install -m 0755 "$src" "$dest" || die "Failed to install binary to '${dest}'."
else
cp "$src" "$dest" || die "Failed to copy binary to '${dest}'."
chmod 0755 "$dest" || die "Failed to set executable permissions on '${dest}'."
fi
fi
}
main() {
need_cmd curl
need_cmd tar
need_cmd mktemp
detect_target
resolve_version
choose_install_dir
ARCHIVE="${BINARY}-${VERSION}-${TARGET}.tar.gz"
CHECKSUMS_FILE="agentwise-${VERSION}-checksums.txt"
ARCHIVE_URL="${GITHUB_RELEASE_BASE}/${RELEASE_TAG}/${ARCHIVE}"
CHECKSUMS_URL="${GITHUB_RELEASE_BASE}/${RELEASE_TAG}/${CHECKSUMS_FILE}"
TMP_DIR=$(mktemp -d 2>/dev/null || mktemp -d -t agentwise-install)
trap 'rm -rf "$TMP_DIR"' EXIT INT TERM
say "Installing agentwise ${VERSION} (${TARGET})..."
say "Downloading ${ARCHIVE}"
curl -fsSL "$ARCHIVE_URL" -o "$TMP_DIR/$ARCHIVE" || die "Failed to download release archive from ${ARCHIVE_URL}."
say "Downloading checksums"
curl -fsSL "$CHECKSUMS_URL" -o "$TMP_DIR/$CHECKSUMS_FILE" || die "Failed to download checksums from ${CHECKSUMS_URL}."
expected_sha=$(grep "[[:space:]]${ARCHIVE}$" "$TMP_DIR/$CHECKSUMS_FILE" | awk '{print $1}' | head -n 1 || true)
[ -n "$expected_sha" ] || die "Could not find checksum for ${ARCHIVE} in ${CHECKSUMS_FILE}."
actual_sha=$(sha256_file "$TMP_DIR/$ARCHIVE")
[ "$actual_sha" = "$expected_sha" ] || die "Checksum verification failed for ${ARCHIVE}."
say "Checksum verified"
tar -xzf "$TMP_DIR/$ARCHIVE" -C "$TMP_DIR" || die "Failed to extract ${ARCHIVE}."
BINARY_PATH="$TMP_DIR/$BINARY"
if [ ! -f "$BINARY_PATH" ]; then
BINARY_PATH=$(find "$TMP_DIR" -maxdepth 2 -type f -name "$BINARY" | head -n 1 || true)
fi
[ -n "${BINARY_PATH:-}" ] && [ -f "$BINARY_PATH" ] || die "Could not locate '${BINARY}' in extracted archive."
prepare_install_dir
install_binary "$BINARY_PATH" "$INSTALL_DIR_CHOSEN/$BINARY"
say ""
say "✅ Installed to $INSTALL_DIR_CHOSEN/$BINARY"
case ":$PATH:" in
*":$INSTALL_DIR_CHOSEN:"*) ;;
*)
if [ "$INSTALL_DIR_CHOSEN" = "${HOME}/.local/bin" ]; then
say "Add ~/.local/bin to your PATH if needed:"
say " export PATH=\"$HOME/.local/bin:$PATH\""
fi
;;
esac
say "Run: agentwise --version"
}
main "$@"