sigs.k8s.io/cluster-api-provider-azure@v1.17.0/hack/verify-shellcheck.sh (about)

     1  #!/usr/bin/env bash
     2  
     3  # Copyright 2018 The Kubernetes Authors.
     4  #
     5  # Licensed under the Apache License, Version 2.0 (the "License");
     6  # you may not use this file except in compliance with the License.
     7  # You may obtain a copy of the License at
     8  #
     9  #     http://www.apache.org/licenses/LICENSE-2.0
    10  #
    11  # Unless required by applicable law or agreed to in writing, software
    12  # distributed under the License is distributed on an "AS IS" BASIS,
    13  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  # See the License for the specific language governing permissions and
    15  # limitations under the License.
    16  
    17  # This script lints each shell script by `shellcheck`.
    18  # Usage: `hack/verify-shellcheck.sh`.
    19  # Taken from - https://github.com/kubernetes/kubernetes/blob/master/hack/verify-shellcheck.sh
    20  # modified according to use.
    21  
    22  set -o errexit
    23  set -o nounset
    24  set -o pipefail
    25  
    26  CAPZ_ROOT=$( cd "$(dirname "${BASH_SOURCE[0]}")/.." &> /dev/null && pwd )
    27  
    28  # allow overriding docker cli, which should work fine for this script
    29  DOCKER="${DOCKER:-docker}"
    30  SHELLCHECK_COLORIZED_OUTPUT="${SHELLCHECK_COLORIZED_OUTPUT:-auto}"
    31  
    32  # required version for this script, if not installed on the host we will
    33  # use the official docker image instead. keep this in sync with SHELLCHECK_IMAGE
    34  SHELLCHECK_VERSION="0.9.0"
    35  # upstream shellcheck latest stable image as of August 23, 2023.
    36  SHELLCHECK_IMAGE="docker.io/koalaman/shellcheck:v0.9.0@sha256:f35e8987b02760d4e76fc99a68ad5c42cc10bb32f3dd2143a3cf92f1e5446a45"
    37  
    38  # disabled lints
    39  disabled=(
    40    # this lint disallows non-constant source, which we use extensively without
    41    # any known bugs
    42    1090
    43    # this lint prefers command -v to which, they are not the same
    44    2230
    45  )
    46  
    47  # comma separate for passing to shellcheck
    48  join_by() {
    49    local IFS="$1";
    50    shift;
    51    echo "$*";
    52  }
    53  SHELLCHECK_DISABLED="$(join_by , "${disabled[@]}")"
    54  readonly SHELLCHECK_DISABLED
    55  
    56  # Ensure we're linting the correct source tree
    57  cd "${CAPZ_ROOT}"
    58  
    59  # Find all shell scripts excluding:
    60  # - Anything git-ignored - No need to lint untracked files.
    61  # - ./_* - No need to lint output directories.
    62  # - ./.git/* - Ignore anything in the git object store.
    63  # - ./vendor* - Vendored code should be fixed upstream instead.
    64  # - ./third_party/*, but re-include ./third_party/forked/*  - only code we
    65  #    forked should be linted and fixed.
    66  all_shell_scripts=()
    67  while IFS=$'\n' read -r script;
    68    do git check-ignore -q "$script" || all_shell_scripts+=("$script");
    69  done < <(find . -name "*.sh" \
    70    -not \( \
    71      -path ./_\*      -o \
    72      -path ./.git\*   -o \
    73      -path ./vendor\* -o \
    74      \( -path ./third_party\* -a -not -path ./third_party/forked\* \) \
    75    \))
    76  
    77  # Detect if the host machine has the required shellcheck version installed
    78  # if so, we will use that instead.
    79  HAVE_SHELLCHECK=false
    80  if which shellcheck &>/dev/null; then
    81    detected_version="$(shellcheck --version | grep 'version: .*')"
    82    if [[ "${detected_version}" = "version: ${SHELLCHECK_VERSION}" ]]; then
    83      HAVE_SHELLCHECK=true
    84    fi
    85  fi
    86  
    87  # common arguments we'll pass to shellcheck
    88  SHELLCHECK_OPTIONS=(
    89    # allow following sourced files that are not specified in the command,
    90    # we need this because we specify one file at a time in order to trivially
    91    # detect which files are failing
    92    "--external-sources"
    93    # include our disabled lints
    94    "--exclude=${SHELLCHECK_DISABLED}"
    95    # set colorized output
    96    "--color=${SHELLCHECK_COLORIZED_OUTPUT}"
    97  )
    98  
    99  # tell the user which we've selected and lint all scripts
   100  res=0
   101  if ${HAVE_SHELLCHECK}; then
   102    echo "Using host shellcheck ${SHELLCHECK_VERSION} binary."
   103    shellcheck "${SHELLCHECK_OPTIONS[@]}" "${all_shell_scripts[@]}" || res=$?
   104  else
   105    echo "Using shellcheck ${SHELLCHECK_VERSION} docker image."
   106    "${DOCKER}" run \
   107      --rm -v "${CAPZ_ROOT}:${CAPZ_ROOT}" -w "${CAPZ_ROOT}" \
   108      "${SHELLCHECK_IMAGE}" \
   109    "${SHELLCHECK_OPTIONS[@]}" "${all_shell_scripts[@]}" || res=$?
   110  fi
   111  
   112  # print a message based on the result
   113  if [ $res -eq 0 ]; then
   114    echo 'Congratulations! All shell files are passing lint :-)'
   115  else
   116    {
   117      echo
   118      echo 'Please review the above warnings. You can test via "./hack/verify-shellcheck.sh"'
   119      echo 'If the above warnings do not make sense, you can exempt this warning with a comment'
   120      echo ' (if your reviewer is okay with it).'
   121      echo 'In general please prefer to fix the error, we have already disabled specific lints'
   122      echo ' that the project chooses to ignore.'
   123      echo 'See: https://github.com/koalaman/shellcheck/wiki/Ignore#ignoring-one-specific-instance-in-a-file'
   124      echo
   125    } >&2
   126    exit 1
   127  fi
   128  
   129  # preserve the result
   130  exit $res