sigs.k8s.io/prow@v0.0.0-20240503223140-c5e374dc7eb1/hack/make-rules/update/go-deps.sh (about) 1 #!/usr/bin/env bash 2 # Copyright 2021 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 nounset 17 set -o errexit 18 set -o pipefail 19 20 SCRIPT_ROOT="$(cd "$(dirname "$0")" && pwd)" 21 REPO_ROOT="${SCRIPT_ROOT}/../../.." 22 23 function usage() { 24 >&2 cat <<EOF 25 Build Prow components and deploy them into the KIND test cluster. 26 27 Usage: $0 [OPTIONS] [packages] 28 29 Examples: 30 # Update all packages with '-u' flag in the root folder. Run "go mod tidy". 31 $0 --minor 32 33 # Update all packages with '-u=patch' flag in the root folder. Run "go mod tidy". 34 $0 --patch 35 36 # Update "ko" package in the hack/tools/go.mod file. Run "go mod tidy". 37 $0 --minor --tools github.com/google/ko 38 39 Options: 40 --minor: 41 Pass "-u" flag to "go get". Run "go mod tidy". 42 43 --patch: 44 Pass "-u=patch" flag to "go get". Run "go mod tidy". 45 46 --tools: 47 Do the actions inside the hack/tools folder, not the root folder. All 48 other flags are still observed. 49 50 --only-tidy: 51 Don't update anything. Instead just run "go mod tidy" in the root and 52 hack/tools folders. This is the behavior if no arguments are provided. 53 54 --help: 55 Display this help message. 56 EOF 57 } 58 59 function update() { 60 declare -a packages 61 local update_mode 62 update_mode="${1}" 63 shift 64 packages=("$@") 65 # Update all packages if no packages are provided. 66 if [[ -z "${packages[*]}" ]]; then 67 packages=("./...") 68 fi 69 70 echo >&2 "Running" go get "${update_mode}" "${packages[@]}" 71 go get "${update_mode}" "${packages[@]}" 72 } 73 74 function tidy() { 75 echo >&2 "Running" go mod tidy 76 go mod tidy 77 } 78 79 function main() { 80 declare -a packages 81 local update_mode 82 local tools 83 local only_tidy=0 84 85 if ! (($#)); then 86 only_tidy=1 87 fi 88 89 for arg in "$@"; do 90 case "${arg}" in 91 --minor) 92 update_mode="-u" 93 ;; 94 --patch) 95 update_mode="-u=patch" 96 ;; 97 --tools) 98 tools="${arg}" 99 ;; 100 --only-tidy) 101 only_tidy=1 102 ;; 103 --help) 104 usage 105 return 106 ;; 107 -*) 108 echo >&2 "Unrecognized option '${arg}'" 109 usage 110 return 111 ;; 112 *) 113 packages+=("${arg}") 114 ;; 115 esac 116 done 117 118 echo >&2 "Ensuring go version." 119 # shellcheck disable=SC1091 120 source "${REPO_ROOT}"/hack/build/setup-go.sh 121 122 echo >&2 "Go version: $(go version)" 123 124 export GO111MODULE=on 125 export GOPROXY=https://proxy.golang.org 126 export GOSUMDB=sum.golang.org 127 128 if ((only_tidy)); then 129 pushd "${REPO_ROOT}" 130 tidy 131 pushd "${REPO_ROOT}/hack/tools" 132 tidy 133 echo >&2 "SUCCESS: ran go mod tidy in root and hack/tools folders" 134 return 135 fi 136 137 if [[ -n "${tools:-}" ]]; then 138 pushd "${REPO_ROOT}/hack/tools" 139 else 140 pushd "${REPO_ROOT}" 141 fi 142 if [[ -n "${update_mode:-}" ]]; then 143 update "${update_mode}" "${packages[@]}" 144 fi 145 146 tidy 147 148 echo >&2 "SUCCESS: updated modules" 149 } 150 151 trap 'echo "FAILED" >&2' ERR 152 153 main "$@"