This repository was archived by the owner on Jul 2, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinstall.sh
More file actions
344 lines (285 loc) · 8.4 KB
/
Copy pathinstall.sh
File metadata and controls
344 lines (285 loc) · 8.4 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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#!/bin/sh
# Flipside CLI Installer
# Usage: curl -fsSL https://raw.githubusercontent.com/FlipsideCrypto/flipside-tools/main/install.sh | sh
# Or with specific version: FLIPSIDE_VERSION=1.0.0 curl -fsSL ... | sh
set -e
# Configuration
REPO="FlipsideCrypto/flipside-tools"
BINARY_NAME="flipside"
GITHUB_API="https://api.github.com/repos/${REPO}/releases/latest"
GITHUB_DOWNLOAD="https://github.com/${REPO}/releases/download"
# Colors (only if terminal supports them)
if [ -t 1 ]; then
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
else
RED=''
GREEN=''
YELLOW=''
BLUE=''
NC=''
fi
info() {
printf "${BLUE}==>${NC} %s\n" "$1"
}
success() {
printf "${GREEN}==>${NC} %s\n" "$1"
}
warn() {
printf "${YELLOW}Warning:${NC} %s\n" "$1"
}
error() {
printf "${RED}Error:${NC} %s\n" "$1" >&2
exit 1
}
# Detect operating system
detect_os() {
OS=$(uname -s | tr '[:upper:]' '[:lower:]')
case "$OS" in
darwin)
echo "darwin"
;;
linux)
echo "linux"
;;
mingw*|msys*|cygwin*)
error "Windows is not supported by this installer. Please download the binary manually from GitHub releases."
;;
*)
error "Unsupported operating system: $OS"
;;
esac
}
# Detect architecture
detect_arch() {
ARCH=$(uname -m)
case "$ARCH" in
x86_64|amd64)
echo "amd64"
;;
arm64|aarch64)
echo "arm64"
;;
*)
error "Unsupported architecture: $ARCH"
;;
esac
}
# Check for required commands
check_dependencies() {
for cmd in curl tar; do
if ! command -v "$cmd" >/dev/null 2>&1; then
error "Required command not found: $cmd"
fi
done
}
# Get the latest version from GitHub API
get_latest_version() {
info "Fetching latest version..." >&2
LATEST=$(curl -fsSL "$GITHUB_API" 2>/dev/null | grep '"tag_name"' | head -1 | sed -E 's/.*"v([^"]+)".*/\1/')
if [ -z "$LATEST" ]; then
error "Failed to fetch latest version from GitHub. Please check your internet connection or try again later."
fi
echo "$LATEST"
}
# Download and verify checksum
download_and_verify() {
VERSION="$1"
OS="$2"
ARCH="$3"
TMPDIR="$4"
ARCHIVE_NAME="${BINARY_NAME}_${VERSION}_${OS}_${ARCH}.tar.gz"
DOWNLOAD_URL="${GITHUB_DOWNLOAD}/v${VERSION}/${ARCHIVE_NAME}"
CHECKSUM_URL="${GITHUB_DOWNLOAD}/v${VERSION}/checksums.txt"
info "Downloading ${BINARY_NAME} v${VERSION} for ${OS}/${ARCH}..."
# Download the archive
if ! curl -fsSL -o "${TMPDIR}/${ARCHIVE_NAME}" "$DOWNLOAD_URL"; then
error "Failed to download ${ARCHIVE_NAME}. URL: ${DOWNLOAD_URL}"
fi
# Download checksums
info "Verifying checksum..."
if ! curl -fsSL -o "${TMPDIR}/checksums.txt" "$CHECKSUM_URL"; then
warn "Could not download checksums file. Skipping verification."
return 0
fi
# Verify checksum
EXPECTED_CHECKSUM=$(grep "${ARCHIVE_NAME}" "${TMPDIR}/checksums.txt" | awk '{print $1}')
if [ -z "$EXPECTED_CHECKSUM" ]; then
warn "Checksum not found for ${ARCHIVE_NAME}. Skipping verification."
return 0
fi
# Calculate checksum (macOS uses shasum, Linux uses sha256sum)
if command -v sha256sum >/dev/null 2>&1; then
ACTUAL_CHECKSUM=$(sha256sum "${TMPDIR}/${ARCHIVE_NAME}" | awk '{print $1}')
elif command -v shasum >/dev/null 2>&1; then
ACTUAL_CHECKSUM=$(shasum -a 256 "${TMPDIR}/${ARCHIVE_NAME}" | awk '{print $1}')
else
warn "Neither sha256sum nor shasum found. Skipping checksum verification."
return 0
fi
if [ "$EXPECTED_CHECKSUM" != "$ACTUAL_CHECKSUM" ]; then
error "Checksum verification failed!\nExpected: ${EXPECTED_CHECKSUM}\nActual: ${ACTUAL_CHECKSUM}\nThe download may be corrupted. Please try again."
fi
success "Checksum verified"
}
# Extract the binary
extract_binary() {
TMPDIR="$1"
VERSION="$2"
OS="$3"
ARCH="$4"
ARCHIVE_NAME="${BINARY_NAME}_${VERSION}_${OS}_${ARCH}.tar.gz"
info "Extracting..."
tar -xzf "${TMPDIR}/${ARCHIVE_NAME}" -C "${TMPDIR}"
if [ ! -f "${TMPDIR}/${BINARY_NAME}" ]; then
error "Binary not found after extraction"
fi
chmod +x "${TMPDIR}/${BINARY_NAME}"
}
# Determine installation directory
determine_install_dir() {
# Check if user specified a directory
if [ -n "$INSTALL_DIR" ]; then
echo "$INSTALL_DIR"
return
fi
# Try /usr/local/bin first (requires root usually)
if [ -w "/usr/local/bin" ]; then
echo "/usr/local/bin"
return
fi
# Check if running as root
if [ "$(id -u)" = "0" ]; then
echo "/usr/local/bin"
return
fi
# Fall back to ~/.local/bin
LOCAL_BIN="${HOME}/.local/bin"
if [ ! -d "$LOCAL_BIN" ]; then
mkdir -p "$LOCAL_BIN"
fi
echo "$LOCAL_BIN"
}
# Install the binary
install_binary() {
TMPDIR="$1"
INSTALL_DIR="$2"
TARGET="${INSTALL_DIR}/${BINARY_NAME}"
# Check if directory exists
if [ ! -d "$INSTALL_DIR" ]; then
if ! mkdir -p "$INSTALL_DIR" 2>/dev/null; then
error "Cannot create directory: ${INSTALL_DIR}\nTry running with sudo or set INSTALL_DIR to a writable location."
fi
fi
# Check if we can write to the directory
if [ ! -w "$INSTALL_DIR" ]; then
error "Cannot write to ${INSTALL_DIR}\nTry running with sudo: curl -fsSL ... | sudo sh"
fi
# Move binary to target
if ! mv "${TMPDIR}/${BINARY_NAME}" "$TARGET" 2>/dev/null; then
error "Failed to install binary to ${TARGET}"
fi
success "Installed to ${TARGET}"
}
# Check if install directory is in PATH
check_path() {
INSTALL_DIR="$1"
case ":$PATH:" in
*":${INSTALL_DIR}:"*)
# Already in PATH
return 0
;;
esac
# Not in PATH, show instructions
echo ""
warn "${INSTALL_DIR} is not in your PATH"
echo ""
echo "Add it to your shell configuration:"
echo ""
SHELL_NAME=$(basename "$SHELL")
case "$SHELL_NAME" in
bash)
echo " echo 'export PATH=\"\$PATH:${INSTALL_DIR}\"' >> ~/.bashrc"
echo " source ~/.bashrc"
;;
zsh)
echo " echo 'export PATH=\"\$PATH:${INSTALL_DIR}\"' >> ~/.zshrc"
echo " source ~/.zshrc"
;;
fish)
echo " fish_add_path ${INSTALL_DIR}"
;;
*)
echo " export PATH=\"\$PATH:${INSTALL_DIR}\""
;;
esac
echo ""
}
# Verify installation
verify_installation() {
INSTALL_DIR="$1"
TARGET="${INSTALL_DIR}/${BINARY_NAME}"
if [ ! -x "$TARGET" ]; then
error "Installation verification failed: ${TARGET} is not executable"
fi
# Try to get version
INSTALLED_VERSION=$("$TARGET" --version 2>/dev/null | head -1) || true
if [ -n "$INSTALLED_VERSION" ]; then
success "Verified: ${INSTALLED_VERSION}"
fi
}
# Cleanup
cleanup() {
if [ -n "$TMPDIR" ] && [ -d "$TMPDIR" ]; then
rm -rf "$TMPDIR"
fi
}
# Main installation function
main() {
echo ""
echo "Flipside CLI Installer"
echo "======================"
echo ""
# Check dependencies
check_dependencies
# Detect platform
OS=$(detect_os)
ARCH=$(detect_arch)
info "Detected platform: ${OS}/${ARCH}"
# Get version (from env or latest)
if [ -n "$FLIPSIDE_VERSION" ]; then
VERSION="$FLIPSIDE_VERSION"
info "Using specified version: v${VERSION}"
else
VERSION=$(get_latest_version)
info "Latest version: v${VERSION}"
fi
# Create temp directory
TMPDIR=$(mktemp -d)
trap cleanup EXIT
# Download and verify
download_and_verify "$VERSION" "$OS" "$ARCH" "$TMPDIR"
# Extract
extract_binary "$TMPDIR" "$VERSION" "$OS" "$ARCH"
# Determine install location
INSTALL_DIR=$(determine_install_dir)
info "Installing to: ${INSTALL_DIR}"
# Install
install_binary "$TMPDIR" "$INSTALL_DIR"
# Check PATH
check_path "$INSTALL_DIR"
# Verify
verify_installation "$INSTALL_DIR"
echo ""
success "Installation complete!"
echo ""
echo "Get started:"
echo " ${BINARY_NAME} config init # Configure your API key"
echo " ${BINARY_NAME} --help # Show available commands"
echo ""
}
# Run main
main