-
Notifications
You must be signed in to change notification settings - Fork 181
/
Copy pathcheck-format.sh
executable file
·58 lines (48 loc) · 1.92 KB
/
check-format.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
#!/bin/bash
# Copyright 2019 The Kubernetes 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.
set -o errexit
set -o nounset
set -o pipefail
# Change directories to the parent directory of the one in which this
# script is located.
cd "$(dirname "${BASH_SOURCE[0]}")/.."
# Ensure the temp out file is removed when this program exits.
out="$(mktemp)"
on_exit() {
[ -z "${out}" ] || [ ! -e "${out}" ] || rm -f "${out}"
}
trap on_exit EXIT
# Run goformat on all the sources.
flags="-e -s -w"
[ -z "${PROW_JOB_ID-}" ] || flags="-d ${flags}"
eval "gofmt ${flags} ./cmd/ ./pkg/" | tee "${out}"
# Check to see if there any suggestions.
goformat_exit_code=0; test -z "$(head -n 1 "${out}")" || goformat_exit_code=1
# Truncate the out file.
rm -f "${out}" && touch "${out}"
# Run goimports on all the sources.
go get golang.org/x/tools/cmd/goimports
go install golang.org/x/tools/cmd/goimports
cmd=$(go list -f \{\{\.Target\}\} golang.org/x/tools/cmd/goimports)
flags="-e -w"
[ -z "${PROW_JOB_ID-}" ] || flags="-d ${flags}"
eval "${cmd} ${flags} ./cmd/ ./pkg/" | tee "${out}"
# Check to see if there any suggestions.
goimports_exit_code=0; test -z "$(head -n 1 "${out}")" || goimports_exit_code=1
# If running on Prow, exit with a non-zero code if either of the tests failed.
if [ -n "${PROW_JOB_ID-}" ]; then
[ "${goformat_exit_code}" -eq "0" ] || exit "${goformat_exit_code}"
[ "${goimports_exit_code}" -eq "0" ] || exit "${goimports_exit_code}"
fi