Skip to content
This repository was archived by the owner on Sep 24, 2021. It is now read-only.

Commit a477868

Browse files
authored
Merge pull request #82 from ericzhang08/add-goimports-verification
Add a goimports verification
2 parents 13632bd + 858ed81 commit a477868

File tree

4 files changed

+90
-0
lines changed

4 files changed

+90
-0
lines changed

hack/update-all.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,4 @@ REPO_PATH=$(get_root_path)
2323

2424
"${REPO_PATH}"/hack/update-deps.sh
2525
"${REPO_PATH}"/hack/update-gofmt.sh
26+
"${REPO_PATH}"/hack/update-goimports.sh

hack/update-goimports.sh

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/usr/bin/env bash
2+
# Copyright 2019 The Kubernetes 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+
# http://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+
# script to run gofmt over our code (not vendor)
17+
set -o errexit
18+
set -o nounset
19+
set -o pipefail
20+
21+
# shellcheck source=/dev/null
22+
source "$(dirname "$0")/utils.sh"
23+
# cd to the root path
24+
cd_root_path
25+
26+
# update go imports
27+
goimports -w ./

hack/verify-all.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ if [[ "${VERIFY_GOFMT:-true}" == "true" ]]; then
5353
cd "${REPO_PATH}"
5454
fi
5555

56+
if [[ "${VERIFY_GOIMPORTS:-true}" == "true" ]]; then
57+
echo "[*] Verifying goimports..."
58+
hack/verify-goimports.sh || res=1
59+
cd "${REPO_PATH}"
60+
fi
61+
5662
if [[ "${VERIFY_GOLINT:-true}" == "true" ]]; then
5763
echo "[*] Verifying golint..."
5864
hack/verify-golint.sh || res=1

hack/verify-goimports.sh

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#!/usr/bin/env bash
2+
# Copyright 2019 The Kubernetes 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+
# http://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+
set -o errexit
17+
set -o nounset
18+
set -o pipefail
19+
20+
# shellcheck source=/dev/null
21+
source "$(dirname "$0")/utils.sh"
22+
# cd to the root path
23+
cd_root_path
24+
25+
# create a temporary directory
26+
TMP_DIR=$(mktemp -d)
27+
28+
# cleanup
29+
exitHandler() (
30+
echo "Cleaning up..."
31+
rm -rf "${TMP_DIR}"
32+
)
33+
trap exitHandler EXIT
34+
35+
# pull goimports
36+
export GO111MODULE=on
37+
URL="https://github.com/golang/tools.git"
38+
git clone --quiet --depth=1 "${URL}" "${TMP_DIR}"
39+
pushd "${TMP_DIR}" > /dev/null
40+
popd > /dev/null
41+
42+
# build goimports
43+
BIN_PATH="${TMP_DIR}/cmd/goimports"
44+
pushd "${BIN_PATH}" > /dev/null
45+
echo "Building goimports..."
46+
go build > /dev/null
47+
popd > /dev/null
48+
49+
# check for goimports diffs
50+
diff=$(git ls-files | grep "\.go" | grep -v "\/vendor" | xargs "${BIN_PATH}/goimports" -d 2>&1)
51+
if [[ -n "${diff}" ]]; then
52+
echo "${diff}"
53+
echo
54+
echo "Check failed. Please run hack/update-goimports.sh"
55+
exit 1
56+
fi

0 commit comments

Comments
 (0)