airshipctl/tools/gomod_check
Ian Howell 7ecae507e0 Add make tidy to check for up-to-date go.mod
This commit adds the `tidy` target. This target will fail if the go.mod
or go.sum files are not up to date with the code. It will also be a
prerequisite to the `lint` command, implying that it will also be caught
by the Zuul gates.

Change-Id: Ie33de2d793beca100435670346eb0e2ea7b3b0dd
2019-11-15 09:41:41 -06:00

23 lines
496 B
Bash
Executable File

#!/bin/bash
set -e
backup_dir=$(mktemp -d)
revert() {
cp "$backup_dir/go.mod" "go.mod"
cp "$backup_dir/go.sum" "go.sum"
}
cp go.mod go.sum "$backup_dir"
if [[ $(go mod tidy 2>&1) ]]; then
printf "FAIL: error in go.mod. Please run 'go mod tidy' and fix any issues\n"
revert
exit 1
fi
if [[ $(diff "go.mod" "$backup_dir/go.mod") ]] || [[ $(diff "go.sum" "$backup_dir/go.sum") ]]; then
printf "FAIL: go.mod/go.sum are not up to date. Please run 'go mod tidy'\n"
revert
exit 1
fi