|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# Copyright 2014 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 | +set -o errexit |
| 18 | +set -o nounset |
| 19 | +set -o pipefail |
| 20 | + |
| 21 | +if [ "$(uname)" = 'Darwin' ]; then |
| 22 | + readlinkf(){ perl -MCwd -e 'print Cwd::abs_path shift' "$1";} |
| 23 | +else |
| 24 | + readlinkf(){ readlink -f "$1"; } |
| 25 | +fi |
| 26 | + |
| 27 | +# shellcheck disable=SC2128 |
| 28 | +SCRIPT_DIR="$(cd "$(dirname "$(readlinkf "$BASH_SOURCE")")" ; pwd)" |
| 29 | + |
| 30 | +# We assume the link to the script ( {ensure,verify}-boilerplate.sh ) to be |
| 31 | +# in a directory 2 levels down from the repo root, e.g. in |
| 32 | +# <root>/repo-infra/verify/verify-boilerplate.sh |
| 33 | +# Alternatively, you can set the project root by setting the variable |
| 34 | +# `REPO_ROOT`. |
| 35 | +# |
| 36 | +# shellcheck disable=SC2128 |
| 37 | +: "${REPO_ROOT:="$(cd "${SCRIPT_DIR}/.." ; pwd)"}" |
| 38 | + |
| 39 | +boilerDir="${SCRIPT_DIR}/boilerplate/" |
| 40 | +boiler="${boilerDir}/boilerplate.py" |
| 41 | + |
| 42 | +verify() { |
| 43 | + # shellcheck disable=SC2207 |
| 44 | + files_need_boilerplate=( |
| 45 | + $( "$boiler" --rootdir="$REPO_ROOT" --boilerplate-dir="$boilerDir" "$@") |
| 46 | + ) |
| 47 | + |
| 48 | + # Run boilerplate check |
| 49 | + if [[ ${#files_need_boilerplate[@]} -gt 0 ]]; then |
| 50 | + for file in "${files_need_boilerplate[@]}"; do |
| 51 | + echo "Boilerplate header is wrong for: ${file}" >&2 |
| 52 | + done |
| 53 | + |
| 54 | + return 1 |
| 55 | + fi |
| 56 | +} |
| 57 | + |
| 58 | +ensure() { |
| 59 | + "$boiler" --rootdir="$REPO_ROOT" --boilerplate-dir="$boilerDir" --ensure "$@" |
| 60 | +} |
| 61 | + |
| 62 | +case "$0" in |
| 63 | + */ensure-boilerplate.sh) |
| 64 | + ensure "$@" |
| 65 | + ;; |
| 66 | + */verify-boilerplate.sh) |
| 67 | + verify "$@" |
| 68 | + ;; |
| 69 | + *) |
| 70 | + { |
| 71 | + echo "unknown command '$0'" |
| 72 | + echo "" |
| 73 | + echo "Call the script as either 'verify-boilerplate.sh' or 'ensure-boilerplate.sh'" |
| 74 | + } >&2 |
| 75 | + |
| 76 | + exit 1 |
| 77 | + ;; |
| 78 | +esac |
0 commit comments