From 38ffde5a9d2bd5db44a8304149bc25a9a3ae9fe5 Mon Sep 17 00:00:00 2001 From: emily Date: Fri, 19 May 2017 05:34:17 -0700 Subject: [PATCH] add gofmt checks to Vault and format existing code (#2745) --- Makefile | 22 +++++++++++++------ api/client.go | 2 +- builtin/credential/github/path_config.go | 12 +++++----- .../logical/cassandra/path_creds_create.go | 4 ++-- command/audit_enable.go | 2 +- http/handler.go | 2 +- scripts/gofmtcheck.sh | 11 ++++++++++ vault/barrier_view.go | 1 - 8 files changed, 37 insertions(+), 19 deletions(-) create mode 100755 scripts/gofmtcheck.sh diff --git a/Makefile b/Makefile index 52ab43c42f..839f583c69 100644 --- a/Makefile +++ b/Makefile @@ -3,11 +3,12 @@ VETARGS?=-asmdecl -atomic -bool -buildtags -copylocks -methods -nilfunc -printf EXTERNAL_TOOLS=\ github.com/mitchellh/gox BUILD_TAGS?=vault +GOFMT_FILES?=$$(find . -name '*.go' | grep -v vendor) default: dev # bin generates the releaseable binaries for Vault -bin: generate +bin: fmtcheck generate @CGO_ENABLED=0 BUILD_TAGS='$(BUILD_TAGS)' sh -c "'$(CURDIR)/scripts/build.sh'" # dev creates binaries for testing Vault locally. These are put @@ -15,22 +16,22 @@ bin: generate # is only put into /bin/ quickdev: generate @CGO_ENABLED=0 go build -i -tags='$(BUILD_TAGS)' -o bin/vault -dev: generate +dev: fmtcheck generate @CGO_ENABLED=0 BUILD_TAGS='$(BUILD_TAGS)' VAULT_DEV_BUILD=1 sh -c "'$(CURDIR)/scripts/build.sh'" dev-dynamic: generate @CGO_ENABLED=1 BUILD_TAGS='$(BUILD_TAGS)' VAULT_DEV_BUILD=1 sh -c "'$(CURDIR)/scripts/build.sh'" # test runs the unit tests and vets the code -test: generate +test: fmtcheck generate CGO_ENABLED=0 VAULT_TOKEN= VAULT_ACC= go test -tags='$(BUILD_TAGS)' $(TEST) $(TESTARGS) -timeout=20m -parallel=4 -testcompile: generate +testcompile: fmtcheck generate @for pkg in $(TEST) ; do \ go test -v -c -tags='$(BUILD_TAGS)' $$pkg -parallel=4 ; \ done # testacc runs acceptance tests -testacc: generate +testacc: fmtcheck generate @if [ "$(TEST)" = "./..." ]; then \ echo "ERROR: Set TEST to a specific package"; \ exit 1; \ @@ -38,7 +39,7 @@ testacc: generate VAULT_ACC=1 go test -tags='$(BUILD_TAGS)' $(TEST) -v $(TESTARGS) -timeout 45m # testrace runs the race checker -testrace: generate +testrace: fmtcheck generate CGO_ENABLED=1 VAULT_TOKEN= VAULT_ACC= go test -tags='$(BUILD_TAGS)' -race $(TEST) $(TESTARGS) -timeout=20m -parallel=4 cover: @@ -71,4 +72,11 @@ proto: protoc -I helper/forwarding -I vault -I ../../.. vault/*.proto --go_out=plugins=grpc:vault protoc -I helper/forwarding -I vault -I ../../.. helper/forwarding/types.proto --go_out=plugins=grpc:helper/forwarding -.PHONY: bin default generate test vet bootstrap +fmtcheck: + @sh -c "'$(CURDIR)/scripts/gofmtcheck.sh'" + +fmt: + gofmt -w $(GOFMT_FILES) + + +.PHONY: bin default generate test vet bootstrap fmt fmtcheck diff --git a/api/client.go b/api/client.go index 85b895328b..83748edee6 100644 --- a/api/client.go +++ b/api/client.go @@ -6,11 +6,11 @@ import ( "net/http" "net/url" "os" + "path" "strconv" "strings" "sync" "time" - "path" "golang.org/x/net/http2" diff --git a/builtin/credential/github/path_config.go b/builtin/credential/github/path_config.go index 9db2e64593..c211450569 100644 --- a/builtin/credential/github/path_config.go +++ b/builtin/credential/github/path_config.go @@ -5,9 +5,9 @@ import ( "net/url" "time" + "github.com/fatih/structs" "github.com/hashicorp/vault/logical" "github.com/hashicorp/vault/logical/framework" - "github.com/fatih/structs" ) func pathConfig(b *backend) *framework.Path { @@ -37,7 +37,7 @@ API-compatible authentication server.`, Callbacks: map[logical.Operation]framework.OperationFunc{ logical.UpdateOperation: b.pathConfigWrite, - logical.ReadOperation: b.pathConfigRead, + logical.ReadOperation: b.pathConfigRead, }, } } @@ -77,10 +77,10 @@ func (b *backend) pathConfigWrite( } entry, err := logical.StorageEntryJSON("config", config{ - Organization: organization, - BaseURL: baseURL, - TTL: ttl, - MaxTTL: maxTTL, + Organization: organization, + BaseURL: baseURL, + TTL: ttl, + MaxTTL: maxTTL, }) if err != nil { diff --git a/builtin/logical/cassandra/path_creds_create.go b/builtin/logical/cassandra/path_creds_create.go index 4b025ba972..98981cefea 100644 --- a/builtin/logical/cassandra/path_creds_create.go +++ b/builtin/logical/cassandra/path_creds_create.go @@ -61,14 +61,14 @@ func (b *backend) pathCredsCreateRead( if err != nil { return nil, err } - + // Set consistency if role.Consistency != "" { consistencyValue, err := gocql.ParseConsistencyWrapper(role.Consistency) if err != nil { return nil, err } - + session.SetConsistency(consistencyValue) } diff --git a/command/audit_enable.go b/command/audit_enable.go index 3293c792f9..8c14b99382 100644 --- a/command/audit_enable.go +++ b/command/audit_enable.go @@ -72,7 +72,7 @@ func (c *AuditEnableCommand) Run(args []string) int { } err = client.Sys().EnableAuditWithOptions(path, &api.EnableAuditOptions{ - Type: auditType, + Type: auditType, Description: desc, Options: opts, Local: local, diff --git a/http/handler.go b/http/handler.go index fb9b7a8a1e..9c5a871985 100644 --- a/http/handler.go +++ b/http/handler.go @@ -10,8 +10,8 @@ import ( "github.com/hashicorp/errwrap" "github.com/hashicorp/vault/helper/consts" - "github.com/hashicorp/vault/helper/parseutil" "github.com/hashicorp/vault/helper/jsonutil" + "github.com/hashicorp/vault/helper/parseutil" "github.com/hashicorp/vault/logical" "github.com/hashicorp/vault/vault" ) diff --git a/scripts/gofmtcheck.sh b/scripts/gofmtcheck.sh new file mode 100755 index 0000000000..574f4d7167 --- /dev/null +++ b/scripts/gofmtcheck.sh @@ -0,0 +1,11 @@ +#!/usr/bin/env bash + +echo "==> Checking that code complies with gofmt requirements..." + +gofmt_files=$(gofmt -l `find . -name '*.go' | grep -v vendor`) +if [[ -n ${gofmt_files} ]]; then + echo 'gofmt needs running on the following files:' + echo "${gofmt_files}" + echo "You can use the command: \`make fmt\` to reformat code." + exit 1 +fi diff --git a/vault/barrier_view.go b/vault/barrier_view.go index 0fa6f2d78f..59a0cae3cd 100644 --- a/vault/barrier_view.go +++ b/vault/barrier_view.go @@ -98,7 +98,6 @@ func (v *BarrierView) Delete(key string) error { return logical.ErrReadOnly } - return v.barrier.Delete(expandedKey) }