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

     1  #!/usr/bin/env bash
     2  
     3  # Copyright 2024 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 [[ "${TRACE-0}" == "1" ]]; then
    22      set -o xtrace
    23  fi
    24  
    25  function usage {
    26    local script
    27    script="$(basename "$0")"
    28    cat >&2 <<EOF
    29  Usage: ${script} [-g <maximum go directive>]
    30  This script should be run at the root of a module.
    31  -g <maximum go directive>
    32    Compare the go directive in the local working copy's go.mod
    33    to the specified maximum version it can be. Versions provided
    34    here are of the form 1.x.y, without the 'go' prefix.
    35  Examples:
    36    ${script} -g 1.20
    37    ${script} -g 1.21.6
    38  EOF
    39    exit 1
    40  }
    41  
    42  directory=""
    43  max=""
    44  while getopts g: opt; do
    45    case "$opt" in
    46      g) max="$OPTARG";;
    47      *) usage;;
    48    esac
    49  done
    50  
    51  if [[ -z "${max}" || "${max}" == go* ]]; then
    52    usage
    53  fi
    54  
    55  if [[ -z "${directory}" ]]; then
    56    directory="."
    57  fi
    58  
    59  # Recursive search for go.mod files
    60  find "${directory}" -name "go.mod" -type f -print0 | while IFS= read -r -d '' file; do
    61    echo "Running go directive verify test for ${file}"
    62    if ! current=$(awk '$1 == "go" {print $2; exit}' "$file"); then
    63      echo >&2 "FAIL: could not get value of go directive from ${file}"
    64      exit 1
    65    fi
    66  
    67    if ! printf '%s\n' "${current}" "${max}" | sort --check=silent --version-sort; then
    68      echo >&2 "FAIL: current Go directive ${current} in ${file} is greater than ${max}"
    69      exit 1
    70    fi
    71  done