Skip to content

Commit 9eeb477

Browse files
committed
scripts: add internal install script
1 parent 122243d commit 9eeb477

1 file changed

Lines changed: 185 additions & 0 deletions

File tree

scripts/install.sh

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
#!/usr/bin/env bash
2+
3+
set -euo pipefail
4+
5+
REPO="${HEYGEN_REPO:-heygen-com/heygen-cli}"
6+
RELEASE_TAG="${HEYGEN_RELEASE_TAG:-dev}"
7+
INSTALL_DIR="${INSTALL_DIR:-$HOME/.local/bin}"
8+
TMPDIR="$(mktemp -d)"
9+
trap 'rm -rf "$TMPDIR"' EXIT
10+
11+
log() {
12+
printf '%s\n' "$*"
13+
}
14+
15+
fail() {
16+
printf 'error: %s\n' "$*" >&2
17+
exit 1
18+
}
19+
20+
detect_os() {
21+
case "$(uname -s)" in
22+
Linux) printf 'linux\n' ;;
23+
Darwin) printf 'darwin\n' ;;
24+
*)
25+
fail "unsupported OS: $(uname -s). Install manually from the release assets."
26+
;;
27+
esac
28+
}
29+
30+
detect_arch() {
31+
case "$(uname -m)" in
32+
x86_64 | amd64) printf 'amd64\n' ;;
33+
arm64 | aarch64) printf 'arm64\n' ;;
34+
*)
35+
fail "unsupported architecture: $(uname -m). Install manually from the release assets."
36+
;;
37+
esac
38+
}
39+
40+
checksum_cmd() {
41+
if command -v sha256sum >/dev/null 2>&1; then
42+
printf 'sha256sum\n'
43+
return
44+
fi
45+
if command -v shasum >/dev/null 2>&1; then
46+
printf 'shasum -a 256\n'
47+
return
48+
fi
49+
fail "missing sha256 tool (need sha256sum or shasum)"
50+
}
51+
52+
github_token() {
53+
if [[ -n "${GITHUB_TOKEN:-}" ]]; then
54+
printf '%s\n' "$GITHUB_TOKEN"
55+
return
56+
fi
57+
if [[ -n "${GH_TOKEN:-}" ]]; then
58+
printf '%s\n' "$GH_TOKEN"
59+
return
60+
fi
61+
if command -v gh >/dev/null 2>&1; then
62+
local token
63+
token="$(gh auth token 2>/dev/null || true)"
64+
if [[ -n "$token" ]]; then
65+
printf '%s\n' "$token"
66+
return
67+
fi
68+
fi
69+
fail "set GITHUB_TOKEN or authenticate gh before running this installer"
70+
}
71+
72+
download_with_gh() {
73+
local asset_name="$1"
74+
local checksums_name="$2"
75+
76+
if ! command -v gh >/dev/null 2>&1; then
77+
return 1
78+
fi
79+
if ! gh auth token >/dev/null 2>&1; then
80+
return 1
81+
fi
82+
83+
if ! gh release download "$RELEASE_TAG" --repo "$REPO" --pattern "$asset_name" --dir "$TMPDIR"; then
84+
return 1
85+
fi
86+
if ! gh release download "$RELEASE_TAG" --repo "$REPO" --pattern "$checksums_name" --dir "$TMPDIR"; then
87+
return 1
88+
fi
89+
if [[ ! -f "${TMPDIR}/${asset_name}" || ! -f "${TMPDIR}/${checksums_name}" ]]; then
90+
return 1
91+
fi
92+
return 0
93+
}
94+
95+
download_with_curl() {
96+
local asset_name="$1"
97+
local checksums_name="$2"
98+
local token="$3"
99+
local base_url
100+
101+
if [[ -n "${HEYGEN_RELEASE_BASE_URL:-}" ]]; then
102+
base_url="${HEYGEN_RELEASE_BASE_URL%/}"
103+
else
104+
base_url="https://github.com/${REPO}/releases/download/${RELEASE_TAG}"
105+
fi
106+
107+
curl -fsSL -H "Authorization: Bearer ${token}" \
108+
"${base_url}/${asset_name}" -o "${TMPDIR}/${asset_name}"
109+
curl -fsSL -H "Authorization: Bearer ${token}" \
110+
"${base_url}/${checksums_name}" -o "${TMPDIR}/${checksums_name}"
111+
}
112+
113+
verify_checksum() {
114+
local asset_name="$1"
115+
local checksums_name="$2"
116+
local checksum_tool
117+
local expected actual
118+
119+
checksum_tool="$(checksum_cmd)"
120+
expected="$(grep " ${asset_name}\$" "${TMPDIR}/${checksums_name}" | awk '{print $1}')"
121+
if [[ -z "$expected" ]]; then
122+
fail "could not find checksum for ${asset_name}"
123+
fi
124+
125+
actual="$($checksum_tool "${TMPDIR}/${asset_name}" | awk '{print $1}')"
126+
if [[ "$expected" != "$actual" ]]; then
127+
fail "checksum verification failed for ${asset_name}"
128+
fi
129+
}
130+
131+
install_archive() {
132+
local archive="$1"
133+
local os="$2"
134+
local extracted_bin="$TMPDIR/heygen"
135+
136+
if [[ "$os" == "darwin" || "$os" == "linux" ]]; then
137+
tar -C "$TMPDIR" -xzf "$archive" heygen
138+
else
139+
fail "unsupported OS for automatic install: ${os}"
140+
fi
141+
142+
mkdir -p "$INSTALL_DIR"
143+
install -m 0755 "$extracted_bin" "${INSTALL_DIR}/heygen"
144+
}
145+
146+
main() {
147+
local os arch asset_name checksums_name token version_output
148+
149+
os="$(detect_os)"
150+
arch="$(detect_arch)"
151+
asset_name="heygen_${os}_${arch}.tar.gz"
152+
checksums_name="checksums.txt"
153+
154+
log "Detected: ${os} ${arch}"
155+
log "Installing heygen from ${REPO} release tag '${RELEASE_TAG}'"
156+
157+
if download_with_gh "$asset_name" "$checksums_name"; then
158+
log "Downloaded release assets with gh"
159+
else
160+
token="$(github_token)"
161+
download_with_curl "$asset_name" "$checksums_name" "$token"
162+
log "Downloaded release assets with curl"
163+
fi
164+
165+
verify_checksum "$asset_name" "$checksums_name"
166+
install_archive "${TMPDIR}/${asset_name}" "$os"
167+
168+
version_output="$("${INSTALL_DIR}/heygen" --version 2>/dev/null || true)"
169+
if [[ -z "$version_output" ]]; then
170+
fail "installed binary but version check failed"
171+
fi
172+
173+
log "Installed: ${INSTALL_DIR}/heygen"
174+
log "${version_output}"
175+
176+
case ":$PATH:" in
177+
*":${INSTALL_DIR}:"*) ;;
178+
*)
179+
log ""
180+
log "Add ${INSTALL_DIR} to your PATH if it is not already included."
181+
;;
182+
esac
183+
}
184+
185+
main "$@"

0 commit comments

Comments
 (0)