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

     1  #!/usr/bin/env bash
     2  # Copyright 2019 The Kubernetes Authors.
     3  #
     4  # Licensed under the Apache License, Version 2.0 (the "License");
     5  # you may not use this file except in compliance with the License.
     6  # You may obtain a copy of the License at
     7  #
     8  #     http://www.apache.org/licenses/LICENSE-2.0
     9  #
    10  # Unless required by applicable law or agreed to in writing, software
    11  # distributed under the License is distributed on an "AS IS" BASIS,
    12  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  # See the License for the specific language governing permissions and
    14  # limitations under the License.
    15  
    16  set -o errexit
    17  set -o nounset
    18  set -o pipefail
    19  
    20  if [[ "${TRACE-0}" == "1" ]]; then
    21      set -o xtrace
    22  fi
    23  
    24  if [ $# -ne 1 ]; then
    25    echo 1>&2 "$0: usage: ./verify-shellcheck.sh <version>"
    26    exit 2
    27  fi
    28  
    29  VERSION=${1}
    30  
    31  OS="unknown"
    32  if [[ "${OSTYPE}" == "linux"* ]]; then
    33    OS="linux"
    34  elif [[ "${OSTYPE}" == "darwin"* ]]; then
    35    OS="darwin"
    36  fi
    37  
    38  # shellcheck source=./hack/utils.sh
    39  source "$(dirname "$0")/utils.sh"
    40  ROOT_PATH=$(get_root_path)
    41  
    42  # create a temporary directory
    43  TMP_DIR=$(mktemp -d)
    44  OUT="${TMP_DIR}/out.log"
    45  
    46  # cleanup on exit
    47  cleanup() {
    48    ret=0
    49    if [[ -s "${OUT}" ]]; then
    50      echo "Found errors:"
    51      cat "${OUT}"
    52      ret=1
    53    fi
    54    echo "Cleaning up..."
    55    rm -rf "${TMP_DIR}"
    56    exit ${ret}
    57  }
    58  trap cleanup EXIT
    59  
    60  
    61  SHELLCHECK="./$(dirname "$0")/tools/bin/shellcheck/${VERSION}/shellcheck"
    62  
    63  if [ ! -f "$SHELLCHECK" ]; then
    64    # install buildifier
    65    cd "${TMP_DIR}" || exit
    66    DOWNLOAD_FILE="shellcheck-${VERSION}.${OS}.x86_64.tar.xz"
    67    curl -L "https://github.com/koalaman/shellcheck/releases/download/${VERSION}/${DOWNLOAD_FILE}" -o "${TMP_DIR}/shellcheck.tar.xz"
    68    tar xf "${TMP_DIR}/shellcheck.tar.xz"
    69    cd "${ROOT_PATH}"
    70    mkdir -p "$(dirname "$0")/tools/bin/shellcheck/${VERSION}"
    71    mv "${TMP_DIR}/shellcheck-${VERSION}/shellcheck" "$SHELLCHECK"
    72  fi
    73  
    74  echo "Running shellcheck..."
    75  cd "${ROOT_PATH}" || exit
    76  FILES=$(find . -name "*.sh")
    77  while read -r file; do
    78      "$SHELLCHECK" -x "$file" >> "${OUT}" 2>&1
    79  done <<< "$FILES"