Skip to content

Commit

Permalink
Update formatting script
Browse files Browse the repository at this point in the history
- 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>
  • Loading branch information
ncdc committed Oct 23, 2018
1 parent 6cf3519 commit 573ce7d
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 24 deletions.
4 changes: 4 additions & 0 deletions hack/build-image/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,8 @@ RUN apk add --update --no-cache git bash && \
cd /go/src/k8s.io && \
git clone -b kubernetes-1.11.0 https://github.com/kubernetes/code-generator && \
git clone -b kubernetes-1.11.0 https://github.com/kubernetes/apimachinery && \
go get golang.org/x/tools/cmd/goimports && \
cd /go/src/golang.org/x/tools && \
git checkout 40a48ad93fbe707101afb2099b738471f70594ec && \
go install ./cmd/goimports && \
echo chmod -R a+w /go
55 changes: 48 additions & 7 deletions hack/update-fmt.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash -e
#!/bin/bash
#
# Copyright 2017 the Heptio Ark contributors.
#
Expand All @@ -14,13 +14,54 @@
# See the License for the specific language governing permissions and
# limitations under the License.

HACK_DIR=$(dirname "${BASH_SOURCE}")
set -o errexit
set -o nounset
set -o pipefail

echo "Updating formatting"
if [[ ${1:-} == '--verify' ]]; then
# List file diffs that need formatting updates
MODE='-d'
ACTION='Verifying'
else
# Write formatting updates to files
MODE='-w'
ACTION='Updating'
fi

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

command -v goimports > /dev/null || go get golang.org/x/tools/cmd/goimports
goimports -w -d $(find . -type f -name "*.go" -not -path "./vendor/*" -not -path "./pkg/generated/*" -not -name "zz_generated*")
files="$(find . -type f -name '*.go' -not -path './vendor/*' -not -path './pkg/generated/*' -not -name 'zz_generated*')"
echo "${ACTION} gofmt"
for file in ${files}; do
output=$(gofmt "${MODE}" -s "${file}")
if [[ -n "${output}" ]]; then
VERIFY_FMT_FAILED=1
echo "${output}"
fi
done
if [[ -n "${VERIFY_FMT_FAILED:-}" ]]; then
echo "${ACTION} gofmt - failed! Please run 'make update'."
else
echo "${ACTION} gofmt - done!"
fi

echo "Success!"
echo "${ACTION} goimports"
for file in ${files}; do
output=$(goimports "${MODE}" -local github.com/heptio/ark "${file}")
if [[ -n "${output}" ]]; then
VERIFY_IMPORTS_FAILED=1
echo "${output}"
fi
done
if [[ -n "${VERIFY_IMPORTS_FAILED:-}" ]]; then
echo "${ACTION} goimports - failed! Please run 'make update'."
else
echo "${ACTION} goimports - done!"
fi

if [[ -n "${VERIFY_FMT_FAILED:-}" || -n "${VERIFY_IMPORTS_FAILED:-}" ]]; then
exit 1
fi
20 changes: 3 additions & 17 deletions hack/verify-fmt.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash -e
#!/bin/bash
#
# Copyright 2017 the Heptio Ark contributors.
#
Expand All @@ -14,19 +14,5 @@
# See the License for the specific language governing permissions and
# limitations under the License.

HACK_DIR=$(dirname "${BASH_SOURCE}")

echo "Verifying gofmt"
files=$(gofmt -l -s $(find . -type f -name "*.go" -not -path "./vendor/*" -not -path "./pkg/generated/*" -not -name "zz_generated*"))
if [[ -n "${files}" ]]; then
echo "The following files need gofmt updating - please run 'make update'"
echo "${files}"
exit 1
fi
echo "Success!"

echo "Verifying goimports"
command -v goimports > /dev/null || go get golang.org/x/tools/cmd/goimports
goimports -l $(find . -type f -name "*.go" -not -path "./vendor/*" -not -path "./pkg/generated/*" -not -name "zz_generated*")
echo "Success!"

HACK_DIR=$(dirname "${BASH_SOURCE[0]}")
"${HACK_DIR}"/update-fmt.sh --verify

0 comments on commit 573ce7d

Please sign in to comment.