-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·317 lines (266 loc) · 8.25 KB
/
install.sh
File metadata and controls
executable file
·317 lines (266 loc) · 8.25 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
#!/usr/bin/env bash
# Socket CLI installation script.
# Downloads and installs the appropriate Socket CLI binary for your platform.
set -euo pipefail
# Colors for output.
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
PURPLE='\033[0;35m'
BOLD='\033[1m'
NC='\033[0m' # No Color
# Print colored messages.
info() {
echo -e "${BLUE}ℹ${NC} $1"
}
success() {
echo -e "${GREEN}✓${NC} $1"
}
error() {
echo -e "${RED}✗${NC} $1"
}
warning() {
echo -e "${YELLOW}⚠${NC} $1"
}
step() {
echo -e "${CYAN}→${NC} $1"
}
socket_brand() {
echo -e "${PURPLE}⚡${NC} $1"
}
# Detect if running on musl libc (Alpine Linux, etc.).
detect_musl() {
# Check for Alpine in /etc/os-release.
if [ -f /etc/os-release ]; then
if grep -qi 'alpine' /etc/os-release 2>/dev/null; then
return 0
fi
fi
# Check for musl dynamic linker.
if [ -f /lib/ld-musl-x86_64.so.1 ] || [ -f /lib/ld-musl-aarch64.so.1 ]; then
return 0
fi
# Check ldd output for musl.
if command -v ldd &> /dev/null; then
if ldd --version 2>&1 | grep -qi musl; then
return 0
fi
fi
return 1
}
# Detect platform and architecture.
detect_platform() {
local os
local arch
local libc_suffix=""
# Detect OS.
case "$(uname -s)" in
Linux*)
os="linux"
# Check for musl libc on Linux.
if detect_musl; then
libc_suffix="-musl"
fi
;;
Darwin*)
os="darwin"
;;
MINGW*|MSYS*|CYGWIN*)
os="win32"
;;
*)
error "Unsupported operating system: $(uname -s)"
echo ""
info "Socket CLI supports Linux, macOS, and Windows."
info "If you think this is an error, please open an issue at:"
info "https://github.com/SocketDev/socket-cli/issues"
exit 1
;;
esac
# Detect architecture.
case "$(uname -m)" in
x86_64|amd64)
arch="x64"
;;
aarch64|arm64)
arch="arm64"
;;
*)
error "Unsupported architecture: $(uname -m)"
echo ""
info "Socket CLI supports x64 and arm64 architectures."
info "If you think this is an error, please open an issue at:"
info "https://github.com/SocketDev/socket-cli/issues"
exit 1
;;
esac
echo "${os}-${arch}${libc_suffix}"
}
# Get the latest version from npm registry.
get_latest_version() {
local package_name="$1"
local version
# Try using curl with npm registry API.
if command -v curl &> /dev/null; then
version=$(curl -fsSL "https://registry.npmjs.org/${package_name}/latest" | grep -o '"version": *"[^"]*"' | head -1 | sed 's/"version": *"\([^"]*\)"/\1/')
# Fallback to wget.
elif command -v wget &> /dev/null; then
version=$(wget -qO- "https://registry.npmjs.org/${package_name}/latest" | grep -o '"version": *"[^"]*"' | head -1 | sed 's/"version": *"\([^"]*\)"/\1/')
else
error "Neither curl nor wget found on your system"
echo ""
info "Please install curl or wget to continue:"
info " macOS: brew install curl"
info " Ubuntu: sudo apt-get install curl"
info " Fedora: sudo dnf install curl"
exit 1
fi
if [ -z "$version" ]; then
error "Failed to fetch latest version from npm registry"
echo ""
info "This might be a temporary network issue. Please try again."
info "If the problem persists, check your internet connection."
exit 1
fi
echo "$version"
}
# Calculate SHA256 hash of a string.
calculate_hash() {
local str="$1"
if command -v sha256sum &> /dev/null; then
echo -n "$str" | sha256sum | cut -d' ' -f1
elif command -v shasum &> /dev/null; then
echo -n "$str" | shasum -a 256 | cut -d' ' -f1
else
error "Neither sha256sum nor shasum found"
exit 1
fi
}
# Download and install Socket CLI.
install_socket_cli() {
local platform
local version
local package_name
local download_url
local dlx_dir
local package_hash
local install_dir
local binary_path
local bin_dir
local symlink_path
step "Detecting your platform..."
platform=$(detect_platform)
success "Platform detected: ${BOLD}$platform${NC}"
# Construct package name.
package_name="@socketbin/cli-${platform}"
step "Fetching latest version from npm..."
version=$(get_latest_version "$package_name")
success "Found version ${BOLD}$version${NC}"
# Construct download URL from npm registry.
download_url="https://registry.npmjs.org/${package_name}/-/cli-${platform}-${version}.tgz"
socket_brand "Downloading Socket CLI..."
# Create DLX directory structure.
dlx_dir="${HOME}/.socket/_dlx"
mkdir -p "$dlx_dir"
# Calculate content hash for the package.
package_hash=$(calculate_hash "${package_name}@${version}")
install_dir="${dlx_dir}/${package_hash}"
# Create installation directory.
mkdir -p "$install_dir"
# Download tarball to temporary location.
local temp_tarball="${install_dir}/socket.tgz"
if command -v curl &> /dev/null; then
curl -fsSL -o "$temp_tarball" "$download_url"
elif command -v wget &> /dev/null; then
wget -qO "$temp_tarball" "$download_url"
fi
success "Package downloaded successfully"
# Extract tarball.
step "Capturing lightning in a bottle ⚡"
tar -xzf "$temp_tarball" -C "$install_dir"
# Get Socket CLI version from extracted package.
local cli_version
if [ -f "${install_dir}/package/package.json" ]; then
cli_version=$(grep -o '"version": *"[^"]*"' "${install_dir}/package/package.json" | head -1 | sed 's/"version": *"\([^"]*\)"/\1/')
if [ -n "$cli_version" ]; then
success "Socket CLI ${BOLD}v${cli_version}${NC} (build ${version})"
fi
fi
# Find the binary (it's in package/bin/socket or package/bin/socket.exe).
if [ "$platform" = "win32-x64" ] || [ "$platform" = "win32-arm64" ]; then
binary_path="${install_dir}/package/bin/socket.exe"
else
binary_path="${install_dir}/package/bin/socket"
fi
if [ ! -f "$binary_path" ]; then
error "Binary not found at expected path: $binary_path"
echo ""
info "This might be a temporary issue with the package. Try again in a moment."
exit 1
fi
# Make binary executable (Unix-like systems).
if [ "$platform" != "win32-x64" ] && [ "$platform" != "win32-arm64" ]; then
chmod +x "$binary_path"
# Clear macOS quarantine attribute.
if [ "$platform" = "darwin-x64" ] || [ "$platform" = "darwin-arm64" ]; then
xattr -d com.apple.quarantine "$binary_path" 2>/dev/null || true
success "Cleared macOS security restrictions"
fi
fi
# Clean up tarball.
rm "$temp_tarball"
success "Binary ready at ${BOLD}$binary_path${NC}"
# Create symlink in user's local bin directory.
bin_dir="${HOME}/.local/bin"
mkdir -p "$bin_dir"
symlink_path="${bin_dir}/socket"
# Remove existing symlink if present.
if [ -L "$symlink_path" ] || [ -f "$symlink_path" ]; then
step "Replacing existing installation..."
rm "$symlink_path"
fi
# Create symlink.
step "Creating command shortcut..."
ln -s "$binary_path" "$symlink_path"
success "Command ready: ${BOLD}socket${NC}"
echo ""
# Check if ~/.local/bin is in PATH.
if [[ ":$PATH:" != *":${bin_dir}:"* ]]; then
warning "Almost there! One more step needed..."
echo ""
echo " Add ${BOLD}~/.local/bin${NC} to your PATH by adding this line to your shell profile:"
echo " ${BOLD}(~/.bashrc, ~/.zshrc, ~/.bash_profile, or ~/.profile)${NC}"
echo ""
echo " ${CYAN}export PATH=\"\$HOME/.local/bin:\$PATH\"${NC}"
echo ""
echo " Then restart your shell or run: ${CYAN}source ~/.zshrc${NC} (or your shell config)"
echo ""
else
success "Your PATH is already configured perfectly!"
fi
echo ""
if [ -n "$cli_version" ]; then
socket_brand "${BOLD}Socket CLI v${cli_version} installed successfully!${NC}"
else
socket_brand "${BOLD}Socket CLI installed successfully!${NC}"
fi
echo ""
info "Quick start:"
echo -e " ${CYAN}socket --help${NC} Get started with Socket"
echo -e " ${CYAN}socket self-update${NC} Update to the latest version"
echo ""
socket_brand "Happy securing!"
}
# Main execution.
main() {
echo ""
echo -e "${PURPLE}${BOLD}⚡ Socket CLI Installer ⚡${NC}"
echo -e "${BOLD}═══════════════════════════${NC}"
echo ""
echo " Secure your dependencies with Socket Security"
echo ""
install_socket_cli
}
main "$@"