-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·205 lines (171 loc) · 7.12 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·205 lines (171 loc) · 7.12 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
#!/bin/bash
# MobileCLI Installer (Linux & macOS)
# Usage: curl -fsSL https://raw.githubusercontent.com/MobileCLI/mobilecli/main/install.sh | bash
#
# Windows users: install via cargo or download binaries from GitHub Releases.
# cargo install --git https://github.com/MobileCLI/mobilecli --path cli
set -euo pipefail
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
CYAN='\033[0;36m'
NC='\033[0m' # No Color
REPO="MobileCLI/mobilecli"
BINARY_NAME="mobilecli"
CHECKSUM_FILE="SHA256SUMS.txt"
# Print styled messages
info() { echo -e "${CYAN}$1${NC}"; }
success() { echo -e "${GREEN}✓ $1${NC}"; }
warn() { echo -e "${YELLOW}⚠ $1${NC}"; }
error() { echo -e "${RED}✗ $1${NC}" >&2; exit 1; }
# Compute a SHA-256 digest using the platform's available tool.
sha256_digest() {
local file="$1"
if command -v sha256sum >/dev/null 2>&1; then
sha256sum "$file" | awk '{print $1}'
elif command -v shasum >/dev/null 2>&1; then
shasum -a 256 "$file" | awk '{print $1}'
else
error "No SHA-256 checksum tool found. Install sha256sum or shasum and try again."
fi
}
# Verify the downloaded archive against the release checksum manifest.
verify_archive_checksum() {
local archive_path="$1"
local archive_name="$2"
local checksum_path="$3"
local expected actual
if [ ! -s "$checksum_path" ]; then
error "Checksum manifest is missing or empty."
fi
expected=$(awk -v file="$archive_name" '($2 == file || $2 == "*" file || $2 == "./" file) { print $1; exit }' "$checksum_path")
if [ -z "$expected" ]; then
error "No checksum entry found for ${archive_name}. Refusing to install."
fi
expected=$(printf '%s' "$expected" | tr '[:upper:]' '[:lower:]')
if ! printf '%s\n' "$expected" | grep -Eq '^[0-9a-f]{64}$'; then
error "Invalid checksum entry for ${archive_name}. Refusing to install."
fi
actual=$(sha256_digest "$archive_path" | tr '[:upper:]' '[:lower:]')
if [ "$actual" != "$expected" ]; then
error "Checksum verification failed for ${archive_name}. Refusing to install."
fi
}
# Map OS/arch to Rust target triple (matches release workflow archives)
detect_target() {
local os arch
os="$(uname -s)"
arch="$(uname -m)"
case "${os}-${arch}" in
Linux-x86_64) echo "x86_64-unknown-linux-gnu" ;;
Linux-aarch64) echo "aarch64-unknown-linux-gnu" ;;
Darwin-x86_64) echo "x86_64-apple-darwin" ;;
Darwin-arm64) echo "aarch64-apple-darwin" ;;
*) error "Unsupported platform: ${os} ${arch}" ;;
esac
}
# Get the latest release version
get_latest_version() {
local version
version=$(curl -fsSL "https://api.github.com/repos/${REPO}/releases/latest" 2>/dev/null | grep '"tag_name"' | sed -E 's/.*"([^"]+)".*/\1/')
if [ -z "$version" ]; then
error "Failed to fetch latest version. Check your internet connection."
fi
echo "$version"
}
# Get install directory
get_install_dir() {
# Check for common user-local bin directories
if [ -d "$HOME/.local/bin" ] && echo "$PATH" | grep -q "$HOME/.local/bin"; then
echo "$HOME/.local/bin"
elif [ -d "$HOME/bin" ] && echo "$PATH" | grep -q "$HOME/bin"; then
echo "$HOME/bin"
elif [ -w "/usr/local/bin" ]; then
echo "/usr/local/bin"
else
# Create ~/.local/bin if it doesn't exist
mkdir -p "$HOME/.local/bin"
echo "$HOME/.local/bin"
fi
}
# Download and install
install() {
local platform version install_dir archive_name download_url checksums_url tmp_dir
info "╔══════════════════════════════════════════════════════════════╗"
info "║ 📱 MobileCLI Installer ║"
info "╚══════════════════════════════════════════════════════════════╝"
echo
# Detect target
platform=$(detect_target)
info "Detected target: $platform"
# Get latest version
info "Fetching latest version..."
version=$(get_latest_version)
success "Latest version: $version"
# Determine install directory
install_dir=$(get_install_dir)
info "Install directory: $install_dir"
# Construct download URL
archive_name="${BINARY_NAME}-${version}-${platform}.tar.gz"
download_url="https://github.com/${REPO}/releases/download/${version}/${archive_name}"
checksums_url="https://github.com/${REPO}/releases/download/${version}/${CHECKSUM_FILE}"
# Create temp directory
tmp_dir=$(mktemp -d)
trap "rm -rf '${tmp_dir}'" EXIT
# Download checksum manifest
info "Downloading ${CHECKSUM_FILE}..."
if ! curl -fsSL "$checksums_url" -o "${tmp_dir}/${CHECKSUM_FILE}"; then
error "Failed to download checksums from $checksums_url"
fi
success "Downloaded checksums"
# Download archive
info "Downloading ${archive_name}..."
if ! curl -fsSL "$download_url" -o "${tmp_dir}/${archive_name}"; then
error "Failed to download from $download_url"
fi
success "Downloaded successfully"
# Verify archive before extraction
info "Verifying checksum..."
verify_archive_checksum "${tmp_dir}/${archive_name}" "$archive_name" "${tmp_dir}/${CHECKSUM_FILE}"
success "Checksum verified"
# Extract archive
info "Extracting..."
tar -xzf "${tmp_dir}/${archive_name}" -C "$tmp_dir"
# Install binary
info "Installing to ${install_dir}..."
if [ -w "$install_dir" ]; then
mv "${tmp_dir}/${BINARY_NAME}" "${install_dir}/${BINARY_NAME}"
chmod +x "${install_dir}/${BINARY_NAME}"
else
warn "Need sudo to install to ${install_dir}"
sudo mv "${tmp_dir}/${BINARY_NAME}" "${install_dir}/${BINARY_NAME}"
sudo chmod +x "${install_dir}/${BINARY_NAME}"
fi
success "Installed to ${install_dir}/${BINARY_NAME}"
# Check if install_dir is in PATH
if ! echo "$PATH" | grep -q "$install_dir"; then
echo
warn "Note: $install_dir is not in your PATH"
echo "Add it to your shell config:"
echo " export PATH=\"\$PATH:$install_dir\""
echo
fi
# Verify installation
echo
success "╔══════════════════════════════════════════════════════════════╗"
success "║ ✓ Installation Complete! ║"
success "╚══════════════════════════════════════════════════════════════╝"
echo
info "Run 'mobilecli setup' to get started!"
echo
# Show version
if command -v mobilecli &> /dev/null; then
mobilecli --version
fi
}
# Run installation when executed directly. Tests can source this file to exercise
# checksum helpers without downloading or installing.
if [[ "${BASH_SOURCE[0]:-$0}" == "$0" ]]; then
install
fi