sigs.k8s.io/cluster-api@v1.7.1/hack/verify-starlark.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  SCRIPT_DIR="$(dirname "${BASH_SOURCE[0]}")"
    25  ROOT_PATH="$(cd "${SCRIPT_DIR}"/.. && pwd)"
    26  
    27  VERSION="0.29.0"
    28  
    29  MODE="check"
    30  
    31  if [[ "$*" == "fix" ]]; then
    32    MODE="fix"
    33  fi
    34  
    35  if [[ "${OSTYPE}" == "linux"* ]]; then
    36    BINARY="buildifier"
    37  elif [[ "${OSTYPE}" == "darwin"* ]]; then
    38    BINARY="buildifier.mac"
    39  fi
    40  
    41  # create a temporary directory
    42  TMP_DIR=$(mktemp -d)
    43  OUT="${TMP_DIR}/out.log"
    44  
    45  # cleanup on exit
    46  cleanup() {
    47    ret=0
    48    if [[ -s "${OUT}" ]]; then
    49      echo "Found errors:"
    50      cat "${OUT}"
    51      echo ""
    52      echo "run make tiltfile-fix to fix the errors"
    53      ret=1
    54    fi
    55    echo "Cleaning up..."
    56    rm -rf "${TMP_DIR}"
    57    exit ${ret}
    58  }
    59  trap cleanup EXIT
    60  
    61  BUILDIFIER="${SCRIPT_DIR}/tools/bin/buildifier/${VERSION}/buildifier"
    62  
    63  if [ ! -f "$BUILDIFIER" ]; then
    64    # install buildifier
    65    cd "${TMP_DIR}" || exit
    66    curl -L "https://github.com/bazelbuild/buildtools/releases/download/${VERSION}/${BINARY}" -o "${TMP_DIR}/buildifier"
    67    chmod +x "${TMP_DIR}/buildifier"
    68    cd "${ROOT_PATH}"
    69    mkdir -p "$(dirname "$0")/tools/bin/buildifier/${VERSION}"
    70    mv "${TMP_DIR}/buildifier" "$BUILDIFIER"
    71  fi
    72  
    73  echo "Running buildifier..."
    74  cd "${ROOT_PATH}" || exit
    75  "${BUILDIFIER}" -mode=${MODE} Tiltfile >> "${OUT}" 2>&1