#!/bin/sh # Hclaro installer (macOS, Linux, and Windows from Git Bash, MSYS2 or Cygwin). # # curl -fsSL https://get.hclaro.dev | sh # # Always write the full https:// URL: without a scheme curl may start on port 80 and follow a # redirect a network attacker controls. To read the script first, download it, then run it. # # Environment: # HCLARO_VERSION version to install, e.g. v0.1.0-beta.5 (default: newest published release) # INSTALL_DIR absolute install folder (default: /usr/local/bin; $HOME/.local/bin on Windows) # # It downloads the release archive and checksums file, checks the sha256, and installs the # binary atomically. It asks for sudo only to write an INSTALL_DIR you cannot write, and runs # nothing but `hclaro --version`. # # HCLARO_RELEASES_URL, HCLARO_API_URL and HCLARO_LATEST_URL only exist so tests can point at local files. # Everything lives in main, called on the last line, so a truncated download runs nothing. main() { set -eu releases="${HCLARO_RELEASES_URL:-https://github.com/botopsy/hclaro/releases/download}" api="${HCLARO_API_URL:-https://api.github.com/repos/botopsy/hclaro/releases?per_page=10}" latest="${HCLARO_LATEST_URL:-https://github.com/botopsy/hclaro/releases/latest}" ext=tar.gz exe=hclaro case "$(uname -s)" in Darwin) os=darwin ;; Linux) os=linux ;; MINGW* | MSYS* | CYGWIN*) os=windows ext=zip exe=hclaro.exe ;; *) die "unsupported OS $(uname -s)" ;; esac dir="${INSTALL_DIR:-/usr/local/bin}" [ "$os" != windows ] || dir="${INSTALL_DIR:-$HOME/.local/bin}" case "$(uname -m)" in x86_64 | amd64) arch=amd64 ;; arm64 | aarch64) arch=arm64 ;; *) die "unsupported CPU $(uname -m); releases cover amd64 and arm64" ;; esac [ "$os$arch" != windowsarm64 ] || die "no Windows arm64 release; only windows/amd64" case "$dir" in /*) ;; *) die "INSTALL_DIR must be an absolute path, got '$dir'" ;; esac if command -v curl >/dev/null 2>&1; then fetch() { curl -fsSL "$1" -o "$2"; } elif command -v wget >/dev/null 2>&1; then fetch() { wget -qO "$2" "$1"; } else die "curl or wget is required" fi final_url() { command -v curl >/dev/null 2>&1 && curl -fsSL -o /dev/null -w '%{url_effective}' "$1"; } tmp="$(mktemp -d)" trap 'rm -rf "$tmp"' EXIT INT TERM tag="${HCLARO_VERSION:-}" if [ -z "$tag" ]; then # The web redirect has no API rate limit but skips pre-releases (all Hclaro releases so # far); when it resolves no tag, fall back to the API, which lists newest first without drafts. final="$(final_url "$latest" 2>/dev/null || true)" case "$final" in */releases/tag/*) tag="${final##*/}" ;; esac if [ -z "$tag" ]; then fetch "$api" "$tmp/releases.json" || die "could not list releases" tag="$(grep -o '"tag_name": *"[^"]*"' "$tmp/releases.json" | head -n 1 | sed 's/.*: *"\(.*\)"/\1/')" fi [ -n "$tag" ] || die "no published release found" fi case "$tag" in v*) ;; *) tag="v$tag" ;; esac # Reject anything that could change the download path or the file name. printf '%s\n' "$tag" | grep -Eq '^v[0-9]+\.[0-9]+\.[0-9]+(-[0-9A-Za-z.-]+)?$' || die "invalid version '$tag'" ver="${tag#v}" name="hclaro_${ver}_${os}_${arch}" sums="hclaro_${ver}_checksums.txt" say "Installing hclaro $ver ($os/$arch) to $dir" fetch "$releases/$tag/$name.$ext" "$tmp/$name.$ext" || die "download failed: $name.$ext (does $tag exist?)" fetch "$releases/$tag/$sums" "$tmp/$sums" || die "download failed: $sums" want="$(awk -v f="$name.$ext" '{ sub(/\r$/, "", $2) } $2 == f || $2 == "*" f { print tolower($1); exit }' "$tmp/$sums")" [ -n "$want" ] || die "$name.$ext is not in $sums; nothing installed" if command -v sha256sum >/dev/null 2>&1; then got="$(sha256sum "$tmp/$name.$ext" | awk '{ print $1 }')" elif command -v shasum >/dev/null 2>&1; then got="$(shasum -a 256 "$tmp/$name.$ext" | awk '{ print $1 }')" elif command -v openssl >/dev/null 2>&1; then got="$(openssl dgst -sha256 "$tmp/$name.$ext" | awk '{ print $NF }')" else die "sha256sum, shasum or openssl is required to verify the download" fi [ "$got" = "$want" ] || die "checksum mismatch for $name.$ext; nothing installed" if [ "$ext" = zip ]; then # Git Bash has no unzip; Windows' own bsdtar reads zip archives. { unzip -q "$tmp/$name.zip" -d "$tmp" 2>/dev/null || tar -xf "$tmp/$name.zip" -C "$tmp"; } || die "could not extract the archive (need unzip or a zip-capable tar)" else tar -xzf "$tmp/$name.$ext" -C "$tmp" "$name/$exe" || die "could not extract the archive" fi sudo="" if ! { [ -d "$dir" ] || mkdir -p "$dir" 2>/dev/null; } || [ ! -w "$dir" ]; then { [ "$os" != windows ] && command -v sudo >/dev/null 2>&1; } || die "$dir is not writable and sudo is not available; try INSTALL_DIR=\$HOME/.local/bin" say "Installing to $dir (requires sudo)..." sudo=sudo $sudo mkdir -p "$dir" || die "could not create $dir" fi # Copy next to the target, then rename: a failure never leaves a half-written binary. stage="$dir/.$exe.$$" $sudo cp "$tmp/$name/$exe" "$stage" && $sudo chmod 755 "$stage" && $sudo mv -f "$stage" "$dir/$exe" || { $sudo rm -f "$stage"; die "could not write $dir/$exe"; } "$dir/$exe" --version case ":$PATH:" in *":$dir:"*) ;; *) say "Note: $dir is not on your PATH. Add it, or run $dir/$exe." ;; esac say "Next: run 'hclaro' in a Terraform or OpenTofu project. Hclaro uses your installed engine." } say() { printf '%s\n' "$*"; } die() { printf 'hclaro install: %s\n' "$*" >&2; exit 1; } main "$@"