#!/bin/sh # Product Codebases CLI installer (macOS and Linux). # # curl -fsSL https://octetdata.net/codebases/install.sh | sh # curl -fsSL https://octetdata.net/codebases/install.sh | sh -s -- --dir "$HOME/bin" # # What it does: picks the build for your OS and CPU, downloads it and SHA256SUMS from # https://github.com/afisherdev/octet-codebases-releases, checks the SHA-256, and copies the binary to # ~/.local/bin (or --dir / OCTET_CODEBASES_INSTALL_DIR). It refuses to install if the checksum doesn't # match. It doesn't edit your shell profile and doesn't need sudo. # # Options (environment): # OCTET_CODEBASES_INSTALL_DIR where to put the binary (default ~/.local/bin) # OCTET_CODEBASES_VERSION a release tag such as v0.3.0 (default: the latest release) # # The CLI makes no network calls while it analyses a repository. set -eu REPO="afisherdev/octet-codebases-releases" BIN="octet-codebases" say() { printf '%s\n' "$*"; } fail() { printf 'octet-codebases install: %s\n' "$*" >&2; exit 1; } # asset_for : the release asset name, or nothing if unsupported. asset_for() { case "$1" in Darwin) os=darwin ;; Linux) os=linux ;; *) return 1 ;; esac case "$2" in x86_64 | amd64) arch=x64 ;; arm64 | aarch64 | armv8* | arm64e) arch=arm64 ;; *) return 1 ;; esac printf '%s-%s-%s\n' "$BIN" "$os" "$arch" } # download_url download_url() { if [ -n "${OCTET_CODEBASES_DOWNLOAD_URL:-}" ]; then printf '%s/%s\n' "${OCTET_CODEBASES_DOWNLOAD_URL%/}" "$1" elif [ -n "${OCTET_CODEBASES_VERSION:-}" ]; then printf 'https://github.com/%s/releases/download/%s/%s\n' "$REPO" "$OCTET_CODEBASES_VERSION" "$1" else printf 'https://github.com/%s/releases/latest/download/%s\n' "$REPO" "$1" fi } fetch() { if command -v curl >/dev/null 2>&1; then curl -fsSL --proto '=https,file' -o "$2" "$1" elif command -v wget >/dev/null 2>&1; then wget -q -O "$2" "$1" else fail "needs curl or wget." fi } sha256_of() { if command -v sha256sum >/dev/null 2>&1; then sha256sum "$1" | cut -d' ' -f1 elif command -v shasum >/dev/null 2>&1; then shasum -a 256 "$1" | cut -d' ' -f1 elif command -v openssl >/dev/null 2>&1; then openssl dgst -sha256 "$1" | sed 's/^.*= *//' else fail "needs sha256sum, shasum or openssl to check the download." fi } main() { dir="${OCTET_CODEBASES_INSTALL_DIR:-$HOME/.local/bin}" while [ $# -gt 0 ]; do case "$1" in --dir) [ $# -ge 2 ] || fail "--dir needs a folder."; dir="$2"; shift 2 ;; --dir=*) dir="${1#--dir=}"; shift ;; -h | --help) say "Usage: install.sh [--dir ]"; exit 0 ;; *) fail "unknown option $1" ;; esac done s="$(uname -s)" m="$(uname -m)" # An Intel shell on Apple silicon (Rosetta): use the native build. if [ "$s" = Darwin ] && [ "$m" = x86_64 ] && [ "$(sysctl -n sysctl.proc_translated 2>/dev/null || echo 0)" = 1 ]; then m=arm64; fi case "$s" in MINGW* | MSYS* | CYGWIN*) fail "on Windows, use PowerShell: irm https://octetdata.net/codebases/install.ps1 | iex" ;; esac asset="$(asset_for "$s" "$m")" || fail "no build for $s $m. Builds exist for macOS and Linux (x64, arm64) and Windows x64." tmp="$(mktemp -d 2>/dev/null || mktemp -d -t octet-codebases)" trap 'rm -rf "$tmp"' EXIT INT TERM say "Downloading $asset" fetch "$(download_url "$asset")" "$tmp/$asset" || fail "download failed: $(download_url "$asset")" fetch "$(download_url SHA256SUMS)" "$tmp/SHA256SUMS" || fail "couldn't download SHA256SUMS." expected="$(awk -v a="$asset" '$2 == a || $2 == "*" a { print $1 }' "$tmp/SHA256SUMS")" [ -n "$expected" ] || fail "SHA256SUMS has no entry for $asset. Nothing was installed." actual="$(sha256_of "$tmp/$asset")" if [ "$expected" != "$actual" ]; then fail "checksum mismatch for $asset (expected $expected, got $actual). Nothing was installed." fi say "Checksum OK ($actual)" mkdir -p "$dir" chmod 755 "$tmp/$asset" mv -f "$tmp/$asset" "$dir/$BIN" say "Installed $BIN to $dir/$BIN" "$dir/$BIN" --version >/dev/null 2>&1 || say "Warning: $dir/$BIN didn't start. On macOS, see the install page for Gatekeeper help." say "" case ":$PATH:" in *":$dir:"*) ;; *) say "$dir isn't on your PATH. Add this line to your shell profile:"; say " export PATH=\"$dir:\$PATH\""; say "" ;; esac say "Next steps:" say " cd your-repo" say " $BIN . # local analysis; no network calls" say " $BIN --json # machine-readable report" say " $BIN submit # optional: open the submit page" say "Product Codebases only licenses private repositories. Estimates are illustrative, not offers." } # Sourcing with OCTET_CODEBASES_SOURCE_ONLY=1 loads the functions without installing (used by tests). # main runs last, so a partly downloaded script does nothing. if [ "${OCTET_CODEBASES_SOURCE_ONLY:-}" != 1 ]; then main "$@"; fi