verify-go-modules.sh 3.6 KB

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