-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathinstall.sh
More file actions
164 lines (142 loc) · 5.04 KB
/
Copy pathinstall.sh
File metadata and controls
164 lines (142 loc) · 5.04 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
#!/usr/bin/env bash
set -euo pipefail
APP_NAME="clausura"
APP_VERSION="${1:-latest}"
GITHUB_REPO="liuyanghejerry/Clausura"
# --- Color helpers ---
RED='\033[0;31m'
YELLOW='\033[1;33m'
GREEN='\033[0;32m'
NC='\033[0m' # No Color
info() { echo -e "${GREEN}[info]${NC} $*"; }
warn() { echo -e "${YELLOW}[warn]${NC} $*"; }
error() { echo -e "${RED}[error]${NC} $*"; }
# --- Detect OS and arch ---
OS="$(uname -s | tr '[:upper:]' '[:lower:]')"
ARCH="$(uname -m)"
case "$OS" in
linux) TARGET="unknown-linux-gnu" ;;
darwin) TARGET="apple-darwin" ;;
mingw*|msys*|cygwin*) OS="windows"; TARGET="pc-windows-msvc" ;;
*) error "Unsupported OS: $OS"; exit 1 ;;
esac
case "$ARCH" in
x86_64|amd64) ARCH="x86_64" ;;
aarch64|arm64) ARCH="aarch64" ;;
*) error "Unsupported arch: $ARCH"; exit 1 ;;
esac
# --- Determine install directory ---
if [ -w "/usr/local/bin" ]; then
INSTALL_DIR="/usr/local/bin"
elif [ -w "$HOME/.local/bin" ]; then
INSTALL_DIR="$HOME/.local/bin"
else
mkdir -p "$HOME/.local/bin"
INSTALL_DIR="$HOME/.local/bin"
fi
mkdir -p "$INSTALL_DIR"
# --- Construct download URL ---
if [ "$APP_VERSION" = "latest" ]; then
BASE_URL="https://github.com/$GITHUB_REPO/releases/latest/download"
else
BASE_URL="https://github.com/$GITHUB_REPO/releases/download/v${APP_VERSION}"
fi
DOWNLOAD_URL="$BASE_URL/${APP_NAME}-${ARCH}-${TARGET}.tar.gz"
CHECKSUMS_URL="$BASE_URL/checksums.txt"
# --- Try prebuilt binary first ---
download_binary() {
info "Downloading $APP_NAME $APP_VERSION for ${ARCH}-${TARGET}..."
local tmp_dir
tmp_dir=$(mktemp -d)
trap "rm -rf '$tmp_dir'" EXIT
local http_code
if command -v curl &>/dev/null; then
http_code=$(curl -fsSL -w "%{http_code}" -o "$tmp_dir/${APP_NAME}.tar.gz" "$DOWNLOAD_URL")
elif command -v wget &>/dev/null; then
# wget: capture HTTP status from server response headers
http_code=$(wget -q --server-response "$DOWNLOAD_URL" -O "$tmp_dir/${APP_NAME}.tar.gz" 2>&1 | \
awk '/HTTP\// {print $2}' | tail -1)
if [ -z "$http_code" ]; then
http_code="000"
fi
else
error "curl or wget is required for download"
return 1
fi
if [ "$http_code" != "200" ]; then
warn "Prebuilt binary not available (HTTP $http_code)"
return 1
fi
# Download checksums and verify the tarball's SHA256
if command -v curl &>/dev/null; then
curl -fsSL -o "$tmp_dir/checksums.txt" "$CHECKSUMS_URL" || true
else
wget -q -O "$tmp_dir/checksums.txt" "$CHECKSUMS_URL" || true
fi
if [ ! -s "$tmp_dir/checksums.txt" ]; then
warn "Checksum file not available, cannot verify download"
return 1
fi
local tarball_name="${APP_NAME}-${ARCH}-${TARGET}.tar.gz"
local verify_ok=1
if [ "$OS" = "darwin" ]; then
(cd "$tmp_dir" && grep " ${tarball_name}\$" checksums.txt | shasum -a 256 -c) && verify_ok=0
else
(cd "$tmp_dir" && grep " ${tarball_name}\$" checksums.txt | sha256sum -c) && verify_ok=0
fi
if [ "$verify_ok" != "0" ]; then
warn "Checksum verification failed for $tarball_name"
return 1
fi
info "Checksum verified."
tar xzf "$tmp_dir/${APP_NAME}.tar.gz" -C "$tmp_dir"
cp "$tmp_dir/${APP_NAME}" "$INSTALL_DIR/"
chmod +x "$INSTALL_DIR/$APP_NAME"
return 0
}
# --- Fallback: install via cargo ---
install_via_cargo() {
if ! command -v cargo &>/dev/null; then
error "Prebuilt binary not available and 'cargo' is not installed."
echo ""
echo "Install options:"
echo " 1. Install Rust: curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh"
echo " Then run: cargo install clausura-cli"
echo " 2. Use Docker: docker pull ghcr.io/liuyanghejerry/clausura:latest"
echo " 3. Build from source:"
echo " git clone https://github.com/liuyanghejerry/Clausura.git"
echo " cd Clausura && cargo build --release --package clausura-cli"
return 1
fi
warn "Prebuilt binary not available, falling back to cargo install..."
if [ -n "${APP_VERSION}" ] && [ "$APP_VERSION" != "latest" ]; then
cargo install clausura-cli --version "$APP_VERSION"
else
cargo install clausura-cli
fi
# Find where cargo installed the binary
if command -v "$APP_NAME" &>/dev/null; then
INSTALL_DIR="$(dirname "$(command -v "$APP_NAME")")"
fi
return 0
}
# --- Main ---
if download_binary; then
info "Installed $APP_NAME (prebuilt) to $INSTALL_DIR/$APP_NAME"
elif install_via_cargo; then
info "Installed $APP_NAME (via cargo) to $INSTALL_DIR/$APP_NAME"
else
error "Installation failed."
exit 1
fi
# --- Verify ---
if command -v "$APP_NAME" &>/dev/null; then
echo ""
info "Installation successful!"
"$APP_NAME" --version 2>/dev/null || true
echo "Run '$APP_NAME --help' to get started."
else
warn "$APP_NAME was installed but is not on PATH."
echo "Add $INSTALL_DIR to your PATH:"
echo " export PATH=\"$INSTALL_DIR:\$PATH\""
fi