github.com/MetalBlockchain/metalgo@v1.11.9/scripts/shellcheck.sh (about)

     1  #!/usr/bin/env bash
     2  
     3  set -euo pipefail
     4  
     5  # This script can also be used to correct the problems detected by shellcheck by invoking as follows:
     6  #
     7  # ./scripts/tests.shellcheck.sh -f diff | git apply
     8  #
     9  
    10  if ! [[ "$0" =~ scripts/shellcheck.sh ]]; then
    11    echo "must be run from repository root"
    12    exit 255
    13  fi
    14  
    15  VERSION="v0.9.0"
    16  
    17  function get_version {
    18    local target_path=$1
    19    if command -v "${target_path}" > /dev/null; then
    20      echo "v$("${target_path}" --version | grep version: | awk '{print $2}')"
    21    fi
    22  }
    23  
    24  SYSTEM_VERSION="$(get_version shellcheck)"
    25  if [[ "${SYSTEM_VERSION}" == "${VERSION}" ]]; then
    26    SHELLCHECK=shellcheck
    27  else
    28    # Try to install a local version
    29    SHELLCHECK=./bin/shellcheck
    30    LOCAL_VERSION="$(get_version "${SHELLCHECK}")"
    31    if [[ -z "${LOCAL_VERSION}" || "${LOCAL_VERSION}" != "${VERSION}" ]]; then
    32      if which sw_vers &> /dev/null; then
    33        echo "on macos, only x86_64 binaries are available so rosetta is required"
    34        echo "to avoid using rosetta, install via homebrew: brew install shellcheck"
    35        DIST=darwin.x86_64
    36      else
    37        # Linux - binaries for common arches *should* be available
    38        arch="$(uname -i)"
    39        DIST="linux.${arch}"
    40      fi
    41      curl -s -L "https://github.com/koalaman/shellcheck/releases/download/${VERSION}/shellcheck-${VERSION}.${DIST}.tar.xz" | tar Jxv -C /tmp > /dev/null
    42      mkdir -p "$(dirname "${SHELLCHECK}")"
    43      cp /tmp/shellcheck-"${VERSION}"/shellcheck "${SHELLCHECK}"
    44    fi
    45  fi
    46  
    47  # `find *` is the simplest way to ensure find does not include a
    48  # leading `.` in filenames it emits. A leading `.` will prevent the
    49  # use of `git apply` to fix reported shellcheck issues. This is
    50  # compatible with both macos and linux (unlike the use of -printf).
    51  #
    52  # shellcheck disable=SC2035
    53  find * -name "*.sh" -type f -print0 | xargs -0 "${SHELLCHECK}" "${@}"