forked from dyoshikawa/rulesync
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
328 lines (278 loc) · 8.96 KB
/
install.sh
File metadata and controls
328 lines (278 loc) · 8.96 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
#!/bin/bash
# Rulesync Single Binary Installer
# https://github.com/dyoshikawa/rulesync
#
# Usage:
# curl -fsSL https://github.com/dyoshikawa/rulesync/releases/latest/download/install.sh | bash
#
# Options:
# RULESYNC_HOME: Installation directory (default: ~/.rulesync)
# RULESYNC_VERSION: Version to install (default: latest)
#
# Examples:
# # Install latest version
# curl -fsSL https://github.com/dyoshikawa/rulesync/releases/latest/download/install.sh | bash
#
# # Install specific version
# curl -fsSL https://github.com/dyoshikawa/rulesync/releases/latest/download/install.sh | bash -s -- v6.4.0
#
# # Install to custom directory
# RULESYNC_HOME=~/.local curl -fsSL https://github.com/dyoshikawa/rulesync/releases/latest/download/install.sh | bash
set -euo pipefail
# Colors for output (respect NO_COLOR and non-terminal output)
if [ -t 1 ] && [ "${NO_COLOR:-}" = "" ]; then
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
BLUE='\033[0;34m'
NC='\033[0m'
else
RED=''
GREEN=''
YELLOW=''
BLUE=''
NC=''
fi
# Configuration
GITHUB_REPO="dyoshikawa/rulesync"
INSTALL_DIR="${RULESYNC_HOME:-$HOME/.rulesync}"
BIN_DIR="$INSTALL_DIR/bin"
VERSION="${1:-${RULESYNC_VERSION:-latest}}"
# Detect HTTP client once
HTTP_CLIENT=""
if command -v curl &> /dev/null; then
HTTP_CLIENT="curl"
elif command -v wget &> /dev/null; then
HTTP_CLIENT="wget"
else
printf '%b\n' "${RED}error${NC}: Neither curl nor wget is available. Please install one of them." >&2
exit 1
fi
# Temporary directory for downloads
TMP_DIR=""
# Cleanup function
cleanup() {
if [ -n "$TMP_DIR" ] && [ -d "$TMP_DIR" ]; then
rm -rf "$TMP_DIR"
fi
}
trap cleanup EXIT
# Print functions
info() {
printf '%b\n' "${BLUE}info${NC}: $1"
}
success() {
printf '%b\n' "${GREEN}success${NC}: $1"
}
warn() {
printf '%b\n' "${YELLOW}warn${NC}: $1"
}
error() {
printf '%b\n' "${RED}error${NC}: $1" >&2
exit 1
}
# Validate version format
validate_version() {
local version="$1"
if [ "$version" = "latest" ]; then
return 0
fi
if ! [[ "$version" =~ ^v[0-9]+\.[0-9]+\.[0-9]+(-[a-zA-Z0-9.]+)?$ ]]; then
error "Invalid version format: $version (expected format: v1.2.3 or v1.2.3-beta.1)"
fi
}
# Detect OS
detect_os() {
local os
os="$(uname -s)"
case "$os" in
Linux*) echo "linux" ;;
Darwin*) echo "darwin" ;;
MINGW*|MSYS*|CYGWIN*)
error "Windows is not supported by this installer. Please download the binary manually from https://github.com/$GITHUB_REPO/releases"
;;
*)
error "Unsupported operating system: $os"
;;
esac
}
# Detect architecture
detect_arch() {
local arch
arch="$(uname -m)"
case "$arch" in
x86_64|amd64) echo "x64" ;;
arm64|aarch64) echo "arm64" ;;
*)
error "Unsupported architecture: $arch"
;;
esac
}
# Get the latest release version from GitHub using redirect-based detection
get_latest_version() {
local redirect_url version
if [ "$HTTP_CLIENT" = "curl" ]; then
redirect_url=$(curl -fsSL --max-redirs 5 -o /dev/null -w "%{url_effective}" "https://github.com/$GITHUB_REPO/releases/latest")
else
# Use wget --spider to detect redirect without downloading content
redirect_url=$(wget --max-redirect=5 --spider --server-response "https://github.com/$GITHUB_REPO/releases/latest" 2>&1 | grep -i "Location:" | tail -1 | awk '{print $2}' | tr -d '\r')
fi
version="${redirect_url##*/}"
if [ -z "$version" ]; then
error "Failed to get latest version from GitHub. This may be caused by GitHub API rate limiting or network issues."
fi
echo "$version"
}
# Download a file
download() {
local url="$1"
local output="$2"
if [ "$HTTP_CLIENT" = "curl" ]; then
curl -fsSL --max-redirs 5 "$url" -o "$output"
else
wget --max-redirect=5 -q "$url" -O "$output"
fi
}
# Verify SHA256 checksum
verify_checksum() {
local file="$1"
local expected="$2"
local actual
if command -v sha256sum &> /dev/null; then
actual=$(sha256sum "$file" | cut -d' ' -f1)
elif command -v shasum &> /dev/null; then
actual=$(shasum -a 256 "$file" | cut -d' ' -f1)
else
error "Neither sha256sum nor shasum is available. Cannot verify download integrity."
fi
if [ "$actual" != "$expected" ]; then
error "Checksum verification failed!\nExpected: $expected\nActual: $actual"
fi
success "Checksum verified"
}
# Get shell configuration file
# Note: $SHELL refers to the user's login shell, not the shell running this script.
# This is intentional since we want to show PATH instructions for the user's default shell.
get_shell_config() {
local shell_name
shell_name="$(basename "${SHELL:-/bin/sh}")"
case "$shell_name" in
bash)
if [ -f "$HOME/.bashrc" ]; then
echo "$HOME/.bashrc"
elif [ -f "$HOME/.bash_profile" ]; then
echo "$HOME/.bash_profile"
else
echo "$HOME/.profile"
fi
;;
zsh)
echo "$HOME/.zshrc"
;;
fish)
echo "$HOME/.config/fish/config.fish"
;;
*)
echo "$HOME/.profile"
;;
esac
}
# Print PATH setup instructions
print_path_instructions() {
local shell_name
local shell_config
shell_name="$(basename "${SHELL:-/bin/sh}")"
shell_config="$(get_shell_config)"
echo ""
info "Add rulesync to your PATH by running:"
echo ""
case "$shell_name" in
fish)
printf '%b\n' " ${YELLOW}set -Ux fish_user_paths $BIN_DIR \$fish_user_paths${NC}"
echo ""
echo " Or add to $shell_config:"
printf '%b\n' " ${YELLOW}set -gx PATH $BIN_DIR \$PATH${NC}"
;;
*)
printf '%b\n' " ${YELLOW}echo 'export PATH=\"$BIN_DIR:\$PATH\"' >> $shell_config${NC}"
printf '%b\n' " ${YELLOW}source $shell_config${NC}"
;;
esac
echo ""
info "Then verify the installation:"
printf '%b\n' " ${YELLOW}rulesync --version${NC}"
}
# Print uninstall instructions
print_uninstall_instructions() {
echo ""
info "To uninstall rulesync:"
printf '%b\n' " ${YELLOW}rm -f $BIN_DIR/rulesync${NC}"
echo ""
echo " If you no longer need the installation directory:"
printf '%b\n' " ${YELLOW}rm -rf $INSTALL_DIR${NC}"
echo ""
echo " And remove the PATH entry from your shell configuration file."
}
# Main installation
main() {
echo ""
printf '%b\n' "${BLUE}╔════════════════════════════════════════╗${NC}"
printf '%b\n' "${BLUE}║ Rulesync Binary Installer ║${NC}"
printf '%b\n' "${BLUE}╚════════════════════════════════════════╝${NC}"
echo ""
# Validate version format
validate_version "$VERSION"
# Detect platform
local os arch binary_name
os="$(detect_os)"
arch="$(detect_arch)"
binary_name="rulesync-$os-$arch"
info "Detected platform: $os-$arch"
# Resolve version
local version="$VERSION"
if [ "$version" = "latest" ]; then
info "Fetching latest version..."
version="$(get_latest_version)"
validate_version "$version"
fi
info "Installing rulesync $version"
# Create temporary directory
TMP_DIR="$(mktemp -d)"
# Download URLs
local base_url="https://github.com/$GITHUB_REPO/releases/download/$version"
local binary_url="$base_url/$binary_name"
local checksums_url="$base_url/SHA256SUMS"
# Download binary and checksums
info "Downloading rulesync binary..."
download "$binary_url" "$TMP_DIR/$binary_name"
info "Downloading checksums..."
download "$checksums_url" "$TMP_DIR/SHA256SUMS"
# Verify checksum
info "Verifying checksum..."
local expected_checksum
expected_checksum=$(awk -v name="$binary_name" '$2 == name || $2 == "./"name || $2 == "*"name {print $1}' "$TMP_DIR/SHA256SUMS")
if [ -z "$expected_checksum" ]; then
error "Could not find checksum for $binary_name in SHA256SUMS"
fi
verify_checksum "$TMP_DIR/$binary_name" "$expected_checksum"
# Create installation directory
info "Installing to $BIN_DIR..."
mkdir -p "$BIN_DIR"
# Install binary
mv "$TMP_DIR/$binary_name" "$BIN_DIR/rulesync"
chmod +x "$BIN_DIR/rulesync"
echo ""
success "rulesync $version has been installed to $BIN_DIR/rulesync"
# Check if already in PATH
if echo "$PATH" | tr ':' '\n' | grep -qxF -- "$BIN_DIR"; then
echo ""
info "rulesync is already in your PATH"
echo ""
info "Verify the installation:"
printf '%b\n' " ${YELLOW}rulesync --version${NC}"
else
print_path_instructions
fi
print_uninstall_instructions
}
main