From 389a7592bc19edc22b8f41b20feed11479655af8 Mon Sep 17 00:00:00 2001 From: Navid Shaikh Date: Mon, 8 Jul 2019 15:47:33 +0530 Subject: [PATCH] Adds step to simplify code along with goimports (#239) (and gofmt if goimports isn't available) - Runs goimports on all packages except vendor dir and filter on only *.go files - Runs gofmt on all dirs except vendor and filters on *.go files, also writes the results instead of printing diff - Fixes typo s/insteat/insead - Adds the changes after adding simply code step in hack/build.sh - Uses source_dir var specifying the dirs to work on - Combines gofmt commands into single command --- hack/build.sh | 9 ++++++--- pkg/serving/config_changes.go | 2 +- pkg/serving/config_changes_test.go | 6 +++--- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/hack/build.sh b/hack/build.sh index a65a3ba2f..ebf5ccc3a 100755 --- a/hack/build.sh +++ b/hack/build.sh @@ -16,6 +16,8 @@ set -o pipefail +source_dirs="cmd pkg test" + # Store for later if [ -z "$1" ]; then ARGS=("") @@ -81,7 +83,7 @@ run() { go_fmt() { echo "🧹 ${S}Format" - go fmt ./cmd/... ./pkg/... + find $(echo $source_dirs) -name "*.go" -print0 | xargs -0 gofmt -s -w } source_format() { @@ -92,11 +94,12 @@ source_format() { echo "✋ go get golang.org/x/tools/cmd/goimports" echo "✋ to enable import cleanup. Import cleanup skipped." - # Run go fmt insteat + # Run go fmt instead go_fmt else echo "🧽 ${S}Format" - goimports -w cmd pkg + goimports -w $(echo $source_dirs) + find $(echo $source_dirs) -name "*.go" -print0 | xargs -0 gofmt -s -w fi set -e } diff --git a/pkg/serving/config_changes.go b/pkg/serving/config_changes.go index a409a7b00..b4bbebddf 100644 --- a/pkg/serving/config_changes.go +++ b/pkg/serving/config_changes.go @@ -146,7 +146,7 @@ func extractContainer(template *servingv1alpha1.RevisionTemplateSpec) (*corev1.C func updateEnvVarsFromMap(env []corev1.EnvVar, vars map[string]string) []corev1.EnvVar { set := make(map[string]bool) - for i, _ := range env { + for i := range env { envVar := &env[i] value, present := vars[envVar.Name] if present { diff --git a/pkg/serving/config_changes_test.go b/pkg/serving/config_changes_test.go index f35149f2a..51dbcda9a 100644 --- a/pkg/serving/config_changes_test.go +++ b/pkg/serving/config_changes_test.go @@ -118,7 +118,7 @@ func TestUpdateEnvVarsModify(t *testing.T) { func testUpdateEnvVarsModify(t *testing.T, revision *servingv1alpha1.RevisionTemplateSpec, container *corev1.Container) { container.Env = []corev1.EnvVar{ - corev1.EnvVar{Name: "a", Value: "foo"}} + {Name: "a", Value: "foo"}} env := map[string]string{ "a": "fancy", } @@ -176,8 +176,8 @@ func TestUpdateEnvVarsBoth(t *testing.T) { func testUpdateEnvVarsBoth(t *testing.T, template *servingv1alpha1.RevisionTemplateSpec, container *corev1.Container) { container.Env = []corev1.EnvVar{ - corev1.EnvVar{Name: "a", Value: "foo"}, - corev1.EnvVar{Name: "c", Value: "caroline"}} + {Name: "a", Value: "foo"}, + {Name: "c", Value: "caroline"}} env := map[string]string{ "a": "fancy", "b": "boo",