Skip to content

Update hack/verify-shellcheck.sh #1884

@guettli

Description

@guettli

/kind feature

I think our hack/verify-shellcheck.sh is outdated. We could use something like this:

#!/usr/bin/env bash

# TODO: Add Boilerplate

# Bash Strict Mode: https://github.com/guettli/bash-strict-mode
trap 'echo -e "\n🤷 🚨 🔥 Warning: A command has failed. Exiting the script. Line was ($0:$LINENO): $(sed -n "${LINENO}p" "$0" 2>/dev/null || true) 🔥 🚨 🤷 "; exit 3' ERR
set -Eeuo pipefail

# https://github.com/koalaman/shellcheck/releases
SHELLCHECK_VERSION="v0.11.0"

# disabled lints
disabled=(
  # this lint disallows non-constant source, which we use extensively without
  # any known bugs
  1090
  # this lint prefers command -v to which, they are not the same
  2230

  1091
)

OS="unknown"
if [[ "${OSTYPE}" == "linux"* ]]; then
  OS="linux"
elif [[ "${OSTYPE}" == "darwin"* ]]; then
  OS="darwin"
fi

# comma separate for passing to shellcheck
join_by() {
  local IFS="$1"
  shift
  echo "$*"
}
# shellcheck source=./hack/utils.sh
# shellcheck disable=SC1091
source "$(dirname "$0")/lib/utils.sh"
ROOT_PATH=$(get_root_path)

# create a temporary directory
TMP_DIR=$(mktemp -d)
OUT="${TMP_DIR}/out.log"

SHELLCHECK_DISABLED="$(join_by , "${disabled[@]}")"
readonly SHELLCHECK_DISABLED

SHELLCHECK="./$(dirname "$0")/tools/bin/shellcheck/${SHELLCHECK_VERSION}/shellcheck"

if [ ! -f "$SHELLCHECK" ]; then
  # install buildifier
  cd "${TMP_DIR}" || exit
  DOWNLOAD_FILE="shellcheck-${SHELLCHECK_VERSION}.${OS}.x86_64.tar.xz"
  curl -L "https://github.com/koalaman/shellcheck/releases/download/${SHELLCHECK_VERSION}/${DOWNLOAD_FILE}" -o "${TMP_DIR}/shellcheck.tar.xz"
  tar xf "${TMP_DIR}/shellcheck.tar.xz"
  cd "${ROOT_PATH}"
  mkdir -p "$(dirname "$0")/tools/bin/shellcheck/${SHELLCHECK_VERSION}"
  mv "${TMP_DIR}/shellcheck-${SHELLCHECK_VERSION}/shellcheck" "$SHELLCHECK"
fi

echo "Running shellcheck..."
cd "${ROOT_PATH}" || exit
ALL_SH_FILES="${TMP_DIR}/all-sh-files.txt"
OUR_SH_FILES="${TMP_DIR}/filtered-sh-files.txt"
EXCLUDED_SH_FILES="${TMP_DIR}/excluded-sh-files.txt"
EXCLUDED_FILE_PATTERN='/vendor/|hack/version.sh|/node_modules/'

find . -type f -name "*.sh" | sort >"${ALL_SH_FILES}"

grep -P -v "${EXCLUDED_FILE_PATTERN}" "${ALL_SH_FILES}" >"${OUR_SH_FILES}" || true

# Set operation: All files, minus our sh files.
comm -23 "${ALL_SH_FILES}" "${OUR_SH_FILES}" >"${EXCLUDED_SH_FILES}"

#if [[ -s "${EXCLUDED_SH_FILES}" ]]; then
#  echo "$(pwd) Ignoring shellcheck on:"
#  cat "${EXCLUDED_SH_FILES}"
#fi

while read -r file; do
  "$SHELLCHECK" -x "--exclude=${SHELLCHECK_DISABLED}" "--color=auto" "$file" >>"${OUT}" 2>&1 || true
done <"${OUR_SH_FILES}"

if [[ -s "${OUT}" ]]; then
  echo "Found errors:"
  cat "${OUT}"
  echo
  echo 'Please review the above warnings. You can test via "./hack/verify-shellcheck.sh"'
  echo 'If the above warnings do not make sense, you can exempt this warning with a comment'
  echo ' (if your reviewer is okay with it).'
  echo 'In general please prefer to fix the error, we have already disabled specific lints.'
  echo 'See: https://github.com/koalaman/shellcheck/wiki/Ignore#ignoring-one-specific-instance-in-a-file'
  echo
  exit 1
fi
echo 'Congratulations! All shell files are passing lint :-)'

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions