forked from docker/compose
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathverify-go-modules.sh
executable file
·112 lines (97 loc) · 3.55 KB
/
verify-go-modules.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/env bash
# Copyright The containerd Authors.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# verifies if the require and replace directives for two go.mod files are in sync
#
set -eu -o pipefail
ROOT=$(dirname "${BASH_SOURCE}")
if [ "$#" -ne 1 ]; then
echo "Usage: $0 dir-for-second-go-mod"
exit 1
fi
if ! command -v jq &> /dev/null ; then
echo Please install jq
exit 1
fi
# Load the requires and replaces section in the root go.mod file
declare -A map_requires_1
declare -A map_replaces_1
pushd "${ROOT}" > /dev/null
while IFS='#' read -r key value
do
map_requires_1[$key]="$value"
done<<<$(go mod edit -json | jq -r '.Require[] | .Path + " # " + .Version')
while IFS='#' read -r key value
do
[ "$key" = "" ] || map_replaces_1[$key]="$value"
done<<<$(go mod edit -json | jq -r 'try .Replace[] | .Old.Path + " # " + .New.Path + " : " + .New.Version')
popd > /dev/null
# Load the requires and replaces section in the other go.mod file
declare -A map_requires_2
declare -A map_replaces_2
pushd "${ROOT}/$1" > /dev/null
while IFS='#' read -r key value
do
[ "$key" = "" ] || map_requires_2[$key]="$value"
done<<<$(go mod edit -json | jq -r '.Require[] | .Path + " # " + .Version')
while IFS='#' read -r key value
do
map_replaces_2[$key]="$value"
done<<<$(go mod edit -json | jq -r 'try .Replace[] | .Old.Path + " # " + .New.Path + " : " + .New.Version')
popd > /dev/null
# signal for errors later
ERRORS=0
# iterate through the second go.mod's require section and ensure that all items
# have the same values in the root go.mod replace section
for k in "${!map_requires_2[@]}"
do
if [ -v "map_requires_1[$k]" ]; then
if [ "${map_requires_2[$k]}" != "${map_requires_1[$k]}" ]; then
echo "${k} has different values in the go.mod files require section:" \
"${map_requires_1[$k]} in root go.mod ${map_requires_2[$k]} in $1/go.mod"
ERRORS=$(( ERRORS + 1 ))
fi
fi
done
# iterate through the second go.mod's replace section and ensure that all items
# have the same values in the root go.mod's replace section. Except for the
# containerd/containerd which we know will be different
for k in "${!map_replaces_2[@]}"
do
if [[ "${k}" == "github.com/docker/compose"* ]]; then
continue
fi
if [ -v "map_replaces_1[$k]" ]; then
if [ "${map_replaces_2[$k]}" != "${map_replaces_1[$k]}" ]; then
echo "${k} has different values in the go.mod files replace section:" \
"${map_replaces_1[$k]} in root go.mod ${map_replaces_2[$k]} in $1/go.mod"
ERRORS=$(( ERRORS + 1 ))
fi
fi
done
# iterate through the root go.mod's replace section and ensure that all the
# same items are present in the second go.mod's replace section and nothing is missing
for k in "${!map_replaces_1[@]}"
do
if [[ "${k}" == "github.com/docker/compose"* ]]; then
continue
fi
if [ ! -v "map_replaces_2[$k]" ]; then
echo "${k} has an entry in root go.mod replace section, but is missing from" \
" replace section in $1/go.mod"
ERRORS=$(( ERRORS + 1 ))
fi
done
if [ "$ERRORS" -ne 0 ]; then
echo "Found $ERRORS error(s)."
exit 1
fi