#!/bin/sh # Installs cue, the official distribution of CUE, on Linux or macOS: # # curl -fsSL https://cue.dev/install.sh | sh # curl -fsSL https://cue.dev/install.sh | sh -s -- v0.18.0 # curl -fsSL https://cue.dev/install.sh | sh -s -- --dir "$HOME/bin" v0.18.0 # # It checks the archive against the release's checksum file, installs the # license notices beside cue as the macOS installer does, never uses sudo, # never edits shell profiles, and never replaces a cue that Homebrew or a # package manager installed. See https://cue.dev/docs/installing-cue/#install-script # # The whole script is one brace group, which the shell reads in full # before running any of it: a download cut off anywhere runs nothing. { set -eu umask 022 host='https://dl.cue.dev/' script='https://cue.dev/install.sh' page='https://cue.dev/docs/installing-cue/' releases='https://cue.dev/docs/releases/' agent='cue.dev-install.sh/1' proto=${host%%:*} # The archive's legal files, which the script installs in share/doc/cue # beside the install directory when the archive has them (research R13). legal='NOTICES.txt DISTRIBUTION-TERMS.txt' tmp= staged= staged_doc= say() { printf 'cue.dev install: %s\n' "$*" >&2 } # fail reports a failure at a step of the contract and exits 1. fail() { step=$1 shift printf 'cue.dev install: %s: %s\n' "$step" "$*" >&2 exit 1 } usage_error() { printf 'cue.dev install: usage: %s Run with --help for usage.\n' "$*" >&2 exit 2 } usage() { cat </dev/null || :)" = 1 ]; then arch=arm64 fi } # package_owner prints which package a database records $1 as part of, # or nothing. package_owner() { if command -v pkgutil >/dev/null 2>&1; then p=$(pkgutil --file-info "$1" 2>/dev/null | awk '/^pkgid: / { print $2; exit }') || p= if [ -n "$p" ]; then echo "the macOS package $p" return 0 fi fi if command -v dpkg-query >/dev/null 2>&1 && p=$(dpkg-query -S "$1" 2>/dev/null); then echo "the Debian package ${p%%:*}" return 0 fi if command -v rpm >/dev/null 2>&1 && p=$(rpm -qf "$1" 2>/dev/null); then echo "the RPM package $p" return 0 fi if command -v apk >/dev/null 2>&1 && p=$(apk info --who-owns "$1" 2>/dev/null) && [ -n "$p" ]; then echo "the Alpine package ${p##* }" return 0 fi return 0 } # check_ours stops if the file $1 belongs to another installation. check_ours() { other="Upgrade it the way it was installed, or choose another directory with --dir." if [ -L "$1" ]; then link=$(ls -l "$1" | awk '{ sub(/.* -> /, ""); print }') fail install "$1 is a link to $link, from another installation; nothing was installed. $other" fi if [ -e "$1" ] && ! [ -f "$1" ]; then fail install "$1 is not a regular file; nothing was installed. Remove it, or choose another directory with --dir." fi if [ -f "$1" ]; then owner=$(package_owner "$1") if [ -n "$owner" ]; then fail install "$1 belongs to $owner; nothing was installed. $other" fi fi } # choose_dir settles the install directory and the notices' directory, # and stops if either belongs to another installation (research R12, R13). choose_dir() { if [ -z "$dir" ]; then if [ -d /usr/local/bin ] && [ -w /usr/local/bin ] && ! [ -d /usr/local/bin/../Cellar ]; then dir=/usr/local/bin elif [ -n "${HOME:-}" ]; then dir=$HOME/.local/bin else fail install "HOME is not set, so there is no default directory to install cue in; nothing was installed. Choose one with --dir." fi fi if [ -d "$dir/../Cellar" ]; then fail install "$dir is Homebrew's; nothing was installed. Install cue with Homebrew (${page}#homebrew), or choose another directory with --dir." fi target=$dir/cue docdir=${dir%/*}/share/doc/cue check_ours "$target" for f in $legal; do check_ours "$docdir/$f" done for d in "$dir" "$docdir"; do if [ -d "$d" ] && ! [ -w "$d" ]; then fail install "cannot write to $d; nothing was installed. Choose another directory with --dir, or run this script with sudo sh." fi done } # install_legal installs the legal files extracted beside cue, each with # one rename, and lists them in docs. install_legal() { docs= for f in $legal; do if [ -f "$tmp/x/$f" ]; then docs="$docs $f" fi done if [ -z "$docs" ]; then return 0 fi mkdir -p "$docdir" 2>/dev/null || fail install "could not create $docdir for the license notices; $target is unchanged. Choose another directory with --dir." for f in $docs; do staged_doc=$docdir/.$f.$$.tmp { cp "$tmp/x/$f" "$staged_doc" && chmod 644 "$staged_doc" && mv -f "$staged_doc" "$docdir/$f"; } 2>/dev/null || fail install "could not write $docdir/$f; $target is unchanged. Choose another directory with --dir." staged_doc= done } # fetch downloads $1 to $2 and sets code to the HTTP status. It stops at # the download step if the host cannot be reached. fetch() { say "downloading $1" if ! code=$(curl -sSL --proto "=$proto" -A "$agent" -o "$2" -w '%{http_code}' "$1" 2>"$tmp/curl.err"); then reason=$(awk 'NF { sub(/^curl: \([0-9]+\) /, ""); line = $0 } END { print line }' "$tmp/curl.err") fail download "could not download $1: $reason; nothing was installed. Try again, or see $page" fi } # fetch_ok is fetch, stopping at the download step on any status but 200. fetch_ok() { fetch "$1" "$2" if [ "$code" != 200 ]; then fail download "$1 returned HTTP $code; nothing was installed. Try again, or see $page" fi } resolve_version() { if [ -n "$version" ]; then return 0 fi fetch "${host}latest.txt" "$tmp/latest.txt" if [ "$code" = 404 ]; then fail version "${host}latest.txt names no stable release yet; nothing was installed. Name a version from $releases" fi if [ "$code" != 200 ]; then fail download "${host}latest.txt returned HTTP $code; nothing was installed. Try again, or see $page" fi version=$(awk '{ gsub(/[ \t\r]/, ""); s = s $0 } END { print s }' "$tmp/latest.txt") if ! is_version "$version"; then fail version "${host}latest.txt does not name a version; nothing was installed. Name a version from $releases" fi } main() { parse_args "$@" trap cleanup EXIT trap 'exit 130' INT trap 'exit 143' TERM detect_platform choose_dir if command -v sha256sum >/dev/null 2>&1; then sha256() { sha256sum "$1" | awk '{ print $1 }'; } elif command -v shasum >/dev/null 2>&1; then sha256() { shasum -a 256 "$1" | awk '{ print $1 }'; } else fail check "neither sha256sum nor shasum is installed, so the download cannot be checked; nothing was installed. Install one, or see $page" fi if [ "$(id -u 2>/dev/null || :)" = 0 ]; then say "running as root" fi tmp=$(mktemp -d) || fail install "could not create a temporary directory; nothing was installed" resolve_version say "installing cue $version for $os/$arch in $dir" sums=cue_${version}_checksums.txt fetch "$host$version/$sums" "$tmp/$sums" if [ "$code" = 404 ]; then fail version "$version is not on ${host}; nothing was installed. See the releases at $releases (versions before v0.18.0 are at https://github.com/cue-lang/cue/releases)" fi if [ "$code" != 200 ]; then fail download "$host$version/$sums returned HTTP $code; nothing was installed. Try again, or see $page" fi archive=cue_${version}_${os}_${arch}.tar.gz sum=$(awk -v f="$archive" '$2 == f || $2 == "*" f { print $1; exit }' "$tmp/$sums") if [ -z "$sum" ]; then fail platform "$version has no archive for $os/$arch in $host$version/$sums; nothing was installed. See $page" fi fetch_ok "$host$version/$archive" "$tmp/$archive" if [ "$(sha256 "$tmp/$archive")" != "$sum" ]; then fail check "$archive does not match its checksum in $host$version/$sums; nothing was installed. See $page" fi mkdir "$tmp/x" tar -xzf "$tmp/$archive" -C "$tmp/x" cue 2>/dev/null || fail install "could not extract cue from $archive; nothing was installed. See $page" for f in $legal; do tar -xzf "$tmp/$archive" -C "$tmp/x" "$f" 2>/dev/null || : done mkdir -p "$dir" 2>/dev/null || fail install "could not create $dir; nothing was installed. Choose another directory with --dir." staged=$dir/.cue.$$.tmp { cp "$tmp/x/cue" "$staged" && chmod 755 "$staged"; } 2>/dev/null || fail install "could not write $staged; nothing was installed. Choose another directory with --dir." if ! out=$("$staged" version 2>&1); then out=$(printf '%s\n' "$out" | awk 'NR == 1') fail install "the new cue does not run on this machine ($out); $target is unchanged. See $page" fi install_legal if [ -e "$target" ]; then say "replacing $target" fi mv -f "$staged" "$target" || fail install "could not move $staged onto $target; $target is unchanged. See $page" staged= installed=$(printf '%s\n' "$out" | awk 'NR == 1 { print $3 }') say "installed cue ${installed:-$version} at $target" if [ -n "$docs" ]; then say "its license notices are in $docdir" fi case ":${PATH:-}:" in *":$dir:"*) ;; *) say "$dir is not on your PATH. Add it in your shell's profile, for example: export PATH=\"$dir:\$PATH\"" ;; esac first=$(command -v cue 2>/dev/null || :) if [ -n "$first" ] && [ "$first" != "$target" ]; then say "your shell runs $first first, which this script left alone; run $target, or put $dir earlier on your PATH" fi } main "$@" }