Skip to content

Commit 43117cf

Browse files
committed
Script to check if entries in go.mod files are in sync
- ensure that the root go.mod and the module specific go.mod have the same `require` and `replace` directives for different dependencies. Signed-off-by: Davanum Srinivas <[email protected]>
1 parent caf9e25 commit 43117cf

4 files changed

Lines changed: 117 additions & 0 deletions

File tree

.github/workflows/ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ jobs:
7171

7272
- name: verify go modules and vendor directory
7373
run: |
74+
sudo apt-get install -y jq
7475
make verify-vendor
7576
working-directory: src/github.com/containerd/containerd
7677

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,8 @@ verify-vendor: ## verify if all the go.mod/go.sum files are up-to-date
425425
@(cd ${TMPDIR}/containerd/integration/client && ${GO} mod tidy)
426426
@diff -r -u -q ${ROOTDIR} ${TMPDIR}/containerd
427427
@rm -rf ${TMPDIR}
428+
@${ROOTDIR}/script/verify-go-modules.sh api
429+
@${ROOTDIR}/script/verify-go-modules.sh integration/client
428430

429431

430432
help: ## this help

api/go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,7 @@ require (
1313

1414
replace (
1515
github.com/gogo/googleapis => github.com/gogo/googleapis v1.3.2
16+
// urfave/cli must be <= v1.22.1 due to a regression: https://github.com/urfave/cli/issues/1092
17+
github.com/urfave/cli => github.com/urfave/cli v1.22.1
1618
google.golang.org/genproto => google.golang.org/genproto v0.0.0-20200224152610-e50cd9704f63
1719
)

script/verify-go-modules.sh

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
#!/usr/bin/env bash
2+
3+
# Copyright The containerd 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+
#
18+
# verifies if the require and replace directives for two go.mod files are in sync
19+
#
20+
set -eu -o pipefail
21+
22+
ROOT=$(dirname "${BASH_SOURCE}")/..
23+
24+
if [ "$#" -ne 1 ]; then
25+
echo "Usage: $0 dir-for-second-go-mod"
26+
exit 1
27+
fi
28+
29+
if ! command -v jq &> /dev/null ; then
30+
echo Please install jq
31+
exit 1
32+
fi
33+
34+
# Load the requires and replaces section in the root go.mod file
35+
declare -A map_requires_1
36+
declare -A map_replaces_1
37+
pushd "${ROOT}" > /dev/null
38+
while IFS='#' read -r key value
39+
do
40+
map_requires_1[$key]="$value"
41+
done<<<$(go mod edit -json | jq -r '.Require[] | .Path + " # " + .Version')
42+
while IFS='#' read -r key value
43+
do
44+
map_replaces_1[$key]="$value"
45+
done<<<$(go mod edit -json | jq -r '.Replace[] | .Old.Path + " # " + .New.Path + " : " + .New.Version')
46+
popd > /dev/null
47+
48+
# Load the requires and replaces section in the other go.mod file
49+
declare -A map_requires_2
50+
declare -A map_replaces_2
51+
pushd "${ROOT}/$1" > /dev/null
52+
while IFS='#' read -r key value
53+
do
54+
map_requires_2[$key]="$value"
55+
done<<<$(go mod edit -json | jq -r '.Require[] | .Path + " # " + .Version')
56+
while IFS='#' read -r key value
57+
do
58+
map_replaces_2[$key]="$value"
59+
done<<<$(go mod edit -json | jq -r '.Replace[] | .Old.Path + " # " + .New.Path + " : " + .New.Version')
60+
popd > /dev/null
61+
62+
# signal for errors later
63+
ERRORS=0
64+
65+
# iterate through the second go.mod's require section and ensure that all items
66+
# have the same values in the root go.mod replace section
67+
for k in "${!map_requires_2[@]}"
68+
do
69+
if [ -v "map_requires_1[$k]" ]; then
70+
if [ "${map_requires_2[$k]}" != "${map_requires_1[$k]}" ]; then
71+
echo "${k} has different values in the go.mod files require section:" \
72+
"${map_requires_1[$k]} in root go.mod ${map_requires_2[$k]} in $1/go.mod"
73+
ERRORS=$(( ERRORS + 1 ))
74+
fi
75+
fi
76+
done
77+
78+
# iterate through the second go.mod's replace section and ensure that all items
79+
# have the same values in the root go.mod's replace section. Except for the
80+
# containerd/containerd which we know will be different
81+
for k in "${!map_replaces_2[@]}"
82+
do
83+
if [[ "${k}" == "github.com/containerd/containerd"* ]]; then
84+
continue
85+
fi
86+
if [ -v "map_replaces_1[$k]" ]; then
87+
if [ "${map_replaces_2[$k]}" != "${map_replaces_1[$k]}" ]; then
88+
echo "${k} has different values in the go.mod files replace section:" \
89+
"${map_replaces_1[$k]} in root go.mod ${map_replaces_2[$k]} in $1/go.mod"
90+
ERRORS=$(( ERRORS + 1 ))
91+
fi
92+
fi
93+
done
94+
95+
# iterate through the root go.mod's replace section and ensure that all the
96+
# same items are present in the second go.mod's replace section and nothing is missing
97+
for k in "${!map_replaces_1[@]}"
98+
do
99+
if [[ "${k}" == "github.com/containerd/containerd"* ]]; then
100+
continue
101+
fi
102+
if [ ! -v "map_replaces_2[$k]" ]; then
103+
echo "${k} has an entry in root go.mod replace section, but is missing from" \
104+
" replace section in $1/go.mod"
105+
ERRORS=$(( ERRORS + 1 ))
106+
fi
107+
done
108+
109+
if [ "$ERRORS" -ne 0 ]; then
110+
echo "Found $ERRORS error(s)."
111+
exit 1
112+
fi

0 commit comments

Comments
 (0)