-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·274 lines (228 loc) · 10 KB
/
install.sh
File metadata and controls
executable file
·274 lines (228 loc) · 10 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
274
#!/bin/bash
# KalamDB CLI Installer
# Usage: curl -fsSL https://kalamdb.com/install.sh | bash
#
# Environment variables:
# KALAM_INSTALL_DIR - Installation directory (default: $HOME/.kalam/bin)
# KALAM_VERSION - Specific version to install (default: latest)
# KALAM_NO_MODIFY_PATH - Set to 1 to skip PATH modification
set -euo pipefail
# ── Configurable ────────────────────────────────────────────────────────────
GITHUB_REPO="kalamstack/KalamDB"
BINARY_NAME="kalam"
ARTIFACT_PREFIX="kalamcli"
INSTALL_DIR="${KALAM_INSTALL_DIR:-$HOME/.kalam/bin}"
VERSION="${KALAM_VERSION:-}"
NO_MODIFY_PATH="${KALAM_NO_MODIFY_PATH:-0}"
# ── Colors & helpers ────────────────────────────────────────────────────────
RED=$'\033[0;31m'
GREEN=$'\033[0;32m'
YELLOW=$'\033[1;33m'
BLUE=$'\033[0;34m'
BOLD=$'\033[1m'
NC=$'\033[0m' # No Color
info() { printf "${BLUE}▸${NC} %s\n" "$*"; }
ok() { printf "${GREEN}✔${NC} %s\n" "$*"; }
warn() { printf "${YELLOW}⚠${NC} %s\n" "$*"; }
err() { printf "${RED}✘${NC} %b\n" "$*" >&2; }
fatal() { err "$@"; exit 1; }
# ── Detect platform ────────────────────────────────────────────────────────
detect_platform() {
local os arch
os="$(uname -s)"
arch="$(uname -m)"
case "$os" in
Linux) os="linux" ;;
Darwin) os="macos" ;;
MINGW*|MSYS*|CYGWIN*) os="windows" ;;
*) fatal "Unsupported operating system: $os" ;;
esac
case "$arch" in
x86_64|amd64) arch="x86_64" ;;
aarch64|arm64) arch="aarch64" ;;
*) fatal "Unsupported architecture: $arch" ;;
esac
PLATFORM="${os}-${arch}"
}
# ── Check required commands ─────────────────────────────────────────────────
check_deps() {
local missing=()
for cmd in curl tar; do
if ! command -v "$cmd" &>/dev/null; then
missing+=("$cmd")
fi
done
if [[ ${#missing[@]} -gt 0 ]]; then
fatal "Missing required commands: ${missing[*]}"
fi
}
# ── Resolve latest version from GitHub ──────────────────────────────────────
resolve_version() {
if [[ -n "$VERSION" ]]; then
info "Using requested version: $VERSION"
return
fi
info "Fetching latest release version…"
local tag_name=""
# 1) Try the /releases/latest endpoint first
local releases_url="https://api.github.com/repos/${GITHUB_REPO}/releases/latest"
local response
response="$(curl -fsSL "$releases_url" 2>/dev/null)" && \
tag_name="$(echo "$response" | grep '"tag_name"' | head -1 | sed -E 's/.*"tag_name": *"([^"]+)".*/\1/')"
# 2) Fall back to the first tag if no release exists yet
if [[ -z "$tag_name" ]]; then
local tags_url="https://api.github.com/repos/${GITHUB_REPO}/tags"
response="$(curl -fsSL "$tags_url" 2>/dev/null)" || {
fatal "Could not reach GitHub API. Check your internet connection or set KALAM_VERSION."
}
tag_name="$(echo "$response" | grep '"name"' | head -1 | sed -E 's/.*"name": *"([^"]+)".*/\1/')"
fi
if [[ -z "$tag_name" ]]; then
fatal "Could not determine latest version. Set KALAM_VERSION explicitly (e.g. KALAM_VERSION=0.3.0-alpha2)."
fi
# Strip leading 'v' (artifact names don't include it)
VERSION="${tag_name#v}"
ok "Latest version: $VERSION"
}
# ── Download & install ──────────────────────────────────────────────────────
download_and_install() {
# Windows releases use .zip; everything else uses .tar.gz
local ext="tar.gz"
if [[ "$PLATFORM" == windows-* ]]; then
ext="zip"
fi
local archive="${ARTIFACT_PREFIX}-${VERSION}-${PLATFORM}.${ext}"
local base_url="https://github.com/${GITHUB_REPO}/releases/download/v${VERSION}"
local download_url="${base_url}/${archive}"
local checksums_url="${base_url}/SHA256SUMS"
local tmpdir=""
tmpdir="$(mktemp -d)" || fatal "Could not create temporary directory"
trap 'rm -rf "$tmpdir"' EXIT
# Download the archive
info "Downloading ${BOLD}${archive}${NC}…"
curl -fsSL --progress-bar -o "${tmpdir}/${archive}" "$download_url" || {
fatal "Download failed. Check that version '${VERSION}' exists at:\n https://github.com/${GITHUB_REPO}/releases"
}
# Verify checksum if SHA256SUMS is available
info "Verifying checksum…"
if curl -fsSL -o "${tmpdir}/SHA256SUMS" "$checksums_url" 2>/dev/null; then
local expected_hash actual_hash
expected_hash="$(grep "${archive}" "${tmpdir}/SHA256SUMS" | awk '{print $1}')"
if [[ -n "$expected_hash" ]]; then
if command -v sha256sum &>/dev/null; then
actual_hash="$(sha256sum "${tmpdir}/${archive}" | awk '{print $1}')"
elif command -v shasum &>/dev/null; then
actual_hash="$(shasum -a 256 "${tmpdir}/${archive}" | awk '{print $1}')"
else
warn "No sha256sum or shasum found — skipping checksum verification"
actual_hash="$expected_hash"
fi
if [[ "$actual_hash" != "$expected_hash" ]]; then
fatal "Checksum mismatch!\n Expected: ${expected_hash}\n Got: ${actual_hash}"
fi
ok "Checksum verified"
else
warn "Checksum entry for ${archive} not found in SHA256SUMS — skipping verification"
fi
else
warn "SHA256SUMS not available — skipping checksum verification"
fi
# Extract
info "Extracting…"
if [[ "$ext" == "zip" ]]; then
command -v unzip &>/dev/null || fatal "unzip is required to install on Windows"
unzip -q "${tmpdir}/${archive}" -d "${tmpdir}" || fatal "Failed to extract archive"
else
tar -xzf "${tmpdir}/${archive}" -C "${tmpdir}" || fatal "Failed to extract archive"
fi
# Find the binary — could be named 'kalam', 'kalamcli', with or without version suffix
local binary_path
binary_path="$(find "${tmpdir}" -type f \( -name "${BINARY_NAME}" -o -name "${ARTIFACT_PREFIX}" \) | head -1)"
if [[ -z "$binary_path" ]]; then
# Fallback: any executable file that isn't an archive or metadata
binary_path="$(find "${tmpdir}" -type f ! -name '*.tar.gz' ! -name '*.zip' ! -name 'SHA256SUMS' ! -name '*.txt' ! -name '*.md' | head -1)"
fi
if [[ -z "$binary_path" ]]; then
fatal "Could not find ${BINARY_NAME} binary in the archive"
fi
# Install as 'kalam' regardless of archive naming
mkdir -p "$INSTALL_DIR"
cp "$binary_path" "${INSTALL_DIR}/${BINARY_NAME}"
chmod +x "${INSTALL_DIR}/${BINARY_NAME}"
# Explicit cleanup and disarm the trap so 'set -u' doesn't complain after return
rm -rf "$tmpdir"
trap - EXIT
ok "Installed ${BOLD}${BINARY_NAME}${NC} to ${INSTALL_DIR}/${BINARY_NAME}"
}
# ── Update PATH ─────────────────────────────────────────────────────────────
configure_path() {
if [[ "$NO_MODIFY_PATH" == "1" ]]; then
return
fi
# Check if already in PATH
if echo "$PATH" | tr ':' '\n' | grep -qx "$INSTALL_DIR"; then
return
fi
local export_line="export PATH=\"${INSTALL_DIR}:\$PATH\""
local shell_name
shell_name="$(basename "${SHELL:-/bin/bash}")"
local rc_files=()
case "$shell_name" in
zsh)
rc_files=("$HOME/.zshrc")
;;
bash)
# Prefer .bashrc on Linux, .bash_profile on macOS
if [[ -f "$HOME/.bash_profile" ]]; then
rc_files=("$HOME/.bash_profile")
elif [[ -f "$HOME/.bashrc" ]]; then
rc_files=("$HOME/.bashrc")
else
rc_files=("$HOME/.bashrc")
fi
;;
fish)
local fish_conf="$HOME/.config/fish/config.fish"
mkdir -p "$(dirname "$fish_conf")"
if ! grep -qF "$INSTALL_DIR" "$fish_conf" 2>/dev/null; then
echo "set -gx PATH \"${INSTALL_DIR}\" \$PATH" >> "$fish_conf"
ok "Added ${INSTALL_DIR} to PATH in ${fish_conf}"
fi
return
;;
*)
rc_files=("$HOME/.profile")
;;
esac
for rc in "${rc_files[@]}"; do
if ! grep -qF "$INSTALL_DIR" "$rc" 2>/dev/null; then
echo "" >> "$rc"
echo "# KalamDB CLI" >> "$rc"
echo "$export_line" >> "$rc"
ok "Added ${INSTALL_DIR} to PATH in ${rc}"
fi
done
}
# ── Main ────────────────────────────────────────────────────────────────────
main() {
printf "\n${BOLD} KalamDB CLI Installer${NC}\n\n"
check_deps
detect_platform
info "Detected platform: ${BOLD}${PLATFORM}${NC}"
resolve_version
download_and_install
configure_path
printf "\n${GREEN}${BOLD} Installation complete!${NC}\n\n"
# Check if the binary is already on PATH
if command -v "$BINARY_NAME" &>/dev/null; then
local installed_path
installed_path="$(command -v "$BINARY_NAME")"
info "Binary available at: ${installed_path}"
printf "\n Run ${BOLD}kalam --help${NC} to get started.\n\n"
else
printf " To get started, restart your shell or run:\n\n"
printf " ${BOLD}export PATH=\"${INSTALL_DIR}:\$PATH\"${NC}\n\n"
printf " Then run ${BOLD}kalam --help${NC}\n\n"
fi
}
main "$@"