Skip to content

Commit 573ce7d

Browse files
committed
Update formatting script
- Pin to a specific revision of goimports - Use -local flag with goimports to keep ark imports separated - Correct shellcheck errors Signed-off-by: Andy Goldstein <andy.goldstein@gmail.com>
1 parent 6cf3519 commit 573ce7d

File tree

3 files changed

+55
-24
lines changed

3 files changed

+55
-24
lines changed

hack/build-image/Dockerfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,8 @@ RUN apk add --update --no-cache git bash && \
1919
cd /go/src/k8s.io && \
2020
git clone -b kubernetes-1.11.0 https://github.com/kubernetes/code-generator && \
2121
git clone -b kubernetes-1.11.0 https://github.com/kubernetes/apimachinery && \
22+
go get golang.org/x/tools/cmd/goimports && \
23+
cd /go/src/golang.org/x/tools && \
24+
git checkout 40a48ad93fbe707101afb2099b738471f70594ec && \
25+
go install ./cmd/goimports && \
2226
echo chmod -R a+w /go

hack/update-fmt.sh

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/bin/bash -e
1+
#!/bin/bash
22
#
33
# Copyright 2017 the Heptio Ark contributors.
44
#
@@ -14,13 +14,54 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616

17-
HACK_DIR=$(dirname "${BASH_SOURCE}")
17+
set -o errexit
18+
set -o nounset
19+
set -o pipefail
1820

19-
echo "Updating formatting"
21+
if [[ ${1:-} == '--verify' ]]; then
22+
# List file diffs that need formatting updates
23+
MODE='-d'
24+
ACTION='Verifying'
25+
else
26+
# Write formatting updates to files
27+
MODE='-w'
28+
ACTION='Updating'
29+
fi
2030

21-
gofmt -w -s $(find . -type f -name "*.go" -not -path "./vendor/*" -not -path "./pkg/generated/*" -not -name "zz_generated*")
31+
if ! command -v goimports > /dev/null; then
32+
echo 'goimports is missing - please run "go get golang.org/x/tools/cmd/goimports"'
33+
exit 1
34+
fi
2235

23-
command -v goimports > /dev/null || go get golang.org/x/tools/cmd/goimports
24-
goimports -w -d $(find . -type f -name "*.go" -not -path "./vendor/*" -not -path "./pkg/generated/*" -not -name "zz_generated*")
36+
files="$(find . -type f -name '*.go' -not -path './vendor/*' -not -path './pkg/generated/*' -not -name 'zz_generated*')"
37+
echo "${ACTION} gofmt"
38+
for file in ${files}; do
39+
output=$(gofmt "${MODE}" -s "${file}")
40+
if [[ -n "${output}" ]]; then
41+
VERIFY_FMT_FAILED=1
42+
echo "${output}"
43+
fi
44+
done
45+
if [[ -n "${VERIFY_FMT_FAILED:-}" ]]; then
46+
echo "${ACTION} gofmt - failed! Please run 'make update'."
47+
else
48+
echo "${ACTION} gofmt - done!"
49+
fi
2550

26-
echo "Success!"
51+
echo "${ACTION} goimports"
52+
for file in ${files}; do
53+
output=$(goimports "${MODE}" -local github.com/heptio/ark "${file}")
54+
if [[ -n "${output}" ]]; then
55+
VERIFY_IMPORTS_FAILED=1
56+
echo "${output}"
57+
fi
58+
done
59+
if [[ -n "${VERIFY_IMPORTS_FAILED:-}" ]]; then
60+
echo "${ACTION} goimports - failed! Please run 'make update'."
61+
else
62+
echo "${ACTION} goimports - done!"
63+
fi
64+
65+
if [[ -n "${VERIFY_FMT_FAILED:-}" || -n "${VERIFY_IMPORTS_FAILED:-}" ]]; then
66+
exit 1
67+
fi

hack/verify-fmt.sh

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/bin/bash -e
1+
#!/bin/bash
22
#
33
# Copyright 2017 the Heptio Ark contributors.
44
#
@@ -14,19 +14,5 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616

17-
HACK_DIR=$(dirname "${BASH_SOURCE}")
18-
19-
echo "Verifying gofmt"
20-
files=$(gofmt -l -s $(find . -type f -name "*.go" -not -path "./vendor/*" -not -path "./pkg/generated/*" -not -name "zz_generated*"))
21-
if [[ -n "${files}" ]]; then
22-
echo "The following files need gofmt updating - please run 'make update'"
23-
echo "${files}"
24-
exit 1
25-
fi
26-
echo "Success!"
27-
28-
echo "Verifying goimports"
29-
command -v goimports > /dev/null || go get golang.org/x/tools/cmd/goimports
30-
goimports -l $(find . -type f -name "*.go" -not -path "./vendor/*" -not -path "./pkg/generated/*" -not -name "zz_generated*")
31-
echo "Success!"
32-
17+
HACK_DIR=$(dirname "${BASH_SOURCE[0]}")
18+
"${HACK_DIR}"/update-fmt.sh --verify

0 commit comments

Comments
 (0)