forked from datarobot-oss/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·524 lines (456 loc) · 15.8 KB
/
install.sh
File metadata and controls
executable file
·524 lines (456 loc) · 15.8 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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
#!/bin/sh
# DataRobot CLI installation script for macOS and Linux
#
# Usage:
# Install latest version:
# curl -fsSL https://raw.githubusercontent.com/datarobot-oss/cli/main/install.sh | sh
#
# Install specific version:
# curl -fsSL https://raw.githubusercontent.com/datarobot-oss/cli/main/install.sh | sh -s -- v0.1.0
#
# Custom install directory:
# curl -fsSL https://raw.githubusercontent.com/datarobot-oss/cli/main/install.sh | INSTALL_DIR=/custom/path sh
set -e
# Configuration
REPO="datarobot-oss/cli"
BINARY_NAME="dr"
INSTALL_DIR="${INSTALL_DIR:-$HOME/.local/bin}"
VERSION="${1:-latest}"
# Colors for output
if [ -t 1 ]; then
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
BOLD='\033[1m'
NC='\033[0m'
else
RED=''
GREEN=''
YELLOW=''
BLUE=''
BOLD=''
NC=''
fi
# Helper functions
info() {
printf "${GREEN}==>${NC} ${BOLD}%s${NC}\n" "$1"
}
warn() {
printf "${YELLOW}Warning:${NC} %s\n" "$1"
}
error() {
printf "${RED}Error:${NC} %s\n" "$1" >&2
exit 1
}
step() {
printf "${BLUE} →${NC} %s\n" "$1"
}
# Detect OS and architecture
detect_platform() {
OS=$(uname -s | tr '[:upper:]' '[:lower:]')
ARCH=$(uname -m)
case "$OS" in
linux*)
OS="Linux"
;;
darwin*)
OS="Darwin"
;;
*)
error "Unsupported operating system: $OS"
;;
esac
case "$ARCH" in
x86_64|amd64)
ARCH="x86_64"
;;
arm64|aarch64)
ARCH="arm64"
;;
riscv64)
ARCH="riscv64"
;;
*)
error "Unsupported architecture: $ARCH"
;;
esac
step "Detected platform: $OS $ARCH"
}
# Check for required tools
check_requirements() {
if ! command -v curl >/dev/null 2>&1 && ! command -v wget >/dev/null 2>&1; then
error "Neither curl nor wget is available. Please install one of them."
fi
if ! command -v tar >/dev/null 2>&1; then
error "tar is not available. Please install it."
fi
}
# Get the latest release version or validate specified version
get_version() {
if [ "$VERSION" = "latest" ]; then
step "Fetching latest version..."
# Prepare auth header if GITHUB_TOKEN is available
AUTH_HEADER=""
if [ -n "$GITHUB_TOKEN" ]; then
AUTH_HEADER="Authorization: token $GITHUB_TOKEN"
fi
if command -v curl >/dev/null 2>&1; then
if [ -n "$AUTH_HEADER" ]; then
VERSION=$(curl -fsSL -H "$AUTH_HEADER" "https://api.github.com/repos/$REPO/releases/latest" | grep '"tag_name"' | sed -E 's/.*"([^"]+)".*/\1/')
else
VERSION=$(curl -fsSL "https://api.github.com/repos/$REPO/releases/latest" | grep '"tag_name"' | sed -E 's/.*"([^"]+)".*/\1/')
fi
else
if [ -n "$AUTH_HEADER" ]; then
VERSION=$(wget -qO- --header="$AUTH_HEADER" "https://api.github.com/repos/$REPO/releases/latest" | grep '"tag_name"' | sed -E 's/.*"([^"]+)".*/\1/')
else
VERSION=$(wget -qO- "https://api.github.com/repos/$REPO/releases/latest" | grep '"tag_name"' | sed -E 's/.*"([^"]+)".*/\1/')
fi
fi
if [ -z "$VERSION" ]; then
error "Failed to fetch the latest version from GitHub"
fi
else
step "Using specified version: $VERSION"
fi
# Ensure version starts with 'v'
case "$VERSION" in
v*) ;;
*) VERSION="v$VERSION" ;;
esac
printf " ${BOLD}Version:${NC} %s\n" "$VERSION"
}
# Compare versions (returns 0 if v1 < v2, 1 if v1 >= v2)
compare_versions() {
local v1=$1
local v2=$2
# Remove 'v' prefix and extract version numbers
v1=$(echo "$v1" | sed 's/^v//')
v2=$(echo "$v2" | sed 's/^v//')
# Compare versions
if [ "$v1" = "$v2" ]; then
return 1 # Same version
fi
# Use sort -V for version comparison if available
if printf '%s\n' "$v1" "$v2" | sort -V -C 2>/dev/null; then
return 0 # v1 < v2 (update available)
else
return 1 # v1 >= v2 (no update needed)
fi
}
# Check if binary is already installed
check_existing_installation() {
if [ -x "$INSTALL_DIR/$BINARY_NAME" ]; then
CURRENT_VERSION=$("$INSTALL_DIR/$BINARY_NAME" --version 2>/dev/null | head -n1 || echo "unknown")
# Extract just the version number (e.g., "v1.2.3" from "dr version v1.2.3")
CURRENT_VERSION=$(echo "$CURRENT_VERSION" | grep -oE 'v?[0-9]+\.[0-9]+\.[0-9]+' | head -n1)
if [ -z "$CURRENT_VERSION" ] || [ "$CURRENT_VERSION" = "unknown" ]; then
warn "Unable to determine current version"
step "Proceeding with installation of $VERSION"
return 0
fi
# Normalize versions (ensure both have 'v' prefix)
case "$CURRENT_VERSION" in
v*) ;;
*) CURRENT_VERSION="v$CURRENT_VERSION" ;;
esac
step "Current installation: $CURRENT_VERSION"
step "Target version: $VERSION"
# Check if versions are the same
if [ "$CURRENT_VERSION" = "$VERSION" ]; then
info "DataRobot CLI $VERSION is already installed"
step "Installation location: $INSTALL_DIR/$BINARY_NAME"
if ! echo ":$PATH:" | grep -q ":$INSTALL_DIR:"; then
warn "$INSTALL_DIR is not in your PATH"
show_path_instructions
fi
echo ""
info "Already up to date!"
exit 0
fi
# Check if update is available
if compare_versions "$CURRENT_VERSION" "$VERSION"; then
info "Update available: $CURRENT_VERSION → $VERSION"
# Ask user if they want to update (only in interactive mode)
if [ -t 0 ]; then
echo ""
printf "${BOLD}Would you like to upgrade to $VERSION? [Y/n]${NC} "
read -r response
case "$response" in
[nN][oO]|[nN])
info "Installation cancelled"
exit 0
;;
*)
echo ""
info "Upgrading DataRobot CLI..."
;;
esac
else
step "Upgrading to: $VERSION"
fi
else
warn "Target version ($VERSION) is older than current version ($CURRENT_VERSION)"
# Ask user if they want to downgrade
if [ -t 0 ]; then
echo ""
printf "${BOLD}Would you like to downgrade to $VERSION? [y/N]${NC} "
read -r response
case "$response" in
[yY][eE][sS]|[yY])
echo ""
info "Downgrading DataRobot CLI..."
;;
*)
info "Installation cancelled"
exit 0
;;
esac
else
step "Downgrading to: $VERSION"
fi
fi
fi
}
# Download and extract the binary
download_and_install() {
# Construct download URL
ARCHIVE_NAME="${BINARY_NAME}_${VERSION}_${OS}_${ARCH}.tar.gz"
DOWNLOAD_URL="https://github.com/$REPO/releases/download/$VERSION/$ARCHIVE_NAME"
step "Downloading from GitHub..."
printf " ${DOWNLOAD_URL}\n"
# Create temporary directory
TMP_DIR=$(mktemp -d)
trap 'rm -rf "$TMP_DIR"' EXIT INT TERM
# Download archive
if command -v curl >/dev/null 2>&1; then
if ! curl --fail --location --progress-bar "$DOWNLOAD_URL" -o "$TMP_DIR/$ARCHIVE_NAME"; then
error "Failed to download binary. Please check the version exists: https://github.com/$REPO/releases"
fi
else
if ! wget --show-progress -q "$DOWNLOAD_URL" -O "$TMP_DIR/$ARCHIVE_NAME"; then
error "Failed to download binary. Please check the version exists: https://github.com/$REPO/releases"
fi
fi
# Extract archive
step "Extracting binary..."
if ! tar -xzf "$TMP_DIR/$ARCHIVE_NAME" -C "$TMP_DIR"; then
error "Failed to extract archive"
fi
# Create install directory if it doesn't exist
if [ ! -d "$INSTALL_DIR" ]; then
step "Creating install directory: $INSTALL_DIR"
mkdir -p "$INSTALL_DIR" || error "Failed to create install directory: $INSTALL_DIR"
fi
# Install binary
step "Installing binary to $INSTALL_DIR/$BINARY_NAME..."
if ! mv "$TMP_DIR/$BINARY_NAME" "$INSTALL_DIR/$BINARY_NAME"; then
error "Failed to install binary. Do you have write permissions to $INSTALL_DIR?"
fi
if ! chmod +x "$INSTALL_DIR/$BINARY_NAME"; then
error "Failed to make binary executable"
fi
}
# Show PATH configuration instructions
# Add directory to PATH in shell profile
add_to_path() {
local shell_profile=""
local path_cmd=""
# Detect shell and set appropriate profile file
case "$SHELL" in
*/zsh)
shell_profile="$HOME/.zshrc"
path_cmd="export PATH=\"\$PATH:$INSTALL_DIR\""
;;
*/bash)
shell_profile="$HOME/.bashrc"
path_cmd="export PATH=\"\$PATH:$INSTALL_DIR\""
;;
*/fish)
shell_profile="$HOME/.config/fish/config.fish"
path_cmd="fish_add_path $INSTALL_DIR"
;;
*)
shell_profile="$HOME/.profile"
path_cmd="export PATH=\"\$PATH:$INSTALL_DIR\""
;;
esac
# Add to PATH
if [ -n "$shell_profile" ]; then
# Check if already in profile
if [ -f "$shell_profile" ] && grep -q "$INSTALL_DIR" "$shell_profile" 2>/dev/null; then
step "PATH entry already exists in $shell_profile"
return 0
fi
# Add to profile
echo "" >> "$shell_profile"
echo "# Added by DataRobot CLI installer" >> "$shell_profile"
echo "$path_cmd" >> "$shell_profile"
info "Added $INSTALL_DIR to PATH in $shell_profile"
step "Restart your shell or run: ${BOLD}source $shell_profile${NC}"
return 0
else
warn "Could not detect shell profile"
return 1
fi
}
show_path_instructions() {
echo ""
echo "${YELLOW}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
echo "${BOLD}Next step: Add to PATH${NC}"
echo ""
echo "To use ${BOLD}$BINARY_NAME${NC} from anywhere, add this to your shell profile:"
echo ""
# Detect shell and provide specific instructions
case "$SHELL" in
*/zsh)
echo " ${BLUE}echo 'export PATH=\"\$PATH:$INSTALL_DIR\"' >> ~/.zshrc${NC}"
echo " ${BLUE}source ~/.zshrc${NC}"
;;
*/bash)
echo " ${BLUE}echo 'export PATH=\"\$PATH:$INSTALL_DIR\"' >> ~/.bashrc${NC}"
echo " ${BLUE}source ~/.bashrc${NC}"
;;
*/fish)
echo " ${BLUE}fish_add_path $INSTALL_DIR${NC}"
;;
*)
echo " ${BLUE}export PATH=\"\$PATH:$INSTALL_DIR\"${NC}"
;;
esac
echo ""
echo "Or use the full path: ${BLUE}$INSTALL_DIR/$BINARY_NAME${NC}"
echo "${YELLOW}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
}
# Verify installation
verify_installation() {
if [ -x "$INSTALL_DIR/$BINARY_NAME" ]; then
step "Verifying installation..."
INSTALLED_VERSION=$("$INSTALL_DIR/$BINARY_NAME" --version 2>/dev/null | head -n1 || echo "installed")
printf " ${GREEN}✓${NC} %s\n" "$INSTALLED_VERSION"
else
error "Binary not found at $INSTALL_DIR/$BINARY_NAME"
fi
}
# Prompt user to install shell completions
prompt_completion_install() {
local shell_name=""
# Detect shell name for display
case "$SHELL" in
*/zsh) shell_name="Zsh" ;;
*/bash) shell_name="Bash" ;;
*/fish) shell_name="Fish" ;;
*) shell_name="your shell" ;;
esac
# Only prompt in interactive mode
if [ -t 0 ]; then
echo ""
printf "${BOLD}Would you like to install shell completions for $shell_name? [Y/n]${NC} "
read -r response
case "$response" in
[nN][oO]|[nN])
echo ""
info "Skipping completion installation"
step "You can install completions later with:"
printf " ${BLUE}$BINARY_NAME self completion install --yes${NC}\n"
return 1
;;
*)
echo ""
info "Installing shell completions..."
return 0
;;
esac
else
# Non-interactive mode, skip completions
step "To install completions, run: $BINARY_NAME self completion install --yes"
return 1
fi
}
# Install shell completions using the built-in command
install_completions() {
# Update PATH temporarily for this script
export PATH="$INSTALL_DIR:$PATH"
# Use the interactive completion installer
if "$INSTALL_DIR/$BINARY_NAME" self completion install --yes 2>/dev/null; then
printf " ${GREEN}✓${NC} Shell completions installed successfully\n"
step "Restart your shell to activate completions"
return 0
else
warn "Failed to install completions automatically"
step "Install manually with: $BINARY_NAME self completion install --yes"
return 1
fi
}
# Check if install directory is in PATH
check_path() {
if echo ":$PATH:" | grep -q ":$INSTALL_DIR:"; then
step "Installation directory is in PATH"
return 0
else
warn "$INSTALL_DIR is not in your PATH"
# Ask user if they want to add to PATH automatically
if [ -t 0 ]; then # Check if stdin is a terminal (interactive)
echo ""
printf "${BOLD}Would you like to add $INSTALL_DIR to your PATH automatically? [y/N]${NC} "
read -r response
case "$response" in
[yY][eE][sS]|[yY])
echo ""
if add_to_path; then
return 0
else
show_path_instructions
return 1
fi
;;
*)
echo ""
show_path_instructions
return 1
;;
esac
else
# Non-interactive mode, just show instructions
show_path_instructions
return 1
fi
fi
}
# Main installation flow
main() {
cat << "EOF"
____ __ ____ __ __
/ __ \____ _/ /_____ _/ __ \____ / /_ ____ / /_
/ / / / __ `/ __/ __ `/ /_/ / __ \/ __ \/ __ \/ __/
/ /_/ / /_/ / /_/ /_/ / _, _/ /_/ / /_/ / /_/ / /_
/_____/\__,_/\__/\__,_/_/ |_|\____/_.___/\____/\__/
EOF
info "Installing DataRobot CLI"
echo ""
check_requirements
detect_platform
get_version
check_existing_installation
echo ""
info "Downloading and installing..."
download_and_install
echo ""
info "Installation complete!"
verify_installation
IN_PATH=0
check_path && IN_PATH=1
# Prompt for completion installation
if prompt_completion_install; then
install_completions
fi
echo ""
if [ $IN_PATH -eq 1 ]; then
printf "${GREEN}==>${NC} ${BOLD}Get started by running:${NC} ${BOLD}$BINARY_NAME --help${NC}\n"
else
printf "${GREEN}==>${NC} ${BOLD}Get started by running:${NC} ${BOLD}$INSTALL_DIR/$BINARY_NAME --help${NC}\n"
fi
echo ""
}
main "$@"