|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +set -euo pipefail |
| 4 | + |
| 5 | +install_dir=${INSTALL_DIR:-/usr/local/bin} |
| 6 | + |
| 7 | +mkdir -p "$install_dir" |
| 8 | + |
| 9 | +fetch_latest_tag() { |
| 10 | + local repo="$1" |
| 11 | + curl -fsSL "https://api.github.com/repos/${repo}/releases/latest" \ |
| 12 | + | awk -F '"' '/"tag_name"/ { print $4; exit }' |
| 13 | +} |
| 14 | + |
| 15 | +install_shellcheck() { |
| 16 | + local tag |
| 17 | + local arch |
| 18 | + local tarball |
| 19 | + local tmp_dir |
| 20 | + |
| 21 | + tag=$(fetch_latest_tag "koalaman/shellcheck") |
| 22 | + if [[ -z "$tag" ]]; then |
| 23 | + return 1 |
| 24 | + fi |
| 25 | + arch=$(uname -m) |
| 26 | + case "$arch" in |
| 27 | + x86_64) arch="x86_64" ;; |
| 28 | + aarch64|arm64) arch="aarch64" ;; |
| 29 | + *) |
| 30 | + echo "Unsupported architecture for shellcheck: $arch" >&2 |
| 31 | + exit 1 |
| 32 | + ;; |
| 33 | + esac |
| 34 | + |
| 35 | + tarball="shellcheck-${tag}.linux.${arch}.tar.xz" |
| 36 | + tmp_dir=$(mktemp -d) |
| 37 | + if ! curl -fsSL -o "${tmp_dir}/${tarball}" \ |
| 38 | + "https://github.com/koalaman/shellcheck/releases/download/${tag}/${tarball}"; then |
| 39 | + rm -rf "$tmp_dir" |
| 40 | + return 1 |
| 41 | + fi |
| 42 | + tar -xJf "${tmp_dir}/${tarball}" -C "$tmp_dir" |
| 43 | + mv "${tmp_dir}/shellcheck-${tag}/shellcheck" "${install_dir}/shellcheck" |
| 44 | + chmod +x "${install_dir}/shellcheck" |
| 45 | + rm -rf "$tmp_dir" |
| 46 | +} |
| 47 | + |
| 48 | +install_hadolint() { |
| 49 | + local tag |
| 50 | + local arch |
| 51 | + local os |
| 52 | + local binary |
| 53 | + |
| 54 | + tag=$(fetch_latest_tag "hadolint/hadolint") |
| 55 | + if [[ -z "$tag" ]]; then |
| 56 | + return 1 |
| 57 | + fi |
| 58 | + os="Linux" |
| 59 | + arch=$(uname -m) |
| 60 | + case "$arch" in |
| 61 | + x86_64) arch="x86_64" ;; |
| 62 | + aarch64|arm64) arch="arm64" ;; |
| 63 | + *) |
| 64 | + echo "Unsupported architecture for hadolint: $arch" >&2 |
| 65 | + exit 1 |
| 66 | + ;; |
| 67 | + esac |
| 68 | + |
| 69 | + binary="hadolint-${os}-${arch}" |
| 70 | + if ! curl -fsSL -o "${install_dir}/hadolint" \ |
| 71 | + "https://github.com/hadolint/hadolint/releases/download/${tag}/${binary}"; then |
| 72 | + return 1 |
| 73 | + fi |
| 74 | + chmod +x "${install_dir}/hadolint" |
| 75 | +} |
| 76 | + |
| 77 | +if ! install_shellcheck; then |
| 78 | + echo "Falling back to apt for shellcheck" >&2 |
| 79 | + apt-get update |
| 80 | + apt-get install -y shellcheck |
| 81 | +fi |
| 82 | + |
| 83 | +if ! install_hadolint; then |
| 84 | + echo "Falling back to apt for hadolint" >&2 |
| 85 | + apt-get update |
| 86 | + apt-get install -y hadolint |
| 87 | +fi |
| 88 | + |
| 89 | +shellcheck --version |
| 90 | +hadolint --version |
0 commit comments