Skip to content

Commit

Permalink
refactor: apply linting fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Integralist committed Nov 6, 2023
1 parent ff205d2 commit 7a4c4e2
Show file tree
Hide file tree
Showing 96 changed files with 938 additions and 721 deletions.
169 changes: 169 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
# https://golangci-lint.run/usage/configuration/

issues:
max-per-linter: 0
max-same-issues: 0
run:
allow-parallel-runners: true
linters:
disable-all: true
enable:
- bodyclose
- durationcheck
- errcheck
- exhaustive
- exportloopref
- forcetypeassert
- gocritic
- godot
- gofumpt
- goimports
- gosec
- gosimple
- govet
- ineffassign
- makezero
- misspell
- nilerr
- predeclared
- revive
- staticcheck
- tenv
- typecheck
- unconvert
- unparam
- unused
- vet
linters-settings:
revive:
enableAllRules: true
rules:
- name: "add-constant"
severity: "warning"
arguments:
- maxLitCount: "10"
allowInts: "0,1,2,3,4,5"
disabled: true # most of the time it's fine, and the performance isn't a concern
- name: "argument-limit"
severity: "warning"
arguments: [6]
- name: "atomic"
severity: "warning"
- name: "bare-return"
severity: "warning"
- name: "bool-literal-in-expr"
severity: "warning"
- name: "confusing-naming"
severity: "warning"
- name: "confusing-results"
severity: "warning"
- name: "constant-logical-expr"
severity: "error"
- name: "context-as-argument"
severity: "error"
- name: "context-keys-type"
severity: "error"
- name: "deep-exit"
severity: "warning"
- name: "defer"
severity: "warning"
- name: "early-return"
severity: "warning"
- name: "empty-block"
severity: "error"
- name: "empty-lines"
severity: "warning"
- name: "error-naming"
severity: "error"
- name: "error-return"
severity: "error"
- name: "error-strings"
severity: "error"
- name: "errorf"
severity: "warning"
- name: "exported"
severity: "error"
- name: "flag-parameter"
severity: "warning"
- name: "function-result-limit"
severity: "warning"
arguments: [4]
- name: "function-length"
severity: "warning"
arguments: [50, 0]
- name: "get-return"
severity: "error"
- name: "identical-branches"
severity: "error"
- name: "if-return"
severity: "warning"
- name: "increment-decrement"
severity: "error"
- name: "indent-error-flow"
severity: "warning"
- name: "import-shadowing"
severity: "warning"
- name: "modifies-parameter"
severity: "warning"
- name: "modifies-value-receiver"
severity: "warning"
- name: "nested-structs"
severity: "warning"
- name: "optimize-operands-order"
severity: "warning"
- name: "package-comments"
severity: "warning"
- name: "range"
severity: "warning"
- name: "range-val-in-closure"
severity: "warning"
- name: "range-val-address"
severity: "warning"
- name: "receiver-naming"
severity: "warning"
- name: "redefines-builtin-id"
severity: "error"
- name: "string-of-int"
severity: "warning"
- name: "struct-tag"
severity: "warning"
- name: "superfluous-else"
severity: "warning"
- name: "time-equal"
severity: "warning"
- name: "time-naming"
severity: "warning"
- name: "var-declaration"
severity: "warning"
- name: "var-naming"
severity: "warning"
- name: "unconditional-recursion"
severity: "error"
- name: "unexported-naming"
severity: "warning"
- name: "unexported-return"
severity: "error"
- name: "unhandled-error"
severity: "warning"
arguments:
- "fmt.Print"
- "fmt.Printf"
- "fmt.Println"
- "fmt.Fprint"
- "fmt.Fprintf"
- "fmt.Fprintln"
- name: "unnecessary-stmt"
severity: "warning"
- name: "unreachable-code"
severity: "warning"
- name: "unused-parameter"
severity: "warning"
- name: "unused-receiver"
severity: "warning"
disabled: true
- name: "use-any"
severity: "warning"
- name: "useless-break"
severity: "warning"
- name: "waitgroup-by-value"
severity: "warning"
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ semgrep: ## Run semgrep
staticcheck: ## Run static analysis
staticcheck ./{cmd,pkg}/...

.PHONY: golangci
golangci: ## Run golangci-lint
golangci-lint run --verbose

# Run tests
.PHONY: test
test: config ## Run tests (with race detection)
Expand Down
2 changes: 1 addition & 1 deletion pkg/api/undocumented/undocumented.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (e APIError) Error() string {
return e.Err.Error()
}

// NewError returns an APIError
// NewError returns an APIError.
func NewError(err error, statusCode int) APIError {
return APIError{
Err: err,
Expand Down
88 changes: 44 additions & 44 deletions pkg/app/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,14 @@ import (
"github.com/fastly/cli/pkg/commands/serviceversion"
"github.com/fastly/cli/pkg/commands/shellcomplete"
"github.com/fastly/cli/pkg/commands/stats"
tlsConfig "github.com/fastly/cli/pkg/commands/tls/config"
tlsCustom "github.com/fastly/cli/pkg/commands/tls/custom"
tlsCustomActivation "github.com/fastly/cli/pkg/commands/tls/custom/activation"
tlsCustomCertificate "github.com/fastly/cli/pkg/commands/tls/custom/certificate"
tlsCustomDomain "github.com/fastly/cli/pkg/commands/tls/custom/domain"
tlsCustomPrivateKey "github.com/fastly/cli/pkg/commands/tls/custom/privatekey"
tlsPlatform "github.com/fastly/cli/pkg/commands/tls/platform"
tlsSubscription "github.com/fastly/cli/pkg/commands/tls/subscription"
tlsconfig "github.com/fastly/cli/pkg/commands/tls/config"
tlscustom "github.com/fastly/cli/pkg/commands/tls/custom"
tlscustomactivation "github.com/fastly/cli/pkg/commands/tls/custom/activation"
tlscustomcertificate "github.com/fastly/cli/pkg/commands/tls/custom/certificate"
tlscustomdomain "github.com/fastly/cli/pkg/commands/tls/custom/domain"
tlscustomprivatekey "github.com/fastly/cli/pkg/commands/tls/custom/privatekey"
tlsplatform "github.com/fastly/cli/pkg/commands/tls/platform"
tlssubscription "github.com/fastly/cli/pkg/commands/tls/subscription"
"github.com/fastly/cli/pkg/commands/update"
"github.com/fastly/cli/pkg/commands/user"
"github.com/fastly/cli/pkg/commands/vcl"
Expand Down Expand Up @@ -386,42 +386,42 @@ func defineCommands(
statsHistorical := stats.NewHistoricalCommand(statsCmdRoot.CmdClause, g, m)
statsRealtime := stats.NewRealtimeCommand(statsCmdRoot.CmdClause, g, m)
statsRegions := stats.NewRegionsCommand(statsCmdRoot.CmdClause, g)
tlsConfigCmdRoot := tlsConfig.NewRootCommand(app, g)
tlsConfigDescribe := tlsConfig.NewDescribeCommand(tlsConfigCmdRoot.CmdClause, g, m)
tlsConfigList := tlsConfig.NewListCommand(tlsConfigCmdRoot.CmdClause, g, m)
tlsConfigUpdate := tlsConfig.NewUpdateCommand(tlsConfigCmdRoot.CmdClause, g, m)
tlsCustomCmdRoot := tlsCustom.NewRootCommand(app, g)
tlsCustomActivationCmdRoot := tlsCustomActivation.NewRootCommand(tlsCustomCmdRoot.CmdClause, g)
tlsCustomActivationCreate := tlsCustomActivation.NewCreateCommand(tlsCustomActivationCmdRoot.CmdClause, g, m)
tlsCustomActivationDelete := tlsCustomActivation.NewDeleteCommand(tlsCustomActivationCmdRoot.CmdClause, g, m)
tlsCustomActivationDescribe := tlsCustomActivation.NewDescribeCommand(tlsCustomActivationCmdRoot.CmdClause, g, m)
tlsCustomActivationList := tlsCustomActivation.NewListCommand(tlsCustomActivationCmdRoot.CmdClause, g, m)
tlsCustomActivationUpdate := tlsCustomActivation.NewUpdateCommand(tlsCustomActivationCmdRoot.CmdClause, g, m)
tlsCustomCertificateCmdRoot := tlsCustomCertificate.NewRootCommand(tlsCustomCmdRoot.CmdClause, g)
tlsCustomCertificateCreate := tlsCustomCertificate.NewCreateCommand(tlsCustomCertificateCmdRoot.CmdClause, g, m)
tlsCustomCertificateDelete := tlsCustomCertificate.NewDeleteCommand(tlsCustomCertificateCmdRoot.CmdClause, g, m)
tlsCustomCertificateDescribe := tlsCustomCertificate.NewDescribeCommand(tlsCustomCertificateCmdRoot.CmdClause, g, m)
tlsCustomCertificateList := tlsCustomCertificate.NewListCommand(tlsCustomCertificateCmdRoot.CmdClause, g, m)
tlsCustomCertificateUpdate := tlsCustomCertificate.NewUpdateCommand(tlsCustomCertificateCmdRoot.CmdClause, g, m)
tlsCustomDomainCmdRoot := tlsCustomDomain.NewRootCommand(tlsCustomCmdRoot.CmdClause, g)
tlsCustomDomainList := tlsCustomDomain.NewListCommand(tlsCustomDomainCmdRoot.CmdClause, g, m)
tlsCustomPrivateKeyCmdRoot := tlsCustomPrivateKey.NewRootCommand(tlsCustomCmdRoot.CmdClause, g)
tlsCustomPrivateKeyCreate := tlsCustomPrivateKey.NewCreateCommand(tlsCustomPrivateKeyCmdRoot.CmdClause, g, m)
tlsCustomPrivateKeyDelete := tlsCustomPrivateKey.NewDeleteCommand(tlsCustomPrivateKeyCmdRoot.CmdClause, g, m)
tlsCustomPrivateKeyDescribe := tlsCustomPrivateKey.NewDescribeCommand(tlsCustomPrivateKeyCmdRoot.CmdClause, g, m)
tlsCustomPrivateKeyList := tlsCustomPrivateKey.NewListCommand(tlsCustomPrivateKeyCmdRoot.CmdClause, g, m)
tlsPlatformCmdRoot := tlsPlatform.NewRootCommand(app, g)
tlsPlatformCreate := tlsPlatform.NewCreateCommand(tlsPlatformCmdRoot.CmdClause, g, m)
tlsPlatformDelete := tlsPlatform.NewDeleteCommand(tlsPlatformCmdRoot.CmdClause, g, m)
tlsPlatformDescribe := tlsPlatform.NewDescribeCommand(tlsPlatformCmdRoot.CmdClause, g, m)
tlsPlatformList := tlsPlatform.NewListCommand(tlsPlatformCmdRoot.CmdClause, g, m)
tlsPlatformUpdate := tlsPlatform.NewUpdateCommand(tlsPlatformCmdRoot.CmdClause, g, m)
tlsSubscriptionCmdRoot := tlsSubscription.NewRootCommand(app, g)
tlsSubscriptionCreate := tlsSubscription.NewCreateCommand(tlsSubscriptionCmdRoot.CmdClause, g, m)
tlsSubscriptionDelete := tlsSubscription.NewDeleteCommand(tlsSubscriptionCmdRoot.CmdClause, g, m)
tlsSubscriptionDescribe := tlsSubscription.NewDescribeCommand(tlsSubscriptionCmdRoot.CmdClause, g, m)
tlsSubscriptionList := tlsSubscription.NewListCommand(tlsSubscriptionCmdRoot.CmdClause, g, m)
tlsSubscriptionUpdate := tlsSubscription.NewUpdateCommand(tlsSubscriptionCmdRoot.CmdClause, g, m)
tlsConfigCmdRoot := tlsconfig.NewRootCommand(app, g)
tlsConfigDescribe := tlsconfig.NewDescribeCommand(tlsConfigCmdRoot.CmdClause, g, m)
tlsConfigList := tlsconfig.NewListCommand(tlsConfigCmdRoot.CmdClause, g, m)
tlsConfigUpdate := tlsconfig.NewUpdateCommand(tlsConfigCmdRoot.CmdClause, g, m)
tlsCustomCmdRoot := tlscustom.NewRootCommand(app, g)
tlsCustomActivationCmdRoot := tlscustomactivation.NewRootCommand(tlsCustomCmdRoot.CmdClause, g)
tlsCustomActivationCreate := tlscustomactivation.NewCreateCommand(tlsCustomActivationCmdRoot.CmdClause, g, m)
tlsCustomActivationDelete := tlscustomactivation.NewDeleteCommand(tlsCustomActivationCmdRoot.CmdClause, g, m)
tlsCustomActivationDescribe := tlscustomactivation.NewDescribeCommand(tlsCustomActivationCmdRoot.CmdClause, g, m)
tlsCustomActivationList := tlscustomactivation.NewListCommand(tlsCustomActivationCmdRoot.CmdClause, g, m)
tlsCustomActivationUpdate := tlscustomactivation.NewUpdateCommand(tlsCustomActivationCmdRoot.CmdClause, g, m)
tlsCustomCertificateCmdRoot := tlscustomcertificate.NewRootCommand(tlsCustomCmdRoot.CmdClause, g)
tlsCustomCertificateCreate := tlscustomcertificate.NewCreateCommand(tlsCustomCertificateCmdRoot.CmdClause, g, m)
tlsCustomCertificateDelete := tlscustomcertificate.NewDeleteCommand(tlsCustomCertificateCmdRoot.CmdClause, g, m)
tlsCustomCertificateDescribe := tlscustomcertificate.NewDescribeCommand(tlsCustomCertificateCmdRoot.CmdClause, g, m)
tlsCustomCertificateList := tlscustomcertificate.NewListCommand(tlsCustomCertificateCmdRoot.CmdClause, g, m)
tlsCustomCertificateUpdate := tlscustomcertificate.NewUpdateCommand(tlsCustomCertificateCmdRoot.CmdClause, g, m)
tlsCustomDomainCmdRoot := tlscustomdomain.NewRootCommand(tlsCustomCmdRoot.CmdClause, g)
tlsCustomDomainList := tlscustomdomain.NewListCommand(tlsCustomDomainCmdRoot.CmdClause, g, m)
tlsCustomPrivateKeyCmdRoot := tlscustomprivatekey.NewRootCommand(tlsCustomCmdRoot.CmdClause, g)
tlsCustomPrivateKeyCreate := tlscustomprivatekey.NewCreateCommand(tlsCustomPrivateKeyCmdRoot.CmdClause, g, m)
tlsCustomPrivateKeyDelete := tlscustomprivatekey.NewDeleteCommand(tlsCustomPrivateKeyCmdRoot.CmdClause, g, m)
tlsCustomPrivateKeyDescribe := tlscustomprivatekey.NewDescribeCommand(tlsCustomPrivateKeyCmdRoot.CmdClause, g, m)
tlsCustomPrivateKeyList := tlscustomprivatekey.NewListCommand(tlsCustomPrivateKeyCmdRoot.CmdClause, g, m)
tlsPlatformCmdRoot := tlsplatform.NewRootCommand(app, g)
tlsPlatformCreate := tlsplatform.NewCreateCommand(tlsPlatformCmdRoot.CmdClause, g, m)
tlsPlatformDelete := tlsplatform.NewDeleteCommand(tlsPlatformCmdRoot.CmdClause, g, m)
tlsPlatformDescribe := tlsplatform.NewDescribeCommand(tlsPlatformCmdRoot.CmdClause, g, m)
tlsPlatformList := tlsplatform.NewListCommand(tlsPlatformCmdRoot.CmdClause, g, m)
tlsPlatformUpdate := tlsplatform.NewUpdateCommand(tlsPlatformCmdRoot.CmdClause, g, m)
tlsSubscriptionCmdRoot := tlssubscription.NewRootCommand(app, g)
tlsSubscriptionCreate := tlssubscription.NewCreateCommand(tlsSubscriptionCmdRoot.CmdClause, g, m)
tlsSubscriptionDelete := tlssubscription.NewDeleteCommand(tlsSubscriptionCmdRoot.CmdClause, g, m)
tlsSubscriptionDescribe := tlssubscription.NewDescribeCommand(tlsSubscriptionCmdRoot.CmdClause, g, m)
tlsSubscriptionList := tlssubscription.NewListCommand(tlsSubscriptionCmdRoot.CmdClause, g, m)
tlsSubscriptionUpdate := tlssubscription.NewUpdateCommand(tlsSubscriptionCmdRoot.CmdClause, g, m)
updateRoot := update.NewRootCommand(app, opts.ConfigPath, opts.Versioners.CLI, g)
userCmdRoot := user.NewRootCommand(app, g)
userCreate := user.NewCreateCommand(userCmdRoot.CmdClause, g, m)
Expand Down
8 changes: 7 additions & 1 deletion pkg/app/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,10 @@ func Run(opts RunOpts) error {
fmt.Fprintf(opts.Stdout, "Fastly API endpoint (via %s): %s\n\n", env.Endpoint, endpoint)
case lookup.SourceFile:
fmt.Fprintf(opts.Stdout, "Fastly API endpoint (via config file): %s\n\n", endpoint)
case lookup.SourceFlag:
fmt.Fprintf(opts.Stdout, "Fastly API endpoint provided via --endpoint\n\n")
case lookup.SourceDefault, lookup.SourceUndefined:
fallthrough
default:
fmt.Fprintf(opts.Stdout, "Fastly API endpoint: %s\n\n", endpoint)
}
Expand Down Expand Up @@ -206,7 +210,7 @@ func Run(opts RunOpts) error {
return command.Exec(opts.Stdin, opts.Stdout)
}

// RunOpts represent arguments to Run()
// RunOpts represent arguments to Run().
type RunOpts struct {
APIClient APIClientFactory
Args []string
Expand Down Expand Up @@ -245,6 +249,8 @@ func displayTokenSource(source lookup.Source, out io.Writer, token, profileSourc
fmt.Fprintf(out, "Fastly API token provided via %s\n", token)
case lookup.SourceFile:
fmt.Fprintf(out, "Fastly API token provided via config file (profile: %s)\n", profileSource)
case lookup.SourceDefault, lookup.SourceUndefined:
fallthrough
default:
fmt.Fprintf(out, "Fastly API token not provided\n")
}
Expand Down
39 changes: 21 additions & 18 deletions pkg/app/usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,26 +368,10 @@ func processCommandInput(
// here by calling Parse, again swapping the Writers. This also ensures the
// larger and more verbose help formatting is used.
if cmdName == "help" {
var buf bytes.Buffer
app.Writers(&buf, io.Discard)
_, _ = app.Parse(opts.Args)
app.Writers(opts.Stdout, io.Discard)

// The full-fat output of `fastly help` should have a hint at the bottom
// for more specific help. Unfortunately I don't know of a better way to
// distinguish `fastly help` from e.g. `fastly help pops` than this check.
if len(opts.Args) > 0 && opts.Args[len(opts.Args)-1] == "help" {
fmt.Fprintln(&buf, "\nFor help on a specific command, try e.g.")
fmt.Fprintln(&buf, "")
fmt.Fprintln(&buf, "\tfastly help profile")
fmt.Fprintln(&buf, "\tfastly profile --help")
fmt.Fprintln(&buf, "")
}

return command, cmdName, fsterr.SkipExitError{
Skip: true,
Err: fsterr.RemediationError{
Prefix: buf.String(),
Prefix: useFullHelpOutput(app, opts).String(),
},
}
}
Expand All @@ -404,6 +388,25 @@ func processCommandInput(
return command, cmdName, nil
}

func useFullHelpOutput(app *kingpin.Application, opts RunOpts) *bytes.Buffer {
var buf bytes.Buffer
app.Writers(&buf, io.Discard)
_, _ = app.Parse(opts.Args)
app.Writers(opts.Stdout, io.Discard)

// The full-fat output of `fastly help` should have a hint at the bottom
// for more specific help. Unfortunately I don't know of a better way to
// distinguish `fastly help` from e.g. `fastly help pops` than this check.
if len(opts.Args) > 0 && opts.Args[len(opts.Args)-1] == "help" {
fmt.Fprintln(&buf, "\nFor help on a specific command, try e.g.")
fmt.Fprintln(&buf, "")
fmt.Fprintln(&buf, "\tfastly help profile")
fmt.Fprintln(&buf, "\tfastly profile --help")
fmt.Fprintln(&buf, "")
}
return &buf
}

// metadata is combined into the usage output so the Developer Hub can display
// additional information about how to use the commands and what APIs they call.
// e.g. https://developer.fastly.com/reference/cli/vcl/snippet/create/
Expand Down Expand Up @@ -553,7 +556,7 @@ func recurse(n int, segs []string, data commandsMetadata) commandsMetadata {
return nil
}

// resolveToString extracts a value from a map as a string
// resolveToString extracts a value from a map as a string.
func resolveToString(i any, key string) string {
m, ok := i.(map[string]any)
if ok {
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func (sv *OptionalServiceVersion) Parse(sid string, client api.Interface) (*fast
case "": // no --version flag provided
v, err = GetActiveVersion(vs)
if err != nil {
return vs[0], nil // if no active version, return latest version
return vs[0], nil //lint:ignore nilerr if no active version, return latest version
}
default:
v, err = GetSpecifiedVersion(vs, sv.Value)
Expand Down

0 comments on commit 7a4c4e2

Please sign in to comment.