Skip to content

Commit

Permalink
feat: add more comprehensive linting
Browse files Browse the repository at this point in the history
  • Loading branch information
Ric Featherstone authored and 06kellyjac committed Dec 21, 2023
1 parent d0196a7 commit f71fef3
Show file tree
Hide file tree
Showing 15 changed files with 204 additions and 77 deletions.
118 changes: 118 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
linters-settings:
exhaustive:
check:
- switch
- map
default-signifies-exhaustive: true
lll:
line-length: 120
gci:
sections:
- standard # Captures all standard packages if they do not match another section.
- default # Contains all imports that could not be matched to another section type.
- prefix(github.com/controlplaneio/simulator/) # Groups all imports with the specified Prefix.

issues:
exclude-rules:
# disable funlen for test funcs
- source: "^func Test"
linters:
- funlen

linters:
# please, do not use `enable-all`: it's deprecated and will be removed soon.
# inverted configuration with `enable-all` and `disable` is not scalable during updates of golangci-lint
disable-all: true
enable:
- asasalint
- asciicheck
- bidichk
- bodyclose
- containedctx
- contextcheck
- decorder
# - depguard # just for limiting packages
- dogsled
- dupl
- durationcheck
- errcheck
- errchkjson
- errname
- errorlint
- execinquery
- exhaustive
# - exhaustruct
- exportloopref
- forbidigo
- forcetypeassert
# - funlen # complexity
# disable gci while it doesn't play nice with gofumpt and goimports
# https://github.com/golangci/golangci-lint/issues/1490
# - gci
# - ginkgolinter # not using ginkgo
- gocheckcompilerdirectives
# - gochecknoglobals
# - gochecknoinits
- gocognit # complexity
- goconst
- gocritic
# - gocyclo # using gocognit
# - godot # comment style
- gofmt
# - gofumpt
- goheader
- goimports
- gomnd # detect loose numbers
- gomoddirectives # not sure
- gomodguard # same as depguard?
- goprintffuncname
- gosec
- gosmopolitan
- gosimple
- govet
- grouper
- importas # configure this
- interfacebloat # maybe configure
- ineffassign
# - ireturn
- lll
- loggercheck
# - maintidx # using gocognit
- makezero
- mirror
- misspell
- musttag
- nakedret
# - nestif # using gocognit
- nilerr
- nilnil
# - nlreturn # probably not
- noctx
- nolintlint
- nonamedreturns # maybe
- nosprintfhostport
- paralleltest
- prealloc
- predeclared
- promlinter
- reassign
- revive
- rowserrcheck
- sqlclosecheck
- staticcheck
- stylecheck
- tagalign
- tagliatelle
- tenv
- testpackage
- thelper
- tparallel # same with parallel test
- unconvert
- unparam
- usestdlibvars
- unused
# - varnamelen
- wastedassign
- whitespace
- wrapcheck
# - wsl
7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
SIMULATOR_IMAGE ?= controlplane/simulator

simulator-dev-image:
lint:
golangci-lint run -c .golangci.yml

simulator-dev-image: lint
docker build -t $(SIMULATOR_IMAGE):dev -f dev.Dockerfile .

simulator-image: simulator-dev-image
docker build -t $(SIMULATOR_IMAGE) .

simulator-cli:
simulator-cli: lint
go build -v -o bin/simulator internal/cmd/main.go
11 changes: 4 additions & 7 deletions controlplane/aws/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func CreateBucket(ctx context.Context, name string) error {
cfg, err := config.LoadDefaultConfig(ctx)
if err != nil {
slog.Error("failed to create bucket", "name", name, "error", err)
return err
return errors.Join(errors.New("failed to create bucket"), err)
}

var bucketAlreadyOwnedByYou *types.BucketAlreadyOwnedByYou
Expand All @@ -35,12 +35,9 @@ func CreateBucket(ctx context.Context, name string) error {
LocationConstraint: types.BucketLocationConstraintEuWest2,
},
})
if err != nil {
if !errors.As(err, &bucketAlreadyOwnedByYou) {
} else {
slog.Error("failed to create bucket", "name", name, "error", err)
return err
}
if err != nil && !errors.As(err, &bucketAlreadyOwnedByYou) {
slog.Error("failed to create bucket", "name", name, "error", err)
return errors.Join(errors.New("failed to create bucket"), err)
}

return nil
Expand Down
5 changes: 3 additions & 2 deletions controlplane/cli/bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ var bucketCmd = &cobra.Command{

var createBucketCmd = &cobra.Command{
Use: "create",
RunE: func(cmd *cobra.Command, args []string) error {
Run: func(cmd *cobra.Command, args []string) {
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
defer stop()

cp := controlplane.New()
return cp.CreateBucket(ctx, bucket)
err := cp.CreateBucket(ctx, bucket)
cobra.CheckErr(err)
},
}

Expand Down
5 changes: 3 additions & 2 deletions controlplane/cli/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ var imageCmd = &cobra.Command{

var buildCmd = &cobra.Command{
Use: "build",
RunE: func(cmd *cobra.Command, args []string) error {
Run: func(cmd *cobra.Command, args []string) {
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
defer stop()

cp := controlplane.New()
return cp.BuildImage(ctx, template)
err := cp.BuildImage(ctx, template)
cobra.CheckErr(err)
},
}

Expand Down
10 changes: 6 additions & 4 deletions controlplane/cli/infra.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,25 @@ var infraCmd = &cobra.Command{

var createCmd = &cobra.Command{
Use: "create",
RunE: func(cmd *cobra.Command, args []string) error {
Run: func(cmd *cobra.Command, args []string) {
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
defer stop()

cp := controlplane.New()
return cp.CreateInfrastructure(ctx, bucket, key, name)
err := cp.CreateInfrastructure(ctx, bucket, key, name)
cobra.CheckErr(err)
},
}

var destroyCmd = &cobra.Command{
Use: "destroy",
RunE: func(cmd *cobra.Command, args []string) error {
Run: func(cmd *cobra.Command, args []string) {
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
defer stop()

cp := controlplane.New()
return cp.DestroyInfrastructure(ctx, bucket, key, name)
err := cp.DestroyInfrastructure(ctx, bucket, key, name)
cobra.CheckErr(err)
},
}

Expand Down
11 changes: 6 additions & 5 deletions controlplane/cli/scenario.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,25 @@ var scenarioCmd = &cobra.Command{

var installCmd = &cobra.Command{
Use: "install",
RunE: func(cmd *cobra.Command, args []string) error {
Run: func(cmd *cobra.Command, args []string) {
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
defer stop()

cp := controlplane.New()
return cp.InstallScenario(ctx, name)
err := cp.InstallScenario(ctx, name)
cobra.CheckErr(err)
},
}

var uninstallCmd = &cobra.Command{
Use: "uninstall",
RunE: func(cmd *cobra.Command, args []string) error {
Run: func(cmd *cobra.Command, args []string) {
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
defer stop()

cp := controlplane.New()
return cp.UninstallScenario(ctx, name)
err := cp.UninstallScenario(ctx, name)
cobra.CheckErr(err)
},
}

Expand All @@ -42,5 +44,4 @@ func init() {
scenarioCmd.AddCommand(installCmd)
scenarioCmd.AddCommand(uninstallCmd)
simulatorCmd.AddCommand(scenarioCmd)

}
2 changes: 1 addition & 1 deletion controlplane/commands/ansible.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func AnsiblePlaybookCommand(workingDir, playbookDir, playbook string, extraVars
if len(extraVars) > 0 {
args = append(args,
"--extra-vars",
fmt.Sprintf("%s", strings.Join(extraVars, " ")),
strings.Join(extraVars, " "),
)
}

Expand Down
9 changes: 6 additions & 3 deletions controlplane/commands/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package commands

import (
"context"
"errors"
"fmt"
"io"
"log/slog"
Expand Down Expand Up @@ -34,6 +35,7 @@ func (c command) Run(ctx context.Context, output ...io.Writer) error {
writer = output[0]
}

//nolint:gosec
cmd := exec.CommandContext(ctx, string(c.Executable), c.Arguments...)
cmd.Dir = c.WorkingDir
cmd.Env = c.Environment
Expand All @@ -43,18 +45,19 @@ func (c command) Run(ctx context.Context, output ...io.Writer) error {
// TODO: Ensure ctrl-c stops the command
err := cmd.Run()
if err != nil {
slog.Error("failed to run", "command", c)
return errors.Join(errors.New("failed to run command"), err)
}

return err
return nil
}

func (c command) LogValue() slog.Value {
cmd := fmt.Sprintf("%s %s", c.Executable, strings.Join(c.Arguments, " "))
var env []string
env := make([]string, 0)

// Only log env keys, not values
for _, value := range c.Environment {
//nolint:gocritic
env = append(env, value[:strings.Index(value, "=")])
}

Expand Down
23 changes: 16 additions & 7 deletions controlplane/simulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,47 +49,56 @@ type Simulator interface {
UninstallScenario(ctx context.Context, name string) error
}

type simulator struct {
}
type simulator struct{}

func (s simulator) CreateBucket(ctx context.Context, name string) error {
//nolint:wrapcheck
return aws.CreateBucket(ctx, name)
}

func (s simulator) BuildImage(ctx context.Context, name string) error {
err := commands.PackerInitCommand(PackerTemplateDir, name).Run(ctx)
if err != nil {
return err
return err //nolint:wrapcheck
}

//nolint:wrapcheck
return commands.PackerBuildCommand(PackerTemplateDir, name).Run(ctx)
}

func (s simulator) CreateInfrastructure(ctx context.Context, bucket, key, name string) error {
err := commands.TerraformInitCommand(TerraformWorkspaceDir, backendConfig(bucket, key)).Run(ctx)
if err != nil {
//nolint:wrapcheck
return err
}

return commands.TerraformCommand(TerraformWorkspaceDir, commands.TerraformApply, terraformVars(name, bucket)).Run(ctx)

//nolint:wrapcheck
return commands.TerraformCommand(TerraformWorkspaceDir, commands.TerraformApply, terraformVars(name, bucket)).
Run(ctx)
}

func (s simulator) DestroyInfrastructure(ctx context.Context, bucket, key, name string) error {
err := commands.TerraformInitCommand(TerraformWorkspaceDir, backendConfig(bucket, key)).Run(ctx)
if err != nil {
//nolint:wrapcheck
return err
}

return commands.TerraformCommand(TerraformWorkspaceDir, commands.TerraformDestroy, terraformVars(name, bucket)).Run(ctx)
//nolint:wrapcheck
return commands.TerraformCommand(TerraformWorkspaceDir, commands.TerraformDestroy, terraformVars(name, bucket)).
Run(ctx)
}

func (s simulator) InstallScenario(ctx context.Context, name string) error {
//nolint:wrapcheck
return commands.AnsiblePlaybookCommand(AdminSSHBundleDir, AnsiblePlaybookDir, name).Run(ctx)
}

func (s simulator) UninstallScenario(ctx context.Context, name string) error {
return commands.AnsiblePlaybookCommand(AdminSSHBundleDir, AnsiblePlaybookDir, name, "state=absent").Run(ctx)
//nolint:wrapcheck
return commands.AnsiblePlaybookCommand(AdminSSHBundleDir, AnsiblePlaybookDir, name, "state=absent").
Run(ctx)
}

func backendConfig(bucket, key string) []string {
Expand Down
2 changes: 1 addition & 1 deletion dev.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ WORKDIR /app

COPY . .

RUN /usr/bin/golangci-lint run -v
RUN /usr/bin/golangci-lint run -v -c .golangci.yml

FROM ${GOLANG_IMAGE} as BUILDER

Expand Down
3 changes: 2 additions & 1 deletion internal/cli/scenario.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ func tabulateScenarios(scenarios []scenarios.Scenario) {
s.Name,
s.Description,
s.Category,
s.Difficulty})
s.Difficulty,
})
table.SetRowLine(true)
}
table.Render()
Expand Down
Loading

0 comments on commit f71fef3

Please sign in to comment.