-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgo-helper.sh
executable file
·114 lines (98 loc) · 3.19 KB
/
go-helper.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
113
114
#!/usr/bin/env bash
# Copyright (c) HashiCorp, Inc.
# SPDX-License-Identifier: BUSL-1.1
set -euo pipefail
# Perform Go formatting checks with gofumpt.
check_fmt() {
echo "==> Checking code formatting..."
declare -a malformed=()
IFS=" " read -r -a files <<< "$(tr '\n' ' ' <<< "$@")"
if [ -n "${files+set}" ] && [[ "${files[0]}" != "" ]]; then
echo "--> Checking changed files..."
for file in "${files[@]}"; do
if [ ! -f "$file" ]; then
echo "--> $file no longer exists ⚠"
continue
fi
if echo "$file" | grep -v pb.go | grep -v vendor > /dev/null; then
local output
if ! output=$(gofumpt -l "$file") || [ "$output" != "" ]; then
echo "--> ${file} ✖"
malformed+=("$file")
continue
fi
fi
echo "--> ${file} ✔"
done
else
echo "--> Checking all files..."
IFS=" " read -r -a malformed <<< "$(find . -name '*.go' | grep -v pb.go | grep -v vendor | xargs gofumpt -l)"
fi
if [ "${#malformed[@]}" -ne 0 ] && [ -n "${malformed[0]}" ] ; then
echo "--> The following files need to be reformatted with gofumpt"
printf '%s\n' "${malformed[@]}"
echo "Run \`make fmt\` to reformat code."
exit 1
fi
}
# Check that the Go toolchain meets minimum version requiremets.
check_version() {
GO_CMD=${GO_CMD:-go}
GO_VERSION_MIN=$1
echo "==> Checking that build is using go version >= $1..."
if $GO_CMD version | grep -q devel; then
GO_VERSION="devel"
else
GO_VERSION=$($GO_CMD version | grep -o 'go[0-9]\+\.[0-9]\+\(\.[0-9]\+\)\?' | tr -d 'go')
IFS="." read -r -a GO_VERSION_ARR <<< "$GO_VERSION"
IFS="." read -r -a GO_VERSION_REQ <<< "$GO_VERSION_MIN"
if [[ ${GO_VERSION_ARR[0]} -lt ${GO_VERSION_REQ[0]} ||
( ${GO_VERSION_ARR[0]} -eq ${GO_VERSION_REQ[0]} &&
( ${GO_VERSION_ARR[1]} -lt ${GO_VERSION_REQ[1]} ||
( ${GO_VERSION_ARR[1]} -eq ${GO_VERSION_REQ[1]} && ${GO_VERSION_ARR[2]} -lt ${GO_VERSION_REQ[2]} )))
]]; then
echo "Vault requires go $GO_VERSION_MIN to build; found $GO_VERSION."
exit 1
fi
fi
echo "--> Using go version $GO_VERSION..."
}
# Download all the modules for all go.mod's defined in the project.
mod_download() {
while IFS= read -r -d '' mod; do
echo "==> Downloading Go modules for $mod to $(go env GOMODCACHE)..."
pushd "$(dirname "$mod")" > /dev/null || (echo "failed to push into module dir" && exit 1)
GOOS=linux GOARCH=amd64 go mod download -x
popd > /dev/null || (echo "failed to pop out of module dir" && exit 1)
done < <(find . -type f -name go.mod -print0)
}
# Tidy all the go.mod's defined in the project.
mod_tidy() {
while IFS= read -r -d '' mod; do
echo "==> Tidying $mod..."
pushd "$(dirname "$mod")" > /dev/null || (echo "failed to push into module dir" && exit 1)
GOOS=linux GOARCH=amd64 go mod tidy
popd > /dev/null || (echo "failed to pop out of module dir" && exit 1)
done < <(find . -type f -name go.mod -print0)
}
main() {
case $1 in
mod-download)
mod_download
;;
mod-tidy)
mod_tidy
;;
check-fmt)
check_fmt "${@:2}"
;;
check-version)
check_version "$2"
;;
*)
echo "unknown sub-command" >&2
exit 1
;;
esac
}
main "$@"