Skip to content

Commit

Permalink
Changed to golangci-lint and introduced LintChanges command (#3057)
Browse files Browse the repository at this point in the history
* Changed to golangci-lint and introduced LintChanges command

Signed-off-by: David Gannon <[email protected]>

---------

Signed-off-by: David Gannon <[email protected]>
  • Loading branch information
dgannon991 authored May 18, 2024
1 parent a20fd48 commit 67f1dd8
Show file tree
Hide file tree
Showing 73 changed files with 329 additions and 243 deletions.
2 changes: 1 addition & 1 deletion cmd/exec/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func main() {
defer func() {
// Capture panics and trace them
if panicErr := recover(); panicErr != nil {
log.Error(fmt.Errorf("%s", panicErr),
_ = log.Error(fmt.Errorf("%s", panicErr),
attribute.Bool("panic", true),
attribute.String("stackTrace", string(debug.Stack())))
log.EndSpan()
Expand Down
2 changes: 1 addition & 1 deletion cmd/porter/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ The docker driver builds the bundle image using the local Docker host. To use a
"Path to the build context directory where all bundle assets are located. Defaults to the current directory.")
f.StringVar(&opts.Driver, "driver", porter.BuildDriverDefault,
fmt.Sprintf("Driver for building the invocation image. Allowed values are: %s", strings.Join(porter.BuildDriverAllowedValues, ", ")))
f.MarkHidden("driver") // Hide the driver flag since there aren't any choices to make right now
_ = f.MarkHidden("driver") // Hide the driver flag since there aren't any choices to make right now
f.StringArrayVar(&opts.BuildArgs, "build-arg", nil,
"Set build arguments in the template Dockerfile (format: NAME=VALUE). May be specified multiple times. Max length is 5,000 characters.")
f.StringArrayVar(&opts.SSH, "ssh", nil,
Expand Down
5 changes: 3 additions & 2 deletions cmd/porter/bundle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,10 @@ func TestBuildValidate_Driver(t *testing.T) {
// noop
return nil
}
p.FileSystem.WriteFile("porter.yaml", []byte(""), pkg.FileModeWritable)
err := p.FileSystem.WriteFile("porter.yaml", []byte(""), pkg.FileModeWritable)
require.NoError(t, err)

err := rootCmd.Execute()
err = rootCmd.Execute()
if tc.wantError == "" {
require.NoError(t, err)
assert.Equal(t, tc.wantDriver, p.Data.BuildDriver)
Expand Down
11 changes: 6 additions & 5 deletions cmd/porter/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,18 @@ For additional details see: https://porter.sh/install#command-completion`,
DisableFlagsInUseLine: true,
ValidArgs: []string{"bash", "zsh", "fish", "powershell"},
Args: cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs),
Run: func(cmd *cobra.Command, args []string) {
RunE: func(cmd *cobra.Command, args []string) error {
switch args[0] {
case "bash":
cmd.Root().GenBashCompletion(p.Out)
return cmd.Root().GenBashCompletion(p.Out)
case "zsh":
cmd.Root().GenZshCompletion(p.Out)
return cmd.Root().GenZshCompletion(p.Out)
case "fish":
cmd.Root().GenFishCompletion(p.Out, true)
return cmd.Root().GenFishCompletion(p.Out, true)
case "powershell":
cmd.Root().GenPowerShellCompletionWithDesc(p.Out)
return cmd.Root().GenPowerShellCompletionWithDesc(p.Out)
}
return nil
},
}
cmd.Annotations = map[string]string{
Expand Down
2 changes: 1 addition & 1 deletion cmd/porter/installations.go
Original file line number Diff line number Diff line change
Expand Up @@ -435,5 +435,5 @@ func addBundleActionFlags(f *pflag.FlagSet, actionOpts porter.BundleAction) {

// Gracefully support any renamed flags
f.StringArrayVar(&opts.CredentialIdentifiers, "cred", nil, "DEPRECATED")
f.MarkDeprecated("cred", "please use credential-set instead.")
_ = f.MarkDeprecated("cred", "please use credential-set instead.")
}
4 changes: 2 additions & 2 deletions cmd/porter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func main() {
defer func() {
// Capture panics and trace them
if panicErr := recover(); panicErr != nil {
log.Error(fmt.Errorf("%s", panicErr),
_ = log.Error(fmt.Errorf("%s", panicErr),
attribute.Bool("panic", true),
attribute.String("stackTrace", string(debug.Stack())))
log.EndSpan()
Expand All @@ -84,7 +84,7 @@ func main() {
if err := rootCmd.ExecuteContext(ctx); err != nil {
// Ideally we log all errors in the span that generated it,
// but as a failsafe, always log the error at the root span as well
log.Error(err)
_ = log.Error(err)
return cli.ExitCodeErr
}
return cli.ExitCodeSuccess
Expand Down
44 changes: 30 additions & 14 deletions cmd/porter/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,9 @@ func TestExperimentalFlags(t *testing.T) {

cmd := buildRootCommandFrom(p.Porter)
cmd.SetArgs([]string{"install"})
cmd.Execute()
err := cmd.Execute()
require.Error(t, err)

assert.False(t, p.Config.IsFeatureEnabled(experimental.FlagNoopFeature))
})

Expand All @@ -107,7 +109,8 @@ func TestExperimentalFlags(t *testing.T) {

cmd := buildRootCommandFrom(p.Porter)
cmd.SetArgs([]string{"install", "--experimental", experimental.NoopFeature})
cmd.Execute()
err := cmd.Execute()
require.Error(t, err)

assert.True(t, p.Config.IsFeatureEnabled(experimental.FlagNoopFeature))
})
Expand All @@ -121,7 +124,8 @@ func TestExperimentalFlags(t *testing.T) {

cmd := buildRootCommandFrom(p.Porter)
cmd.SetArgs([]string{"install"})
cmd.Execute()
err := cmd.Execute()
require.Error(t, err)

assert.True(t, p.Config.IsFeatureEnabled(experimental.FlagNoopFeature))
})
Expand All @@ -134,7 +138,8 @@ func TestExperimentalFlags(t *testing.T) {
require.NoError(t, p.FileSystem.WriteFile("/home/myuser/.porter/config.yaml", cfg, pkg.FileModeWritable))
cmd := buildRootCommandFrom(p.Porter)
cmd.SetArgs([]string{"install"})
cmd.Execute()
err := cmd.Execute()
require.Error(t, err)

assert.True(t, p.Config.IsFeatureEnabled(experimental.FlagNoopFeature))
})
Expand All @@ -147,7 +152,8 @@ func TestExperimentalFlags(t *testing.T) {
require.NoError(t, p.FileSystem.WriteFile("/home/myuser/.porter/config.yaml", cfg, pkg.FileModeWritable))
cmd := buildRootCommandFrom(p.Porter)
cmd.SetArgs([]string{"install", "--experimental", "no-op"})
cmd.Execute()
err := cmd.Execute()
require.Error(t, err)

assert.True(t, p.Config.IsFeatureEnabled(experimental.FlagNoopFeature))
})
Expand All @@ -161,7 +167,8 @@ func TestExperimentalFlags(t *testing.T) {

cmd := buildRootCommandFrom(p.Porter)
cmd.SetArgs([]string{"install", "--experimental", "no-op"})
cmd.Execute()
err := cmd.Execute()
require.Error(t, err)

assert.True(t, p.Config.IsFeatureEnabled(experimental.FlagNoopFeature))
})
Expand All @@ -177,7 +184,8 @@ func TestExperimentalFlags(t *testing.T) {
require.NoError(t, p.FileSystem.WriteFile("/home/myuser/.porter/config.yaml", cfg, pkg.FileModeWritable))
cmd := buildRootCommandFrom(p.Porter)
cmd.SetArgs([]string{"install"})
cmd.Execute()
err := cmd.Execute()
require.Error(t, err)

assert.False(t, p.Config.IsFeatureEnabled(experimental.FlagNoopFeature))
})
Expand All @@ -198,7 +206,9 @@ func TestVerbosity(t *testing.T) {

cmd := buildRootCommandFrom(p.Porter)
cmd.SetArgs([]string{"install"})
cmd.Execute()
err := cmd.Execute()
require.Error(t, err)

assert.Equal(t, config.LogLevelInfo, p.Config.GetVerbosity())
})

Expand All @@ -208,7 +218,8 @@ func TestVerbosity(t *testing.T) {

cmd := buildRootCommandFrom(p.Porter)
cmd.SetArgs([]string{"install", "--verbosity=debug"})
cmd.Execute()
err := cmd.Execute()
require.Error(t, err)

assert.Equal(t, config.LogLevelDebug, p.Config.GetVerbosity())
})
Expand All @@ -222,7 +233,8 @@ func TestVerbosity(t *testing.T) {

cmd := buildRootCommandFrom(p.Porter)
cmd.SetArgs([]string{"install"})
cmd.Execute()
err := cmd.Execute()
require.Error(t, err)

assert.Equal(t, config.LogLevelError, p.Config.GetVerbosity())
})
Expand All @@ -235,7 +247,8 @@ func TestVerbosity(t *testing.T) {
require.NoError(t, p.FileSystem.WriteFile("/home/myuser/.porter/config.yaml", cfg, pkg.FileModeWritable))
cmd := buildRootCommandFrom(p.Porter)
cmd.SetArgs([]string{"install"})
cmd.Execute()
err := cmd.Execute()
require.Error(t, err)

assert.Equal(t, config.LogLevelWarn, p.Config.GetVerbosity())
})
Expand All @@ -248,7 +261,8 @@ func TestVerbosity(t *testing.T) {
require.NoError(t, p.FileSystem.WriteFile("/home/myuser/.porter/config.yaml", cfg, pkg.FileModeWritable))
cmd := buildRootCommandFrom(p.Porter)
cmd.SetArgs([]string{"install", "--verbosity", "warn"})
cmd.Execute()
err := cmd.Execute()
require.Error(t, err)

assert.Equal(t, config.LogLevelWarn, p.Config.GetVerbosity())
})
Expand All @@ -262,7 +276,8 @@ func TestVerbosity(t *testing.T) {

cmd := buildRootCommandFrom(p.Porter)
cmd.SetArgs([]string{"install", "--verbosity=debug"})
cmd.Execute()
err := cmd.Execute()
require.Error(t, err)

assert.Equal(t, config.LogLevelDebug, p.Config.GetVerbosity())
})
Expand All @@ -278,7 +293,8 @@ func TestVerbosity(t *testing.T) {
require.NoError(t, p.FileSystem.WriteFile("/home/myuser/.porter/config.yaml", cfg, pkg.FileModeWritable))
cmd := buildRootCommandFrom(p.Porter)
cmd.SetArgs([]string{"install"})
cmd.Execute()
err := cmd.Execute()
require.Error(t, err)

assert.Equal(t, config.LogLevelWarn, p.Config.GetVerbosity())
})
Expand Down
4 changes: 2 additions & 2 deletions cmd/porter/mixins_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestBuildListMixinsCommand_AlternateFormat(t *testing.T) {
defer p.Close()

cmd := buildMixinsListCommand(p.Porter)
cmd.ParseFlags([]string{"-o", "json"})
require.NoError(t, cmd.ParseFlags([]string{"-o", "json"}))

err := cmd.PreRunE(cmd, []string{})

Expand All @@ -38,7 +38,7 @@ func TestBuildListMixinsCommand_BadFormat(t *testing.T) {
defer p.Close()

cmd := buildMixinsListCommand(p.Porter)
cmd.ParseFlags([]string{"-o", "flarts"})
require.NoError(t, cmd.ParseFlags([]string{"-o", "flarts"}))

err := cmd.PreRunE(cmd, []string{})

Expand Down
5 changes: 4 additions & 1 deletion cmd/porter/plugins.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,10 @@ func buildPluginRunCommand(p *porter.Porter) *cobra.Command {
Use: "run PLUGIN_KEY",
Short: "Serve internal plugins",
RunE: func(cmd *cobra.Command, args []string) error {
opts.ApplyArgs(args)
err := opts.ApplyArgs(args)
if err != nil {
return err
}
return p.RunInternalPlugins(cmd.Context(), opts)
},
Hidden: true, // This should ALWAYS be hidden, it is not a user-facing command
Expand Down
4 changes: 2 additions & 2 deletions cmd/porter/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func TestRun_Validate(t *testing.T) {

configTpl, err := p.Templates.GetManifest()
require.NoError(t, err)
p.TestConfig.TestContext.AddTestFileContents(configTpl, config.Name)
require.NoError(t, p.TestConfig.TestContext.AddTestFileContents(configTpl, config.Name))
cmd := buildRunCommand(p.Porter)

p.Setenv(config.EnvACTION, cnab.ActionInstall)
Expand All @@ -30,7 +30,7 @@ func TestRun_ValidateCustomAction(t *testing.T) {

configTpl, err := p.Templates.GetManifest()
require.NoError(t, err)
p.TestConfig.TestContext.AddTestFileContents(configTpl, config.Name)
require.NoError(t, p.TestConfig.TestContext.AddTestFileContents(configTpl, config.Name))
cmd := buildRunCommand(p.Porter)

p.Setenv(config.EnvACTION, "status")
Expand Down
2 changes: 1 addition & 1 deletion mage/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func Docs() {
if baseURL != "" {
cmd.Args("-b", baseURL)
}
cmd.RunV()
mgx.Must(cmd.RunV())
}

func removePreviewContainer() {
Expand Down
6 changes: 3 additions & 3 deletions magefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -702,10 +702,10 @@ func Vet() {
must.RunV("go", "vet", "./...")
}

// Run staticcheck on the project
// Run golangci-lint on the project
func Lint() {
mg.Deps(tools.EnsureStaticCheck)
must.RunV("staticcheck", "./...")
mg.Deps(tools.EnsureGolangCILint)
must.RunV("golangci-lint", "run", "--max-issues-per-linter", "0", "--max-same-issues", "0", "./...")
}

func getPorterHome() string {
Expand Down
3 changes: 1 addition & 2 deletions pkg/build/buildkit/buildx.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,7 @@ func (b *Builder) determineBuildArgs(
manifest *manifest.Manifest,
opts build.BuildImageOptions) (map[string]string, error) {

//lint:ignore SA4006 ignore unused context for now
ctx, span := tracing.StartSpan(ctx)
_, span := tracing.StartSpan(ctx)
defer span.EndSpan()

// This will grow later when we add custom build args from the porter.yaml
Expand Down
Loading

0 comments on commit 67f1dd8

Please sign in to comment.