forked from uusense/devicebase-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·202 lines (164 loc) · 5.14 KB
/
install.sh
File metadata and controls
executable file
·202 lines (164 loc) · 5.14 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
#!/usr/bin/env bash
set -euo pipefail
# =============================================================================
# Devicebase CLI Installation Script
# Usage: curl -fsSL https://git.uusense.cn/devicebase/devicebase-cli/releases/latest/download/install.sh | bash
# =============================================================================
VERSION="${VERSION:-v2026.5.15}"
BASE_URL="${BASE_URL:-https://github.com/uusense/devicebase-cli/releases}"
INSTALL_DIR="${INSTALL_DIR:-/usr/local/bin}"
BINARY_NAME="devicebase"
CHECKSUM_URL="${CHECKSUM_URL:-}"
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'
info() { echo -e "${GREEN}[INFO]${NC} $1"; }
warn() { echo -e "${YELLOW}[WARN]${NC} $1"; }
error() { echo -e "${RED}[ERROR]${NC} $1"; }
# Detect OS
detect_os() {
case "$(uname -s)" in
Linux*) echo "linux" ;;
Darwin*) echo "darwin" ;;
CYGWIN*) echo "windows" ;;
MINGW*) echo "windows" ;;
MSYS*) echo "windows" ;;
*) error "Unsupported OS: $(uname -s)"; exit 1 ;;
esac
}
# Detect architecture
detect_arch() {
case "$(uname -m)" in
x86_64) echo "amd64" ;;
aarch64) echo "arm64" ;;
armv7l) echo "arm" ;;
i386) echo "386" ;;
i686) echo "386" ;;
arm64) echo "arm64" ;;
*) error "Unsupported architecture: $(uname -m)"; exit 1 ;;
esac
}
# Get latest version number
get_latest_version() {
if [ "$VERSION" != "latest" ]; then
echo "$VERSION"
return
fi
local release_url="${BASE_URL}/tags/${VERSION}"
local tag
if command -v curl >/dev/null 2>&1; then
tag=$(curl -fsSL "${release_url}" 2>/dev/null | grep -oP '(?<=<a href="/devicebase/devicebase-cli/releases/tag/)[^"]+' | head -1 || true)
elif command -v wget >/dev/null 2>&1; then
tag=$(wget -qO- "${release_url}" 2>/dev/null | grep -oP '(?<=<a href="/devicebase/devicebase-cli/releases/tag/)[^"]+' | head -1 || true)
fi
if [ -n "$tag" ]; then
echo "$tag"
else
error "Failed to fetch latest version"
exit 1
fi
}
# Build download URL
build_url() {
local os="$1"
local arch="$2"
local ext=""
[ "$os" = "windows" ] && ext=".exe"
echo "${BASE_URL}/download/${VERSION}/${BINARY_NAME}-${os}-${arch}${ext}"
}
# Download file
download() {
local url="$1"
local dest="$2"
info "Downloading ${url} ..."
if command -v curl >/dev/null 2>&1; then
curl -fsSL -o "$dest" "$url"
elif command -v wget >/dev/null 2>&1; then
wget -q -O "$dest" "$url"
else
error "Neither curl nor wget found. Please install curl or wget."
exit 1
fi
}
# Verify checksum
verify_checksum() {
local file="$1"
local expected_checksum="$2"
if [ -z "$expected_checksum" ]; then
warn "No checksum provided, skipping verification"
return
fi
info "Verifying checksum ..."
local actual_checksum
case "${#expected_checksum}" in
64) actual_checksum=$(sha256sum "$file" | cut -d' ' -f1) ;;
128) actual_checksum=$(sha512sum "$file" | cut -d' ' -f1) ;;
*) actual_checksum=$(shasum -a 256 "$file" | cut -d' ' -f1) ;;
esac
if [ "$actual_checksum" != "$expected_checksum" ]; then
error "Checksum mismatch! Expected: $expected_checksum, Got: $actual_checksum"
exit 1
fi
info "Checksum verified."
}
# Check if running as root for system-wide install
check_permissions() {
if [ -d "$INSTALL_DIR" ] && [ ! -w "$INSTALL_DIR" ]; then
warn "${INSTALL_DIR} is not writable. Using sudo..."
SUDO="sudo"
else
SUDO=""
fi
}
# Install binary
install_binary() {
local src="$1"
local dest="${INSTALL_DIR}/${BINARY_NAME}"
check_permissions
info "Installing to ${dest} ..."
if [ -n "${SUDO:-}" ]; then
$SUDO cp "$src" "$dest"
$SUDO chmod +x "$dest"
else
cp "$src" "$dest"
chmod +x "$dest"
fi
info "Installed successfully: ${dest}"
}
# Main
main() {
info "Devicebase CLI Installer"
info "Version: ${VERSION}"
echo ""
local os=$(detect_os)
local arch=$(detect_arch)
info "Detected: ${os}/${arch}"
# Get version
VERSION=$(get_latest_version)
info "Installing version: ${VERSION}"
echo ""
# Build download URL
local url=$(build_url "$os" "$arch")
local tmp_dir=$(mktemp -d)
local tmp_file="${tmp_dir}/${BINARY_NAME}"
trap "rm -rf $tmp_dir" EXIT
# Download
download "$url" "$tmp_file"
# Verify checksum if provided
if [ -n "$CHECKSUM_URL" ]; then
local checksum_file="${tmp_dir}/checksum.txt"
download "$CHECKSUM_URL" "$checksum_file"
local checksum
checksum=$(grep -E "${os}-${arch}" "$checksum_file" | cut -d' ' -f1 || true)
verify_checksum "$tmp_file" "$checksum"
fi
# Install
install_binary "$tmp_file"
echo ""
info "Devicebase CLI installed successfully!"
info "Run 'devicebase --help' to get started."
info "Don't forget to set DEVICEBASE_API_KEY environment variables."
}
main