-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·200 lines (157 loc) · 4.58 KB
/
install.sh
File metadata and controls
executable file
·200 lines (157 loc) · 4.58 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
#!/bin/sh
set -eu
REPO="TimBeyer/clawctl"
BINARY_NAME="clawctl"
DEFAULT_INSTALL_DIR="${HOME}/.local/bin"
INSTALL_DIR="${INSTALL_DIR:-$DEFAULT_INSTALL_DIR}"
GITHUB_API="https://api.github.com/repos/${REPO}/releases/latest"
# --- Utilities ---
info() {
printf '[clawctl] %s\n' "$*" >&2
}
error() {
printf '[clawctl] Error: %s\n' "$*" >&2
exit 1
}
command_exists() {
command -v "$1" >/dev/null 2>&1
}
trim() {
printf '%s' "$1" | tr -d '[:space:]'
}
# --- Downloader ---
DOWNLOADER=""
detect_downloader() {
if command_exists curl; then
DOWNLOADER="curl"
elif command_exists wget; then
DOWNLOADER="wget"
else
error "curl or wget is required but neither was found"
fi
}
download() {
url="$1"
output="$2"
case "$DOWNLOADER" in
curl) curl -fsSL -o "$output" "$url" ;;
wget) wget -qO "$output" "$url" ;;
esac
}
download_to_stdout() {
url="$1"
case "$DOWNLOADER" in
curl) curl -fsSL "$url" ;;
wget) wget -qO- "$url" ;;
esac
}
# --- Platform Detection ---
detect_platform() {
os="$(uname -s)"
arch="$(uname -m)"
case "${os}-${arch}" in
Darwin-arm64)
PLATFORM="darwin-arm64"
;;
*)
error "unsupported platform: ${os} ${arch}
clawctl currently supports macOS on Apple Silicon (arm64) only.
See https://github.com/${REPO} for updates."
;;
esac
}
# --- Version ---
get_latest_version() {
release_info="$(download_to_stdout "$GITHUB_API")"
TAG="$(printf '%s' "$release_info" | grep '"tag_name"' | sed 's/.*"tag_name":[[:space:]]*"\([^"]*\)".*/\1/')"
if [ -z "$TAG" ]; then
error "could not determine latest release from GitHub API"
fi
LATEST_VERSION="${TAG#v}"
}
check_installed_version() {
if [ -x "${INSTALL_DIR}/${BINARY_NAME}" ]; then
INSTALLED_VERSION="$(trim "$("${INSTALL_DIR}/${BINARY_NAME}" --version 2>/dev/null || true)")"
if [ -n "$INSTALLED_VERSION" ] && [ "$INSTALLED_VERSION" = "$LATEST_VERSION" ]; then
info "${BINARY_NAME} is already up to date (v${LATEST_VERSION})"
exit 0
fi
if [ -n "$INSTALLED_VERSION" ]; then
info "updating ${BINARY_NAME} from v${INSTALLED_VERSION} to v${LATEST_VERSION}"
fi
fi
}
# --- Install ---
install_binary() {
download_url="https://github.com/${REPO}/releases/download/${TAG}/${BINARY_NAME}-${PLATFORM}.zip"
tmpdir="$(mktemp -d 2>/dev/null || mktemp -d -t clawctl)"
if [ ! -d "$tmpdir" ]; then
error "failed to create temporary directory"
fi
trap 'rm -rf "$tmpdir"' EXIT
info "downloading ${BINARY_NAME} v${LATEST_VERSION} for ${PLATFORM}..."
download "$download_url" "${tmpdir}/${BINARY_NAME}.zip"
if [ ! -s "${tmpdir}/${BINARY_NAME}.zip" ]; then
error "download failed — file is empty"
fi
if ! command_exists unzip; then
error "unzip is required but was not found"
fi
unzip -o -q "${tmpdir}/${BINARY_NAME}.zip" -d "$tmpdir"
if [ ! -f "${tmpdir}/${BINARY_NAME}" ]; then
error "expected binary '${BINARY_NAME}' not found in archive"
fi
mkdir -p "$INSTALL_DIR" 2>/dev/null || true
if ! install -m 755 "${tmpdir}/${BINARY_NAME}" "${INSTALL_DIR}/${BINARY_NAME}" 2>/dev/null; then
error "permission denied writing to ${INSTALL_DIR}
Try: INSTALL_DIR=<writable-dir> curl -fsSL https://raw.githubusercontent.com/${REPO}/main/install.sh | bash"
fi
}
# --- PATH Setup ---
PATH_LINE='export PATH="$HOME/.local/bin:$PATH"'
setup_path() {
# Only auto-configure PATH when using the default install directory
if [ "$INSTALL_DIR" != "$DEFAULT_INSTALL_DIR" ]; then
return
fi
modified=""
for rcfile in "${HOME}/.zshrc" "${HOME}/.bashrc"; do
if [ -f "$rcfile" ]; then
if grep -qF '$HOME/.local/bin' "$rcfile" 2>/dev/null; then
continue
fi
fi
printf '\n%s\n' "$PATH_LINE" >> "$rcfile"
modified="${modified} $(basename "$rcfile")"
done
if [ -n "$modified" ]; then
info "added ~/.local/bin to PATH in:${modified}"
fi
}
# --- Verify ---
verify_install() {
installed_version="$(trim "$("${INSTALL_DIR}/${BINARY_NAME}" --version 2>/dev/null || true)")"
if [ -z "$installed_version" ]; then
error "installation failed — could not run ${BINARY_NAME}"
fi
info "${BINARY_NAME} v${installed_version} installed to ${INSTALL_DIR}/${BINARY_NAME}"
case ":${PATH}:" in
*":${INSTALL_DIR}:"*) ;;
*)
info "restart your shell or run: source ~/.zshrc"
;;
esac
info "run '${BINARY_NAME} create' to get started"
info "enable tab completions: ${BINARY_NAME} completions --help"
}
# --- Main ---
main() {
detect_downloader
detect_platform
get_latest_version
check_installed_version
install_binary
setup_path
verify_install
}
main "$@"