Repository navigation
Expand file tree
/
Copy pathinstall
More file actions
executable file
·294 lines (264 loc) · 8.35 KB
/
Copy pathinstall
File metadata and controls
executable file
·294 lines (264 loc) · 8.35 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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#!/bin/bash
set -e
GITHUB_REPO="theinterfold/interfold"
BINARY_NAME="interfold"
INSTALL_DIR="${INTERFOLD_INSTALL_DIR:-$HOME/.local/bin}"
# Release tag to install. Empty selects the latest release.
REQUESTED_VERSION="${INTERFOLD_VERSION:-}"
log_info() {
echo "$1" >&2
}
log_success() {
echo "$1" >&2
}
log_warning() {
echo "$1" >&2
}
log_error() {
echo "$1" >&2
}
detect_platform() {
local os=""
local arch=""
case "$(uname -s)" in
Linux*)
os="linux"
case "$(uname -m)" in
x86_64)
arch="x86_64"
;;
arm64|aarch64)
arch="aarch64"
;;
*)
log_error "Unsupported Linux architecture: $(uname -m)"
exit 1
;;
esac
;;
Darwin*)
os="macos"
case "$(uname -m)" in
x86_64)
# A shell that runs through Rosetta 2 on Apple silicon reports x86_64. An Intel
# Mac cannot run the Apple silicon binary.
if [ "$(sysctl -in hw.optional.arm64 2>/dev/null)" = "1" ]; then
arch="aarch64"
else
log_error "No release archive exists for macOS on Intel. Build the CLI from source."
exit 1
fi
;;
arm64|aarch64)
arch="aarch64"
;;
*)
log_error "Unsupported macOS architecture: $(uname -m)"
exit 1
;;
esac
;;
*)
log_error "Unsupported operating system: $(uname -s)"
exit 1
;;
esac
echo "${os}-${arch}"
}
command_exists() {
command -v "$1" >/dev/null 2>&1
}
download_file() {
local url="$1"
local output="$2"
if command_exists curl; then
curl -fsSL "$url" -o "$output"
elif command_exists wget; then
wget -q "$url" -O "$output"
else
log_error "Neither curl nor wget is available. Please install one of them."
exit 1
fi
}
release_api_url() {
if [ -n "$REQUESTED_VERSION" ]; then
local tag="$REQUESTED_VERSION"
case "$tag" in
v*) ;;
*) tag="v${tag}" ;;
esac
echo "https://api.github.com/repos/${GITHUB_REPO}/releases/tags/${tag}"
else
echo "https://api.github.com/repos/${GITHUB_REPO}/releases/latest"
fi
}
get_latest_release() {
local api_url
api_url=$(release_api_url)
local temp_file=$(mktemp)
log_info "Fetching release information..."
if ! download_file "$api_url" "$temp_file"; then
log_error "Failed to fetch release information from GitHub API"
rm -f "$temp_file"
exit 1
fi
local tag_name=$(grep -o '"tag_name"[[:space:]]*:[[:space:]]*"[^"]*"' "$temp_file" | cut -d'"' -f4)
if [ -z "$tag_name" ]; then
log_error "Failed to parse release information"
rm -f "$temp_file"
exit 1
fi
rm -f "$temp_file"
echo "$tag_name"
}
get_download_url() {
local platform="$1"
local api_url
api_url=$(release_api_url)
local temp_file=$(mktemp)
if ! download_file "$api_url" "$temp_file"; then
log_error "Failed to fetch release information"
rm -f "$temp_file"
exit 1
fi
local asset_pattern="${BINARY_NAME}-${platform}"
local download_url=$(grep -o '"browser_download_url"[[:space:]]*:[[:space:]]*"[^"]*'"$asset_pattern"'[^"]*"' "$temp_file" | cut -d'"' -f4)
if [ -z "$download_url" ]; then
log_error "No compatible binary found for platform: $platform"
log_info "Available assets:"
grep -o '"name"[[:space:]]*:[[:space:]]*"[^"]*"' "$temp_file" | cut -d'"' -f4 | sed 's/^/ - /'
rm -f "$temp_file"
exit 1
fi
rm -f "$temp_file"
echo "$download_url"
}
usage() {
cat <<EOF
Usage: install [--version TAG] [--dir DIR]
Options:
--version TAG Release tag of the interfold CLI to install, for example
v0.19.0. Default: the latest release. Also settable with
the INTERFOLD_VERSION environment variable.
--dir DIR Folder to install the binary in. Default: ~/.local/bin.
Also settable with the INTERFOLD_INSTALL_DIR environment
variable.
-h, --help Show this message.
EOF
}
parse_args() {
while [ $# -gt 0 ]; do
case "$1" in
--version)
if [ -z "${2:-}" ]; then
log_error "--version needs a release tag, for example v0.13.0"
exit 1
fi
REQUESTED_VERSION="$2"
shift 2
;;
--version=*)
REQUESTED_VERSION="${1#--version=}"
if [ -z "$REQUESTED_VERSION" ]; then
log_error "--version needs a release tag, for example v0.13.0"
exit 1
fi
shift
;;
--dir)
if [ -z "${2:-}" ]; then
log_error "--dir needs a folder"
exit 1
fi
INSTALL_DIR="$2"
shift 2
;;
--dir=*)
INSTALL_DIR="${1#--dir=}"
if [ -z "$INSTALL_DIR" ]; then
log_error "--dir needs a folder"
exit 1
fi
shift
;;
-h|--help)
usage
exit 0
;;
*)
log_error "Unknown option: $1"
usage >&2
exit 1
;;
esac
done
}
main() {
log_info "Interfold CLI Installer"
parse_args "$@"
local platform
platform=$(detect_platform)
log_info "Detected platform: $platform"
local version
version=$(get_latest_release)
log_info "Version: $version"
local download_url
download_url=$(get_download_url "$platform")
log_info "Download URL: $download_url"
log_info "Creating install directory: $INSTALL_DIR"
mkdir -p "$INSTALL_DIR"
# The temporary folder is in the install folder, so that the final move is a rename.
local temp_dir
temp_dir=$(mktemp -d "$INSTALL_DIR/.interfold-install.XXXXXX")
local asset_name=$(basename "$download_url")
log_info "Downloading $asset_name..."
if ! download_file "$download_url" "$temp_dir/$asset_name"; then
log_error "Failed to download $asset_name"
rm -rf "$temp_dir"
exit 1
fi
local target_path="$INSTALL_DIR/$BINARY_NAME"
log_info "Installing to $target_path..."
# Extract next to the archive, then move the binary into place. The rename replaces the file
# instead of writing into it, so a running node keeps the binary that it started from.
if ! tar -xzf "$temp_dir/$asset_name" -C "$temp_dir" "$BINARY_NAME" || [ ! -f "$temp_dir/$BINARY_NAME" ]; then
log_error "Failed to extract $BINARY_NAME from $asset_name"
rm -rf "$temp_dir"
exit 1
fi
chmod 755 "$temp_dir/$BINARY_NAME"
if ! mv -f "$temp_dir/$BINARY_NAME" "$target_path"; then
log_error "Failed to write $target_path"
rm -rf "$temp_dir"
exit 1
fi
rm -rf "$temp_dir"
log_success "Successfully installed $BINARY_NAME to $target_path"
if [[ ":$PATH:" != *":$INSTALL_DIR:"* ]]; then
log_warning "$INSTALL_DIR is not in your PATH"
echo ""
echo "To add it to your PATH, run one of the following:"
echo ""
echo " # For bash users:"
echo " echo 'export PATH=\"$INSTALL_DIR:\$PATH\"' >> ~/.bashrc"
echo " source ~/.bashrc"
echo ""
echo " # For zsh users:"
echo " echo 'export PATH=\"$INSTALL_DIR:\$PATH\"' >> ~/.zshrc"
echo " source ~/.zshrc"
echo ""
echo " # For fish users:"
echo " fish_add_path $INSTALL_DIR"
echo ""
fi
echo ""
log_success "Installation complete!"
echo ""
echo "Check the installed version:"
echo ""
echo " $BINARY_NAME --version"
echo ""
echo "To install another release, run this script again with --version <TAG>."
echo ""
}
main "$@"