-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathinstall
More file actions
executable file
·274 lines (236 loc) · 7.38 KB
/
install
File metadata and controls
executable file
·274 lines (236 loc) · 7.38 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
#!/bin/sh
# Harness CLI (hc) Installation Script
# Usage: curl https://raw.githubusercontent.com/harness/harness-cli/v2/install | sh
set -e
# Configuration
REPO="harness/harness-cli"
BINARY_NAME="hc"
DEFAULT_VERSION="v1.0.0-beta.3"
INSTALL_DIR="${INSTALL_DIR:-/usr/local/bin}"
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# Helper functions
info() {
printf "${GREEN}[INFO]${NC} %s\n" "$1"
}
warn() {
printf "${YELLOW}[WARN]${NC} %s\n" "$1"
}
error() {
printf "${RED}[ERROR]${NC} %s\n" "$1"
exit 1
}
# Detect OS
detect_os() {
case "$(uname -s)" in
Darwin*)
echo "mac-os"
;;
Linux*)
echo "linux"
;;
MINGW* | MSYS* | CYGWIN*)
echo "windows"
;;
*)
error "Unsupported operating system: $(uname -s)"
;;
esac
}
# Detect architecture
detect_arch() {
case "$(uname -m)" in
x86_64 | amd64)
echo "x86_64"
;;
aarch64 | arm64)
echo "arm64"
;;
armv7l)
echo "armv7"
;;
i386 | i686)
echo "i386"
;;
*)
error "Unsupported architecture: $(uname -m)"
;;
esac
}
# Get the latest version from GitHub
get_latest_version() {
if command -v curl >/dev/null 2>&1; then
curl -sL "https://api.github.com/repos/${REPO}/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/${REPO}/releases/latest" | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/'
else
echo "$DEFAULT_VERSION"
fi
}
# Download file
download_file() {
url="$1"
output="$2"
if command -v curl >/dev/null 2>&1; then
curl -fsSL "$url" -o "$output"
elif command -v wget >/dev/null 2>&1; then
wget -q "$url" -O "$output"
else
error "Neither curl nor wget found. Please install one of them."
fi
}
# Verify checksum of downloaded file
verify_checksum() {
file="$1"
checksums_file="$2"
filename=$(basename "$file")
# Check if sha256sum or shasum is available
if command -v sha256sum >/dev/null 2>&1; then
expected_checksum=$(grep "$filename" "$checksums_file" | awk '{print $1}')
if [ -z "$expected_checksum" ]; then
warn "Checksum for $filename not found in checksums.txt"
return 1
fi
actual_checksum=$(sha256sum "$file" | awk '{print $1}')
elif command -v shasum >/dev/null 2>&1; then
expected_checksum=$(grep "$filename" "$checksums_file" | awk '{print $1}')
if [ -z "$expected_checksum" ]; then
warn "Checksum for $filename not found in checksums.txt"
return 1
fi
actual_checksum=$(shasum -a 256 "$file" | awk '{print $1}')
else
warn "Neither sha256sum nor shasum found, skipping checksum verification"
return 0
fi
if [ "$expected_checksum" = "$actual_checksum" ]; then
return 0
else
error "Checksum mismatch!"
error "Expected: $expected_checksum"
error "Got: $actual_checksum"
return 1
fi
}
# Validate version tag
validate_version() {
version="$1"
# Check if version starts with "v1"
case "$version" in
v1*)
return 0
;;
*)
error "Invalid version: $version. This installer only supports v1.x releases."
;;
esac
}
# Check if running with sufficient privileges
check_privileges() {
if [ ! -w "$INSTALL_DIR" ]; then
if [ "$(id -u)" -ne 0 ]; then
warn "Installation requires sudo privileges. Please re-run with sudo or run:"
warn " curl -fsSL https://raw.githubusercontent.com/harness/harness-cli/v2/install | sudo sh"
error "Insufficient privileges to write to $INSTALL_DIR"
fi
fi
}
# Main installation function
main() {
info "Starting Harness CLI installation..."
# Detect system
OS=$(detect_os)
ARCH=$(detect_arch)
info "Detected OS: $OS"
info "Detected Architecture: $ARCH"
# Get version
VERSION="${HC_VERSION:-$(get_latest_version)}"
if [ -z "$VERSION" ]; then
VERSION="$DEFAULT_VERSION"
fi
# Validate version starts with v1
validate_version "$VERSION"
info "Installing version: $VERSION"
# Construct download URL and filename
if [ "$OS" = "windows" ]; then
FILENAME="${BINARY_NAME}_${VERSION#v}_${OS}_${ARCH}.zip"
ARCHIVE_TYPE="zip"
else
FILENAME="${BINARY_NAME}_${VERSION#v}_${OS}_${ARCH}.tar.gz"
ARCHIVE_TYPE="tar.gz"
fi
DOWNLOAD_URL="https://github.com/${REPO}/releases/download/${VERSION}/${FILENAME}"
info "Download URL: $DOWNLOAD_URL"
# Create temporary directory
TMP_DIR=$(mktemp -d)
trap "rm -rf $TMP_DIR" EXIT
# Download archive
info "Downloading $FILENAME..."
if ! download_file "$DOWNLOAD_URL" "$TMP_DIR/$FILENAME"; then
error "Failed to download from $DOWNLOAD_URL"
fi
# Download checksums file
CHECKSUMS_URL="https://github.com/${REPO}/releases/download/${VERSION}/checksums.txt"
info "Downloading checksums.txt for verification..."
if ! download_file "$CHECKSUMS_URL" "$TMP_DIR/checksums.txt"; then
warn "Failed to download checksums.txt, skipping verification"
else
# Verify checksum
info "Verifying checksum..."
if ! verify_checksum "$TMP_DIR/$FILENAME" "$TMP_DIR/checksums.txt"; then
error "Checksum verification failed! The downloaded file may be corrupted or tampered with."
fi
info "✓ Checksum verified successfully"
fi
# Extract archive
info "Extracting archive..."
cd "$TMP_DIR"
if [ "$ARCHIVE_TYPE" = "tar.gz" ]; then
tar -xzf "$FILENAME"
elif [ "$ARCHIVE_TYPE" = "zip" ]; then
if command -v unzip >/dev/null 2>&1; then
unzip -q "$FILENAME"
else
error "unzip is required but not installed"
fi
fi
# Verify binary exists
if [ ! -f "$BINARY_NAME" ]; then
error "Binary $BINARY_NAME not found in archive"
fi
# Check installation privileges
check_privileges
# Install binary
info "Installing $BINARY_NAME to $INSTALL_DIR..."
# Make binary executable
chmod +x "$BINARY_NAME"
# Move to installation directory
if [ -w "$INSTALL_DIR" ]; then
mv "$BINARY_NAME" "$INSTALL_DIR/"
else
# Need sudo
if command -v sudo >/dev/null 2>&1; then
sudo mv "$BINARY_NAME" "$INSTALL_DIR/"
else
error "sudo is required to install to $INSTALL_DIR"
fi
fi
# Verify installation
if command -v "$BINARY_NAME" >/dev/null 2>&1; then
INSTALLED_VERSION=$("$BINARY_NAME" version 2>&1 || echo "unknown")
info "✓ Installation successful!"
info "Installed version: $INSTALLED_VERSION"
info "Binary location: $INSTALL_DIR/$BINARY_NAME"
echo ""
info "Run '$BINARY_NAME --help' to get started"
else
warn "Installation completed but $BINARY_NAME not found in PATH"
warn "You may need to add $INSTALL_DIR to your PATH:"
warn " export PATH=\"\$PATH:$INSTALL_DIR\""
fi
}
# Run main function
main