-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupgrade.sh
More file actions
executable file
·194 lines (162 loc) · 5.45 KB
/
upgrade.sh
File metadata and controls
executable file
·194 lines (162 loc) · 5.45 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
#!/usr/bin/env bash
# matlas-cli upgrade script
# Updates existing installation to the latest version
set -euo pipefail
# Colors for output
readonly RED='\033[0;31m'
readonly GREEN='\033[0;32m'
readonly YELLOW='\033[1;33m'
readonly BLUE='\033[0;34m'
readonly NC='\033[0m' # No Color
# Configuration
readonly BINARY_NAME="matlas"
# Helper functions
print_success() { echo -e "${GREEN}✓ $1${NC}"; }
print_warning() { echo -e "${YELLOW}⚠ $1${NC}"; }
print_error() { echo -e "${RED}✗ $1${NC}"; }
print_info() { echo -e "${BLUE}ℹ $1${NC}"; }
# Find installed binary
find_installation() {
if command -v "$BINARY_NAME" >/dev/null 2>&1; then
which "$BINARY_NAME"
else
return 1
fi
}
# Get current version
get_current_version() {
local binary_path="$1"
if [[ -x "$binary_path" ]]; then
"$binary_path" version 2>/dev/null | head -1 | awk '{print $NF}' || echo "unknown"
else
echo "unknown"
fi
}
# Get latest version from GitHub
get_latest_version() {
if command -v curl >/dev/null 2>&1; then
curl -s "https://api.github.com/repos/teabranch/matlas-cli/releases/latest" | \
grep '"tag_name":' | \
sed -E 's/.*"([^"]+)".*/\1/'
elif command -v wget >/dev/null 2>&1; then
wget -qO- "https://api.github.com/repos/teabranch/matlas-cli/releases/latest" | \
grep '"tag_name":' | \
sed -E 's/.*"([^"]+)".*/\1/'
else
print_error "Neither curl nor wget found. Please install one of them."
return 1
fi
}
# Compare versions
compare_versions() {
local current="$1"
local latest="$2"
# Remove 'v' prefix if present
current="${current#v}"
latest="${latest#v}"
if [[ "$current" == "$latest" ]]; then
return 0 # Same version
else
return 1 # Different version
fi
}
# Show usage information
show_usage() {
cat << EOF
matlas-cli Upgrade Script
USAGE:
$0 [OPTIONS]
OPTIONS:
-v, --version VERSION Upgrade to specific version (default: latest)
--force Force upgrade even if versions match
-h, --help Show this help
EXAMPLES:
$0 # Upgrade to latest version
$0 -v v1.2.3 # Upgrade to specific version
$0 --force # Force reinstall current version
EOF
}
# Main upgrade function
main() {
local target_version=""
local force=false
# Parse command line arguments
while [[ $# -gt 0 ]]; do
case $1 in
-v|--version)
target_version="$2"
shift 2
;;
--force)
force=true
shift
;;
-h|--help)
show_usage
exit 0
;;
*)
print_error "Unknown option: $1"
show_usage
exit 1
;;
esac
done
print_info "matlas-cli Upgrade Script"
print_info "========================="
# Find existing installation
local binary_path
if ! binary_path=$(find_installation); then
print_error "$BINARY_NAME is not installed or not in PATH"
print_info "Please run the installation script first:"
print_info " curl -fsSL https://raw.githubusercontent.com/teabranch/matlas-cli/main/install.sh | bash"
exit 1
fi
print_info "Found installation: $binary_path"
# Get current version
local current_version
current_version=$(get_current_version "$binary_path")
print_info "Current version: $current_version"
# Get target version
if [[ -z "$target_version" ]]; then
print_info "Fetching latest version..."
if ! target_version=$(get_latest_version); then
print_error "Failed to fetch latest version"
exit 1
fi
fi
print_info "Target version: $target_version"
# Check if upgrade is needed
if [[ "$force" == false ]] && compare_versions "$current_version" "$target_version"; then
print_success "Already running the latest version ($current_version)"
print_info "Use --force to reinstall"
exit 0
fi
# Determine install directory
local install_dir
install_dir=$(dirname "$binary_path")
print_info "Install directory: $install_dir"
# Download and run installation script with appropriate flags
print_info "Downloading installation script..."
local install_flags="--version $target_version --dir $install_dir"
if command -v curl >/dev/null 2>&1; then
curl -fsSL "https://raw.githubusercontent.com/teabranch/matlas-cli/main/install.sh" | bash -s -- $install_flags
elif command -v wget >/dev/null 2>&1; then
wget -qO- "https://raw.githubusercontent.com/teabranch/matlas-cli/main/install.sh" | bash -s -- $install_flags
else
print_error "Neither curl nor wget found. Please install one of them."
exit 1
fi
# Verify upgrade
local new_version
new_version=$(get_current_version "$binary_path")
if [[ "$new_version" == "${target_version#v}" ]] || [[ "$new_version" == "$target_version" ]]; then
print_success "Upgrade completed successfully!"
print_info "Updated from $current_version to $new_version"
else
print_warning "Upgrade may not have completed successfully"
print_info "Expected: $target_version, Got: $new_version"
fi
}
# Run main function with all arguments
main "$@"