| 1 |
#!/usr/bin/env bash |
| 2 |
# Copyright 2019 The Go Cloud Development Kit 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 |
# https://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 |
# This script checks to see if `go mod tidy` has been run on the module |
| 17 |
# in the current directory. |
| 18 |
# |
| 19 |
# It exits with status 1 if "go mod tidy && go list -deps ./..." would |
| 20 |
# make changes. |
| 21 |
# |
| 22 |
# TODO(rvangent): Replace this with `go mod tidy --check` when it exists: |
| 23 |
# https://github.com/golang/go/issues/27005. |
| 24 |
# |
| 25 |
# TODO(rvangent): Drop the "go list" part here and in gomodcleanup.sh once |
| 26 |
# https://github.com/golang/go/issues/31248 is fixed. |
| 27 |
|
| 28 |
set -euo pipefail |
| 29 |
|
| 30 |
TMP_GOMOD=$(mktemp) |
| 31 |
TMP_GOSUM=$(mktemp) |
| 32 |
|
| 33 |
function cleanup() { |
| 34 |
# Restore the original files in case "go mod tidy" made changes. |
| 35 |
if [[ -f "$TMP_GOMOD" ]]; then |
| 36 |
mv "$TMP_GOMOD" ./go.mod |
| 37 |
fi |
| 38 |
if [[ -f "$TMP_GOSUM" ]]; then |
| 39 |
mv "$TMP_GOSUM" ./go.sum |
| 40 |
fi |
| 41 |
} |
| 42 |
trap cleanup EXIT |
| 43 |
|
| 44 |
# Make copies of the current files. |
| 45 |
cp ./go.mod "$TMP_GOMOD" |
| 46 |
cp ./go.sum "$TMP_GOSUM" |
| 47 |
|
| 48 |
# Modifies the files in-place. |
| 49 |
go mod tidy |
| 50 |
go list -deps ./... &> /dev/null |
| 51 |
|
| 52 |
# Check for diffs. |
| 53 |
diff -u "$TMP_GOMOD" ./go.mod |
| 54 |
diff -u "$TMP_GOSUM" ./go.sum |