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

     1  #!/usr/bin/env bash
     2  
     3  # Copyright 2023 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 checks import restrictions. The script looks for a file called
    18  # `.import-restrictions` in each directory, then all imports of the package are
    19  # checked against each "rule" in the file.
    20  # Usage: `hack/verify-import-restrictions.sh`.
    21  
    22  set -o errexit
    23  set -o nounset
    24  set -o pipefail
    25  
    26  sub_packages=(
    27    "api"
    28    "exp/api"
    29    "bootstrap/kubeadm/api"
    30    "cmd/clusterctl/api"
    31    "controlplane/kubeadm/api"
    32    "exp/addons/api"
    33    "exp/ipam/api"
    34    "exp/runtime/api"
    35    "test/infrastructure/docker/api"
    36    "test/infrastructure/docker/exp/api"
    37    "test/infrastructure/inmemory/api"
    38  )
    39  
    40  packages=()
    41  visit() {
    42    local count=0
    43    for file in "$1"/* ; do
    44      if [ -d "$file" ]; then
    45        visit "$file"
    46      elif [ -f "$file" ]; then
    47        ((count += 1))
    48      fi
    49    done
    50    if [ "$count" -gt 0 ]; then
    51      # import-boss may not accept directories without any sources
    52      packages+=("./$1")
    53    fi
    54  }
    55  for d in "${sub_packages[@]}"; do
    56    visit "$d"
    57  done
    58  
    59  INPUT_DIRS="$(IFS=, ; echo "${packages[*]}")"
    60  echo "Enforcing imports in source codes under the following directories: ${INPUT_DIRS}"
    61  
    62  # Make sure GOPATH is unset to avoid behavior inconsistency
    63  # as import-boss will go through the sources
    64  unset GOPATH
    65  import-boss --include-test-files=true --verify-only --input-dirs "${INPUT_DIRS}"