Debian Codegraph @

kokoro/testutils/check_go_generated_files_up_to_date.sh

raw
1 #!/bin/bash
2 # Copyright 2022 Google LLC
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
17 # This scripts checks that a given Go workspace has its generated Bazel files up
18 # to date.
19
20 usage() {
21 echo "Usage: $0 [-h] [-c <compat value (default 1.19)>] <go project dir>"
22 echo " -c: Value to pass to `-compat`. Default to 1.19."
23 echo " -h: Help. Print this usage information."
24 exit 1
25 }
26
27 COMPAT="1.19"
28 GO_PROJECT_DIR=
29
30 process_args() {
31 # Parse options.
32 while getopts "hc:" opt; do
33 case "${opt}" in
34 c) COMPAT="${OPTARG}" ;;
35 *) usage ;;
36 esac
37 done
38 shift $((OPTIND - 1))
39 readonly GO_PROJECT_DIR="$1"
40 if [[ -z "${GO_PROJECT_DIR}" ]]; then
41 usage
42 fi
43 }
44
45 main() {
46 process_args "$@"
47
48 (
49 cd "${GO_PROJECT_DIR}"
50 local -r temp_dir_current_generated_files="$(mktemp -dt \
51 current_tink_go_build_files.XXXXXX)"
52 local -r go_generated_files=(
53 ./go.mod
54 ./go.sum
55 )
56
57 # Copy all current generated files into temp_dir_current_generated_files.
58 local -r current_go_generated_files=( "${go_generated_files[@]}" )
59 for generated_file_path in "${current_go_generated_files[@]}"; do
60 mkdir -p \
61 "$(dirname \
62 "${temp_dir_current_generated_files}/${generated_file_path}")"
63 cp "${generated_file_path}" \
64 "${temp_dir_current_generated_files}/${generated_file_path}"
65 done
66
67 # Update build files.
68 go mod tidy -compat="${COMPAT}"
69
70 # Compare current with new build files.
71 local -r new_go_generated_files=( "${go_generated_files[@]}" )
72
73 for generated_file_path in "${new_go_generated_files[@]}"; do
74 if ! cmp -s "${generated_file_path}" \
75 "${temp_dir_current_generated_files}/${generated_file_path}"; then
76 echo "ERROR: ${generated_file_path} needs to be updated. Please follow \
77 the instructions on go/tink-workflows#update-go-build." >&2
78 echo "Diff for ${generated_file_path}:"
79 diff "${generated_file_path}" \
80 "${temp_dir_current_generated_files}/${generated_file_path}"
81 exit 1
82 fi
83 done
84 )
85 }
86
87 main "$@"