|
| 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 | + REPO_ROOT=$(dirname "${BASH_SOURCE[0]}")/.. |
| 21 | + |
| 22 | + # Usage: |
| 23 | +# hack/pin-dependency.sh $MODULE $SHA-OR-TAG |
| 24 | +# |
| 25 | +# Example: |
| 26 | +# hack/pin-dependency.sh github.com/docker/docker 501cb131a7b7 |
| 27 | + |
| 28 | + # Explicitly opt into go modules, even though we're inside a GOPATH directory |
| 29 | +export GO111MODULE=on |
| 30 | +# Explicitly clear GOPATH, to ensure nothing this script calls makes use of that path info |
| 31 | +export GOPATH= |
| 32 | +# Explicitly clear GOFLAGS, since GOFLAGS=-mod=vendor breaks dependency resolution while rebuilding vendor |
| 33 | +export GOFLAGS= |
| 34 | +# Detect problematic GOPROXY settings that prevent lookup of dependencies |
| 35 | +if [[ "${GOPROXY:-}" == "off" ]]; then |
| 36 | + echo "Cannot run hack/pin-dependency.sh with \$GOPROXY=off" |
| 37 | + exit 1 |
| 38 | +fi |
| 39 | + |
| 40 | + dep="${1:-}" |
| 41 | +sha="${2:-}" |
| 42 | +if [[ -z "${dep}" || -z "${sha}" ]]; then |
| 43 | + echo "Usage:" |
| 44 | + echo " hack/pin-dependency.sh \$MODULE \$SHA-OR-TAG" |
| 45 | + echo "" |
| 46 | + echo "Example:" |
| 47 | + echo " hack/pin-dependency.sh github.com/docker/docker 501cb131a7b7" |
| 48 | + echo "" |
| 49 | + exit 1 |
| 50 | +fi |
| 51 | + |
| 52 | + # Add the require directive |
| 53 | +echo "Running: go get ${dep}@${sha}" |
| 54 | +go get -d "${dep}@${sha}" |
| 55 | + |
| 56 | + # Find the resolved version |
| 57 | +rev=$(go mod edit -json | jq -r ".Require[] | select(.Path == \"${dep}\") | .Version") |
| 58 | +echo "Resolved to ${dep}@${rev}" |
| 59 | + |
| 60 | + # Add the replace directive |
| 61 | +echo "Running: go mod edit -replace ${dep}=${dep}@${rev}" |
| 62 | +go mod edit -replace "${dep}=${dep}@${rev}" |
0 commit comments