Open History to see the last commit
1#!/bin/sh
2# Installs the T3 Code CLI from a GitHub Release archive. Needs only sh, tar,
3# sha256sum or shasum, and curl or wget; no Node, npm, or compiler.
4#
5# curl -fsSL https://t3.codes/install.sh | sh
6#
7# Environment:
8# T3CODE_CHANNEL release train to follow: stable, nightly, or preview
9# (default: stable; preview is a maintainers' test train)
10# T3CODE_VERSION exact version to install (overrides T3CODE_CHANNEL)
11# T3CODE_HOME T3 home directory (default: ~/.t3)
12# T3CODE_INSTALL_BIN_DIR where the `t3` symlink goes (default: ~/.local/bin)
13# T3CODE_RELEASE_BASE_URL mirror for releases/download (default: GitHub)
14#
15# The archive is unpacked into $T3CODE_HOME/runtime/versions/<version>, the
16# same layout `t3 service install` uses, so the service reuses this download
17# instead of fetching the release again.
18set -eu
19
20repo="pingdotgg/t3code"
21base_url="${T3CODE_RELEASE_BASE_URL:-https://github.com/${repo}/releases/download}"
22t3_home="${T3CODE_HOME:-$HOME/.t3}"
23case "$t3_home" in
24 /*) ;;
25 *)
26 # Preserve cwd's trailing newlines; trim only pwd's newline and the sentinel.
27 t3_cwd="$(pwd -P && printf .)"
28 t3_home="${t3_cwd%??}/$t3_home"
29 ;;
30esac
31bin_dir="${T3CODE_INSTALL_BIN_DIR:-$HOME/.local/bin}"
32# Trailing slashes would put `//` in the paths the t3 command guard compares.
33while [ "${t3_home%/}" != "$t3_home" ] && [ "$t3_home" != / ]; do t3_home="${t3_home%/}"; done
34while [ "${bin_dir%/}" != "$bin_dir" ] && [ "$bin_dir" != / ]; do bin_dir="${bin_dir%/}"; done
35# Absolute, so the PATH hint never adds a folder that changes with the working directory.
36case "$bin_dir" in /*) ;; *) bin_dir="$PWD/$bin_dir" ;; esac
37
38fail() {
39 printf '\nt3 install: %s\n' "$1" >&2
40 exit 1
41}
42
43# ANSI stays on stderr, so `curl ... | sh` still gets progress.
44interactive=false
45if [ -t 2 ] && [ "${TERM:-}" != dumb ]; then interactive=true; fi
46reset= bold= muted= accent= green=
47if "$interactive" && [ -z "${NO_COLOR:-}" ]; then
48 reset="$(printf '\033[0m')"; bold="$(printf '\033[1m')"
49 muted="$(printf '\033[2m')"; accent="$(printf '\033[94m')"; green="$(printf '\033[32m')"
50fi
51step() {
52 if "$interactive"; then printf '\r\033[2K %s%s%s' "$muted" "$1" "$reset" >&2
53 else printf ' %s\n' "$1" >&2; fi
54}
55if "$interactive"; then
56 printf '\n%s' "$bold" >&2
57 printf ' %s\n' '██████████ ████████ ' >&2
58 printf ' %s\n' ' ███ ▄██▀ T3 Code' >&2
59 printf ' %s%s %sCLI installer%s\n' ' ███ ████▄ ' "$reset" "$muted" "$reset$bold" >&2
60 printf ' %s\n' ' ███ ▄ ███' >&2
61 printf ' %s\n' ' ███ ███████▀ ' >&2
62 printf '%s\n' "$reset" >&2
63fi
64step "Finding your release..."
65
66# Exit 44 on a 404 so callers can tell "no such asset" from a network failure.
67fetch() {
68 if command -v curl >/dev/null 2>&1; then
69 status="$(curl -sSL -H "Accept: ${3:-*/*}" -w '%{http_code}' "$1" -o "$2")" || return 1
70 case "$status" in
71 2??) return 0 ;;
72 404) return 44 ;;
73 *) printf 'GET %s returned HTTP %s\n' "$1" "$status" >&2; return 1 ;;
74 esac
75 elif command -v wget >/dev/null 2>&1; then
76 wget -q --server-response --header="Accept: ${3:-*/*}" "$1" -O "$2" 2>"$2.headers" && rm -f "$2.headers" && return 0
77 if grep -q ' 404 ' "$2.headers" 2>/dev/null; then rm -f "$2.headers"; return 44; fi
78 cat "$2.headers" >&2; rm -f "$2.headers"; return 1
79 else
80 fail "curl or wget is required"
81 fi
82}
83
84mb() {
85 tenths=$((($1 * 10 + 524288) / 1048576))
86 printf '%s.%s' "$((tenths / 10))" "$((tenths % 10))"
87}
88# Poll the file written by the downloader; no progress-output parsing or extra request.
89download() {
90 if ! "$interactive"; then fetch "$1" "$2"; return; fi
91 if command -v curl >/dev/null 2>&1; then
92 curl -fsSL -D "$2.headers" "$1" -o "$2" 2>"$2.errors" &
93 else
94 wget -q --server-response "$1" -O "$2" 2>"$2.headers" &
95 fi
96 download_pid=$!
97 previous=-1
98 cr="$(printf '\r')"
99 while kill -0 "$download_pid" 2>/dev/null; do
100 bytes=0; total=0
101 if [ -f "$2" ]; then bytes="$(wc -c < "$2")"; fi
102 if [ -f "$2.headers" ]; then
103 while read -r key value; do
104 case "$key" in
105 HTTP/*) total=0 ;;
106 [Cc]ontent-[Ll]ength:) total="${value%"$cr"}" ;;
107 esac
108 done < "$2.headers"
109 fi
110 case "$total" in ''|*[!0-9]*) total=0 ;; esac
111 if [ "$bytes" -ne "$previous" ]; then
112 if [ "$total" -gt 0 ]; then
113 percent=$((bytes * 100 / total)); [ "$percent" -le 100 ] || percent=100
114 filled=$((percent * 32 / 100)); bar=; rest=; n=0
115 while [ "$n" -lt 32 ]; do
116 if [ "$n" -lt "$filled" ]; then bar="${bar}■"; else rest="${rest}·"; fi
117 n=$((n + 1))
118 done
119 printf '\r\033[2K %s%s%s%s%s %3d%% %s%s / %s MB%s' "$accent" "$bar" "$reset$muted" "$rest" "$reset" "$percent" "$muted" "$(mb "$bytes")" "$(mb "$total")" "$reset" >&2
120 else
121 printf '\r\033[2K %sDownloading%s %s MB' "$muted" "$reset" "$(mb "$bytes")" >&2
122 fi
123 previous="$bytes"
124 fi
125 sleep 0.1
126 done
127 result=0; wait "$download_pid" || result=$?
128 download_pid=
129 if [ "$result" -ne 0 ]; then
130 printf '\n' >&2
131 if [ -f "$2.errors" ]; then cat "$2.errors" >&2; else cat "$2.headers" >&2; fi
132 return "$result"
133 fi
134 size="$(mb "$(wc -c < "$2")")"
135 printf '\r\033[2K %s■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■%s 100%% %s%s / %s MB%s\n' "$accent" "$reset" "$muted" "$size" "$size" "$reset" >&2
136 rm -f "$2.headers" "$2.errors"
137}
138
139case "$(uname -s)" in
140 Darwin) platform="darwin" ;;
141 Linux) platform="linux" ;;
142 *) fail "unsupported operating system $(uname -s); use the desktop app or npm" ;;
143esac
144case "$(uname -m)" in
145 arm64 | aarch64) arch="arm64" ;;
146 x86_64 | amd64) arch="x64" ;;
147 *) fail "unsupported architecture $(uname -m)" ;;
148esac
149if [ "${platform}-${arch}" = darwin-x64 ]; then
150 fail "there is no t3 CLI for Intel Macs. Install the desktop app from https://t3.codes/download, or run a server from source: https://github.com/${repo}/blob/main/docs/user/install.md#intel-macs"
151fi
152command -v tar >/dev/null 2>&1 || fail "tar is required"
153if command -v sha256sum >/dev/null 2>&1; then
154 checksum() { sha256sum < "$1" | cut -d' ' -f1; }
155elif command -v shasum >/dev/null 2>&1; then
156 checksum() { shasum -a 256 < "$1" | cut -d' ' -f1; }
157else
158 fail "sha256sum or shasum is required"
159fi
160
161channel="${T3CODE_CHANNEL:-stable}"
162version="${T3CODE_VERSION:-}"
163if [ -z "$version" ]; then
164 case "$channel" in
165 stable) tag_pattern='v\([0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\)' ;;
166 nightly | preview) tag_pattern="v\([0-9][^\"]*-${channel}\.[0-9]*\.[0-9]*\)" ;;
167 *) fail "T3CODE_CHANNEL must be stable, nightly, or preview" ;;
168 esac
169 tmp_index="$(mktemp)"
170 if [ "$channel" = stable ]; then
171 # release.yml marks only plain stable releases latest. This is a github.com
172 # page, so it is not cut off by a page of newer nightlies or the API quota.
173 fetch "https://github.com/${repo}/releases/latest" "$tmp_index" application/json
174 version="$(sed -n "s/.*\"tag_name\": *\"${tag_pattern}\".*/\1/p" "$tmp_index" | head -n 1)"
175 else
176 page=1
177 while [ -z "$version" ] && [ "$page" -le 10 ]; do
178 fetch "https://api.github.com/repos/${repo}/releases?per_page=100&page=${page}" "$tmp_index"
179 version="$(sed -n "s/.*\"tag_name\": *\"${tag_pattern}\".*/\1/p" "$tmp_index" | head -n 1)"
180 grep -q '"tag_name"' "$tmp_index" || break
181 page=$((page + 1))
182 done
183 fi
184 rm -f "$tmp_index"
185 [ -n "$version" ] || fail "could not find a ${channel} release; set T3CODE_VERSION"
186fi
187case "$version" in
188 *-preview.*)
189 printf '%s\n' \
190 "t3 ${version} is a preview build." \
191 " Preview builds are cut by maintainers from unreleased branches to exercise the release" \
192 " pipeline. They can be broken, receive no fixes, and are never offered as updates." \
193 " Set T3CODE_CHANNEL=stable (the default) for a supported build." >&2
194 if [ "$channel" != "preview" ] && [ -z "${T3CODE_VERSION:-}" ]; then
195 fail "refusing a preview build that was not explicitly requested"
196 fi
197 ;;
198esac
199
200stem="t3-${version}-${platform}-${arch}"
201archive="${stem}.tar.gz"
202versions_dir="${t3_home}/runtime/versions"
203target_dir="${versions_dir}/${version}"
204
205if [ -f "${target_dir}/.install-complete" ] && [ "$(cat "${target_dir}/.install-complete")" = "$version" ]; then
206 step "Version ${version} is already downloaded."
207else
208 mkdir -p "$versions_dir"
209 staging="$(mktemp -d "${versions_dir}/.staging-XXXXXX")"
210 download_pid=
211 trap '[ -z "$download_pid" ] || { kill "$download_pid" 2>/dev/null || true; wait "$download_pid" 2>/dev/null || true; }; rm -rf "$staging"' EXIT
212 trap 'printf "\n" >&2; exit 130' INT
213 trap 'printf "\n" >&2; exit 143' TERM
214
215 if "$interactive"; then printf '\r\033[2K' >&2; fi
216 printf ' %sInstalling%s T3 Code %s%s%s\n\n' "$muted" "$reset" "$bold" "$version" "$reset" >&2
217 step "Downloading..."
218 fetch_status=0
219 fetch "${base_url}/v${version}/SHA256SUMS" "${staging}/SHA256SUMS" || fetch_status=$?
220 if [ "$fetch_status" -eq 44 ]; then
221 fail "t3 ${version} has no release archive for ${platform}-${arch}; releases before the self-contained CLI can only be installed with \`npm install -g t3@${version}\`"
222 elif [ "$fetch_status" -ne 0 ]; then
223 fail "could not download the release checksums"
224 fi
225 expected="$(grep " \*\{0,1\}${archive}\$" "${staging}/SHA256SUMS" | cut -d' ' -f1)"
226 [ -n "$expected" ] || fail "t3 ${version} has no release archive for ${platform}-${arch}"
227 download "${base_url}/v${version}/${archive}" "${staging}/${archive}"
228
229 step "Verifying the download..."
230 actual="$(checksum "${staging}/${archive}")"
231 [ "$actual" = "$expected" ] || fail "checksum mismatch for ${archive}"
232
233 step "Extracting T3 Code..."
234 tar -xzf "${staging}/${archive}" -C "$staging" --strip-components=1
235 rm -f "${staging}/${archive}" "${staging}/SHA256SUMS"
236 "${staging}/t3" --version >/dev/null || fail "the downloaded executable does not run"
237 printf '%s\n' "$version" > "${staging}/.install-complete"
238
239 rm -rf "$target_dir"
240 mv "$staging" "$target_dir"
241 trap - EXIT
242fi
243
244step "Setting up the t3 command..."
245mkdir -p "$bin_dir"
246# Replace only this home's own t3 links: into its versions, or the desktop app's launcher.
247# Anything that is not a symlink (a script, a copied binary) belongs to someone else.
248# Exactly <versions>/<version>/t3 ($1 under $2), never another program inside a version.
249version_t3() {
250 case "$1" in "$2"/*) ;; *) return 1 ;; esac
251 case "${1#"$2"/}" in */*/*) return 1 ;; */t3) return 0 ;; *) return 1 ;; esac
252}
253slot_empty=true
254if [ -e "${bin_dir}/t3" ] || [ -L "${bin_dir}/t3" ]; then
255 slot_empty=false
256 owned=false
257 if [ -L "${bin_dir}/t3" ]; then
258 existing="$(readlink "${bin_dir}/t3" 2>/dev/null || true)"
259 case "$existing" in /*) ;; *) existing="${bin_dir}/${existing}" ;; esac
260 real_versions_dir="$(CDPATH='' cd -P -- "$versions_dir" && pwd)"
261 # Through a symlinked T3 home the link text names the alias; resolve its folder to compare.
262 existing_dir="$(CDPATH='' cd -P -- "$(dirname -- "$existing")" 2>/dev/null && pwd || true)"
263 # The text alone counts only without . or .. segments (a pruned version leaves a dangling link).
264 case "$existing" in
265 */./* | */../* | */. | */..) ;;
266 *)
267 if version_t3 "$existing" "$versions_dir" || version_t3 "$existing" "$real_versions_dir" ||
268 [ "$existing" = "${t3_home}/bin/t3" ]; then owned=true; fi
269 ;;
270 esac
271 if [ -n "$existing_dir" ] && version_t3 "${existing_dir}/${existing##*/}" "$real_versions_dir"; then
272 owned=true
273 fi
274 fi
275 "$owned" || fail "another t3 is already at ${bin_dir}/t3. Remove it and run this again, or run ${target_dir}/t3 directly."
276fi
277# An empty spot is linked without -f, so a file that appears after the check above is kept.
278if "$slot_empty"; then
279 ln -s "${target_dir}/t3" "${bin_dir}/t3" || fail "could not link ${bin_dir}/t3; something else was created there."
280else
281 # Known race, accepted: a file written here after the ownership check above is overwritten.
282 ln -sfn "${target_dir}/t3" "${bin_dir}/t3"
283fi
284# Pruning keeps what this launcher points at; a background service cannot see bin_dir otherwise.
285launcher="$(CDPATH='' cd -P -- "$bin_dir" && pwd)/t3"
286launchers="${t3_home}/runtime/launchers"
287grep -qxF "$launcher" "$launchers" 2>/dev/null || printf '%s\n' "$launcher" >> "$launchers"
288if "$interactive"; then printf '\r\033[2K' >&2; fi
289printf ' %sInstalled T3 Code %s%s\n\n' "$green" "$version" "$reset" >&2
290# The folder a shell reads its startup file from: $1 when set and safe to paste, else $2 when
291# unset. A set but unsafe folder prints nothing, since the default would never be read.
292read_from() {
293 case "$1" in
294 "") printf '%s' "$2" ;;
295 /*) case "$1" in *[!A-Za-z0-9_./-]*) ;; *) printf '%s' "$1" ;; esac ;;
296 esac
297}
298# A command to paste for zsh, Linux bash, or fish, like `t3 install` prints. macOS bash reads only
299# one of several startup files, so no single file is safe to change there.
300path_command=
301path_rest="$bin_dir"
302path_prefix=
303case "$bin_dir" in "$HOME"/*) path_rest="${bin_dir#"$HOME"}" path_prefix='$HOME' ;; esac
304case "$path_rest" in *[!A-Za-z0-9_./-]*) path_rest= ;; esac
305if [ -n "$path_rest" ]; then
306 # Last on PATH, so it never shadows another t3.
307 path_line="export PATH=\"\$PATH:${path_prefix}${path_rest}\""
308 shell_name="${SHELL:-}"
309 case "${shell_name##*/}" in
310 zsh)
311 # zsh reads .zshrc from ZDOTDIR when it is set; ~/.zshrc would never be read then.
312 zsh_dir="$(read_from "${ZDOTDIR:-}" '~')"
313 [ -z "$zsh_dir" ] || path_command="printf '\\n%s\\n' '${path_line}' >> ${zsh_dir}/.zshrc" ;;
314 bash) [ "$platform" = darwin ] || path_command="printf '\\n%s\\n' '${path_line}' >> ~/.bashrc" ;;
315 fish)
316 # --path changes $PATH itself; fish_user_paths would go ahead of the system folders.
317 config_dir="$(read_from "${XDG_CONFIG_HOME:-}" '~/.config')"
318 fish_dir="${config_dir}/fish"
319 [ -z "$config_dir" ] || path_command="mkdir -p ${fish_dir} && printf '\\n%s\\n' 'fish_add_path --append --path ${path_prefix}${path_rest}' >> ${fish_dir}/config.fish" ;;
320 esac
321fi
322# A t3 earlier on PATH runs instead; the PATH command adds this folder last.
323# A PATH entry with a trailing slash gives `dir//t3`.
324first_t3="$(command -v t3 2>/dev/null | tr -s / || true)"
325case "$first_t3" in "" | "${bin_dir}/t3") first_t3= ;; esac
326if [ -n "$first_t3" ]; then
327 printf ' Another t3 at %s runs first, so run %s%s/t3%s to use this one.\n\n' "$first_t3" "$bold" "$bin_dir" "$reset"
328else
329 case ":${PATH}:" in
330 *":${bin_dir}:"* | *":${bin_dir}/:"*) printf ' Run %st3%s to use T3 Code here, or %st3 up%s to use it from your other devices.\n\n' "$bold" "$reset" "$bold" "$reset" ;;
331 *)
332 if [ -n "$path_command" ]; then
333 printf ' %s is not on your PATH yet. Add it with:\n\n %s\n\n Then open a new terminal and run %st3%s.\n\n' "$bin_dir" "$path_command" "$bold" "$reset"
334 else
335 printf ' Add %s to your PATH, then run %st3%s.\n\n' "$bin_dir" "$bold" "$reset"
336 fi
337 ;;
338 esac
339fi