From f9689154b4d50158e506ee8ab7455c12aa87091d Mon Sep 17 00:00:00 2001 From: Dominik Schulz Date: Fri, 1 Jun 2018 10:28:20 +0200 Subject: [PATCH] Codequality: add revive linter (#822) --- .revive.toml | 39 +++++++++++++++++++++++ Makefile | 8 +++++ pkg/action/completion.go | 2 +- pkg/backend/crypto/gpg/openpgp/keyring.go | 4 +++ pkg/backend/rcs/git/cli/config.go | 2 +- pkg/config/io_test.go | 4 +-- pkg/cui/recipients.go | 8 ++--- pkg/jsonapi/api_test.go | 2 +- pkg/jsonapi/responses.go | 2 +- pkg/pinentry/pinentry.go | 4 ++- pkg/store/sub/git.go | 2 +- pkg/store/sub/gpg.go | 2 +- pkg/store/sub/rcs.go | 2 +- pkg/store/sub/storage.go | 2 +- tests/init_test.go | 2 +- tests/jsonapi_test.go | 2 +- 16 files changed, 70 insertions(+), 17 deletions(-) create mode 100644 .revive.toml diff --git a/.revive.toml b/.revive.toml new file mode 100644 index 0000000000..cff75f2630 --- /dev/null +++ b/.revive.toml @@ -0,0 +1,39 @@ +# Ignores files with "GENERATED" header, similar to golint +ignoreGeneratedHeader = false + +# Sets the default severity to "warning" +severity = "error" + +# Sets the default failure confidence. This means that linting errors +# with less than 0.8 confidence will be ignored. +confidence = 0.6 + +# Sets the error code for failures with severity "error" +errorCode = 1 + +# Sets the error code for failures with severity "warning" +warningCode = 1 + +[rule.argument-limit] + arguments = [10] +[rule.blank-imports] +[rule.context-as-argument] +[rule.context-keys-type] +[rule.cyclomatic] + arguments = [21] +[rule.dot-imports] +[rule.error-naming] +[rule.error-return] +[rule.error-strings] +[rule.errorf] +[rule.exported] +[rule.if-return] +[rule.increment-decrement] +[rule.indent-error-flow] +[rule.package-comments] +[rule.range] +[rule.receiver-naming] +[rule.time-naming] +[rule.unexported-return] +[rule.var-declaration] +[rule.var-naming] diff --git a/Makefile b/Makefile index 34b2105bbc..33e3079ab6 100644 --- a/Makefile +++ b/Makefile @@ -135,6 +135,14 @@ legal: codequality: @echo ">> CODE QUALITY" + + @echo -n " REVIVE " + @which revive > /dev/null; if [ $$? -ne 0 ]; then \ + $(GO) get -u github.com/mgechev/revive; \ + fi + @revive -formatter friendly -exclude vendor/... ./... + @printf '%s\n' '$(OK)' + @echo -n " FMT " @$(foreach gofile, $(GOFILES_NOVENDOR),\ out=$$(gofmt -s -l -d -e $(gofile) | tee /dev/stderr); if [ -n "$$out" ]; then exit 1; fi;) diff --git a/pkg/action/completion.go b/pkg/action/completion.go index 7e263ce302..15935ffa4c 100644 --- a/pkg/action/completion.go +++ b/pkg/action/completion.go @@ -45,7 +45,7 @@ set -A complete_gopass -- $PASS_LIST %s ` if a == nil { - return fmt.Errorf("Can't parse command options") + return fmt.Errorf("can not parse command options") } var opts []string diff --git a/pkg/backend/crypto/gpg/openpgp/keyring.go b/pkg/backend/crypto/gpg/openpgp/keyring.go index 2c4d1ee40a..b0c8283682 100644 --- a/pkg/backend/crypto/gpg/openpgp/keyring.go +++ b/pkg/backend/crypto/gpg/openpgp/keyring.go @@ -126,6 +126,8 @@ func (g *GPG) writeSecring(fn string) error { return nil } +// revive:disable + // KeysById implements openpgp.Keyring func (g *GPG) KeysById(id uint64) []openpgp.Key { return append(g.secring.KeysById(id), g.pubring.KeysById(id)...) @@ -136,6 +138,8 @@ func (g *GPG) KeysByIdUsage(id uint64, requiredUsage byte) []openpgp.Key { return append(g.secring.KeysByIdUsage(id, requiredUsage), g.pubring.KeysByIdUsage(id, requiredUsage)...) } +// revive:enable + // DecryptionKeys implements openpgp.Keyring func (g *GPG) DecryptionKeys() []openpgp.Key { return append(g.secring.DecryptionKeys(), g.pubring.DecryptionKeys()...) diff --git a/pkg/backend/rcs/git/cli/config.go b/pkg/backend/rcs/git/cli/config.go index 0d25ea588b..da5f63e3ae 100644 --- a/pkg/backend/rcs/git/cli/config.go +++ b/pkg/backend/rcs/git/cli/config.go @@ -41,7 +41,7 @@ func (g *Git) fixConfig(ctx context.Context) error { // InitConfig initialized and preparse the git config func (g *Git) InitConfig(ctx context.Context, userName, userEmail string) error { if userName == "" || userEmail == "" || !strings.Contains(userEmail, "@") { - return fmt.Errorf("Username and Email must not be empty and valid") + return fmt.Errorf("username and email must not be empty and valid") } // set commit identity if err := g.ConfigSet(ctx, "user.name", userName); err != nil { diff --git a/pkg/config/io_test.go b/pkg/config/io_test.go index 4e36d39771..6d340b5540 100644 --- a/pkg/config/io_test.go +++ b/pkg/config/io_test.go @@ -174,7 +174,7 @@ func TestLoadError(t *testing.T) { capture(t, func() error { _, err := load(gcfg) if err == nil { - return fmt.Errorf("Should fail") + return fmt.Errorf("should fail") } return nil }) @@ -198,7 +198,7 @@ func TestDecodeError(t *testing.T) { capture(t, func() error { _, err := load(gcfg) if err == nil { - return fmt.Errorf("Should fail") + return fmt.Errorf("should fail") } return nil }) diff --git a/pkg/cui/recipients.go b/pkg/cui/recipients.go index fd98ff2827..82d86b8540 100644 --- a/pkg/cui/recipients.go +++ b/pkg/cui/recipients.go @@ -183,10 +183,10 @@ func confirmEditRecipients(ctx context.Context, name string, ris recipientInfos) // AskForPrivateKey promts the user to select from a list of private keys func AskForPrivateKey(ctx context.Context, crypto backend.Crypto, name, prompt string) (string, error) { if !ctxutil.IsInteractive(ctx) { - return "", errors.New("Can not select private key without terminal") + return "", errors.New("can not select private key without terminal") } if crypto == nil { - return "", errors.New("Can not select private key without valid crypto backend") + return "", errors.New("can not select private key without valid crypto backend") } kl, err := crypto.ListPrivateKeyIDs(gpg.WithAlwaysTrust(ctx, false)) @@ -194,7 +194,7 @@ func AskForPrivateKey(ctx context.Context, crypto backend.Crypto, name, prompt s return "", err } if len(kl) < 1 { - return "", errors.New("No useable private keys found") + return "", errors.New("no useable private keys found") } for i := 0; i < maxTries; i++ { @@ -241,7 +241,7 @@ func AskForGitConfigUser(ctx context.Context, crypto backend.Crypto, name string return "", "", err } if len(keyList) < 1 { - return "", "", errors.New("No usable private keys found") + return "", "", errors.New("no usable private keys found") } for _, key := range keyList { diff --git a/pkg/jsonapi/api_test.go b/pkg/jsonapi/api_test.go index 6a7692bb65..88f2d71196 100644 --- a/pkg/jsonapi/api_test.go +++ b/pkg/jsonapi/api_test.go @@ -36,7 +36,7 @@ func TestRespondMessageBrokenInput(t *testing.T) { runRespondMessage(t, "", "", "failed to unmarshal JSON message: unexpected end of JSON input", []storedSecret{}) // Empty object - runRespondMessage(t, "{}", "", "Unknown message of type ", []storedSecret{}) + runRespondMessage(t, "{}", "", "unknown message of type ", []storedSecret{}) } func TestRespondMessageQuery(t *testing.T) { diff --git a/pkg/jsonapi/responses.go b/pkg/jsonapi/responses.go index fa5570ee11..a807b1c3fd 100644 --- a/pkg/jsonapi/responses.go +++ b/pkg/jsonapi/responses.go @@ -37,7 +37,7 @@ func (api *API) respondMessage(ctx context.Context, msgBytes []byte) error { case "create": return api.respondCreateEntry(ctx, msgBytes) default: - return fmt.Errorf("Unknown message of type %s", message.Type) + return fmt.Errorf("unknown message of type %s", message.Type) } } diff --git a/pkg/pinentry/pinentry.go b/pkg/pinentry/pinentry.go index eda9236194..5288c5876c 100644 --- a/pkg/pinentry/pinentry.go +++ b/pkg/pinentry/pinentry.go @@ -7,6 +7,8 @@ import ( "io" "os/exec" "strings" + + "github.com/pkg/errors" ) // Client is a pinentry client @@ -77,7 +79,7 @@ func (c *Client) Set(key, value string) error { } line, _, _ := c.out.ReadLine() if string(line) != "OK" { - return fmt.Errorf("Error: %s", line) + return errors.Errorf("error: %s", line) } return nil } diff --git a/pkg/store/sub/git.go b/pkg/store/sub/git.go index 24c26a2579..b858768a5c 100644 --- a/pkg/store/sub/git.go +++ b/pkg/store/sub/git.go @@ -41,7 +41,7 @@ func (s *Store) GitInit(ctx context.Context, un, ue string) error { out.Cyan(ctx, "WARNING: Initializing with no-op (mock) git backend") return nil default: - return fmt.Errorf("Unknown Sync Backend: %d", backend.GetRCSBackend(ctx)) + return fmt.Errorf("unknown Sync Backend: %d", backend.GetRCSBackend(ctx)) } } diff --git a/pkg/store/sub/gpg.go b/pkg/store/sub/gpg.go index 2aa6cf72f7..1ec618987d 100644 --- a/pkg/store/sub/gpg.go +++ b/pkg/store/sub/gpg.go @@ -121,5 +121,5 @@ func (s *Store) importPublicKey(ctx context.Context, r string) error { } return s.crypto.ImportPublicKey(ctx, pk) } - return fmt.Errorf("Public Key not found in store") + return fmt.Errorf("public key not found in store") } diff --git a/pkg/store/sub/rcs.go b/pkg/store/sub/rcs.go index 43356e847b..0146f7e128 100644 --- a/pkg/store/sub/rcs.go +++ b/pkg/store/sub/rcs.go @@ -35,7 +35,7 @@ func (s *Store) initRCSBackend(ctx context.Context) error { // no-op out.Debug(ctx, "Using RCS Backend: noop") default: - return fmt.Errorf("Unknown RCS Backend") + return fmt.Errorf("unknown rcs backend") } return nil } diff --git a/pkg/store/sub/storage.go b/pkg/store/sub/storage.go index cb43c3ef24..3d4b7a72ec 100644 --- a/pkg/store/sub/storage.go +++ b/pkg/store/sub/storage.go @@ -53,7 +53,7 @@ func (s *Store) initStorageBackend(ctx context.Context) error { } s.storage = store default: - return fmt.Errorf("Unknown storage backend") + return fmt.Errorf("unknown storage backend") } return nil } diff --git a/tests/init_test.go b/tests/init_test.go index d65cb07b4c..219eda45d8 100644 --- a/tests/init_test.go +++ b/tests/init_test.go @@ -12,7 +12,7 @@ func TestInit(t *testing.T) { out, err := ts.run("init") assert.Error(t, err) - assert.Equal(t, "[init] Initializing a new password store ...\n\nError: failed to initialize store: failed to read user input: Can not select private key without terminal\n", out) + assert.Equal(t, "[init] Initializing a new password store ...\n\nError: failed to initialize store: failed to read user input: can not select private key without terminal\n", out) ts = newTester(t) defer ts.teardown() diff --git a/tests/jsonapi_test.go b/tests/jsonapi_test.go index dc5de0ffbd..773568f6aa 100644 --- a/tests/jsonapi_test.go +++ b/tests/jsonapi_test.go @@ -61,7 +61,7 @@ func TestJSONAPI(t *testing.T) { // message with empty object response := getMessageResponse(t, ts, "{}") - assert.Equal(t, "{\"error\":\"Unknown message of type \"}", response) + assert.Equal(t, "{\"error\":\"unknown message of type \"}", response) // query for keys with matching one response = getMessageResponse(t, ts, "{\"type\":\"query\",\"query\":\"foo\"}")