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

     1  #!/usr/bin/env bash
     2  
     3  # Copyright 2019 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  KUBE_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
    26  
    27  # Usage:
    28  #   hack/pin-dependency.sh $MODULE $SHA-OR-TAG
    29  #
    30  # Example:
    31  #   hack/pin-dependency.sh github.com/docker/docker 501cb131a7b7
    32  
    33  # Explicitly opt into go modules, even though we're inside a GOPATH directory
    34  export GO111MODULE=on
    35  # Explicitly clear GOFLAGS, since GOFLAGS=-mod=vendor breaks dependency resolution while rebuilding vendor
    36  export GOFLAGS=
    37  # Detect problematic GOPROXY settings that prevent lookup of dependencies
    38  if [[ "${GOPROXY:-}" == "off" ]]; then
    39    kube::log::error "Cannot run with \$GOPROXY=off"
    40    exit 1
    41  fi
    42  
    43  kube::golang::verify_go_version
    44  kube::util::require-jq
    45  
    46  dep="${1:-}"
    47  sha="${2:-}"
    48  if [[ -z "${dep}" || -z "${sha}" ]]; then
    49    echo "Usage:"
    50    echo "  hack/pin-dependency.sh \$MODULE \$SHA-OR-TAG"
    51    echo ""
    52    echo "Example:"
    53    echo "  hack/pin-dependency.sh github.com/docker/docker 501cb131a7b7"
    54    echo ""
    55    exit 1
    56  fi
    57  
    58  _tmp="${KUBE_ROOT}/_tmp"
    59  cleanup() {
    60    rm -rf "${_tmp}"
    61  }
    62  trap "cleanup" EXIT SIGINT
    63  cleanup
    64  mkdir -p "${_tmp}"
    65  
    66  # Add the require directive
    67  echo "Running: go get ${dep}@${sha}"
    68  go get -m -d "${dep}@${sha}"
    69  
    70  # Find the resolved version
    71  rev=$(go mod edit -json | jq -r ".Require[] | select(.Path == \"${dep}\") | .Version")
    72  
    73  # No entry in go.mod, we must be using the natural version indirectly
    74  if [[ -z "${rev}" ]]; then
    75    # backup the go.mod file, since go list modifies it
    76    cp go.mod "${_tmp}/go.mod.bak"
    77    # find the revision
    78    rev=$(go list -m -json "${dep}" | jq -r .Version)
    79    # restore the go.mod file
    80    mv "${_tmp}/go.mod.bak" go.mod
    81  fi
    82  
    83  # No entry found
    84  if [[ -z "${rev}" ]]; then
    85    echo "Could not resolve ${sha}"
    86    exit 1
    87  fi
    88  
    89  echo "Resolved to ${dep}@${rev}"
    90  
    91  # Add the replace directive
    92  echo "Running: go mod edit -replace ${dep}=${dep}@${rev}"
    93  go mod edit -replace "${dep}=${dep}@${rev}"
    94  
    95  echo ""
    96  echo "Run hack/update-vendor.sh to rebuild the vendor directory"