From 673a2af11ca331705f9ef596d69d9c7b21990759 Mon Sep 17 00:00:00 2001 From: Robert Kaussow Date: Mon, 13 Feb 2023 15:26:20 +0100 Subject: [PATCH] refactor: add more linters and fix findings (#129) --- .drone.star | 18 +++--- .golangci.yml | 109 +++++++++++++++++++++++++++++------- Makefile | 2 +- cmd/drone-ansible/config.go | 2 +- cmd/drone-ansible/main.go | 13 +---- go.mod | 6 +- go.sum | 17 ++++-- plugin/ansible.go | 57 +++++++++++-------- plugin/impl.go | 4 +- plugin/plugin.go | 2 +- 10 files changed, 155 insertions(+), 75 deletions(-) diff --git a/.drone.star b/.drone.star index e91250b..0f6359f 100644 --- a/.drone.star +++ b/.drone.star @@ -31,7 +31,7 @@ def test(ctx): "steps": [ { "name": "deps", - "image": "golang:1.19", + "image": "golang:1.20", "commands": [ "make deps", ], @@ -44,7 +44,7 @@ def test(ctx): }, { "name": "generate", - "image": "golang:1.19", + "image": "golang:1.20", "commands": [ "make generate", ], @@ -57,7 +57,7 @@ def test(ctx): }, { "name": "lint", - "image": "golang:1.19", + "image": "golang:1.20", "commands": [ "make lint", ], @@ -70,7 +70,7 @@ def test(ctx): }, { "name": "test", - "image": "golang:1.19", + "image": "golang:1.20", "commands": [ "make test", ], @@ -109,7 +109,7 @@ def build(ctx): "steps": [ { "name": "generate", - "image": "golang:1.19", + "image": "golang:1.20", "pull": "always", "commands": [ "make generate", @@ -123,7 +123,7 @@ def build(ctx): }, { "name": "build", - "image": "techknowlogick/xgo:go-1.19.x", + "image": "techknowlogick/xgo:go-1.20.x", "pull": "always", "commands": [ "ln -s /drone/src /source", @@ -138,7 +138,7 @@ def build(ctx): }, { "name": "executable", - "image": "golang:1.19", + "image": "golang:1.20", "pull": "always", "commands": [ "$(find dist/ -executable -type f -iname drone-ansible-linux-amd64) --help", @@ -205,7 +205,7 @@ def docker(ctx, arch): "steps": [ { "name": "generate", - "image": "golang:1.19", + "image": "golang:1.20", "pull": "always", "commands": [ "make generate", @@ -219,7 +219,7 @@ def docker(ctx, arch): }, { "name": "build", - "image": "golang:1.19", + "image": "golang:1.20", "pull": "always", "commands": [ "make build", diff --git a/.golangci.yml b/.golangci.yml index 6bfef42..fb48c04 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -1,25 +1,94 @@ +--- linters: - enable: - - gosimple - - deadcode - - typecheck - - govet - - errcheck - - staticcheck - - unused - - structcheck - - varcheck - # - dupl - - gofmt - - misspell - - gocritic - - bidichk - - ineffassign - - revive - - gofumpt - - depguard enable-all: false disable-all: true + enable: + - errcheck + - gosimple + - govet + - ineffassign + - staticcheck + - typecheck + - unused + - asasalint + - asciicheck + - bidichk + - bodyclose + - containedctx + - contextcheck + - decorder + - depguard + - dogsled + - dupl + - dupword + - durationcheck + - errchkjson + - errname + - errorlint + - execinquery + - exhaustive + - exportloopref + - forcetypeassert + - ginkgolinter + - gocheckcompilerdirectives + - gochecknoglobals + - gochecknoinits + - gocognit + - goconst + - gocritic + - gocyclo + - godot + - godox + - goerr113 + - gofmt + - gofumpt + - goheader + - goimports + - gomnd + - gomoddirectives + - gomodguard + - goprintffuncname + - gosec + - grouper + - importas + - interfacebloat + - ireturn + - lll + - loggercheck + - maintidx + - makezero + - misspell + - musttag + - nakedret + - nestif + - nilerr + - nilnil + - nlreturn + - noctx + - nolintlint + - nonamedreturns + - nosprintfhostport + - prealloc + - predeclared + - promlinter + - reassign + - revive + - stylecheck + - tagliatelle + - tenv + - testableexamples + - thelper + - tparallel + - unconvert + - unparam + - usestdlibvars + - whitespace + - wsl + ## not working in golangci yet + # - rowserrcheck + # - sqlclosecheck + # - structcheck + # - wastedassign fast: false run: @@ -28,4 +97,4 @@ run: linters-settings: gofumpt: extra-rules: true - lang-version: "1.19" + lang-version: "1.20" diff --git a/Makefile b/Makefile index 97fd185..a25d87d 100644 --- a/Makefile +++ b/Makefile @@ -20,7 +20,7 @@ XGO_PACKAGE ?= src.techknowlogick.com/xgo@latest GENERATE ?= $(IMPORT)/pkg/templates XGO_PACKAGE ?= src.techknowlogick.com/xgo@latest -XGO_VERSION := go-1.19.x +XGO_VERSION := go-1.20.x XGO_TARGETS ?= linux/amd64,linux/arm64,darwin/amd64,darwin/arm64,windows/amd64 TAGS ?= netgo diff --git a/cmd/drone-ansible/config.go b/cmd/drone-ansible/config.go index 3bd7a0d..511edb8 100644 --- a/cmd/drone-ansible/config.go +++ b/cmd/drone-ansible/config.go @@ -120,7 +120,7 @@ func settingsFlags(settings *plugin.Settings) []cli.Flag { Name: "forks", Usage: "specify number of parallel processes to use", EnvVars: []string{"PLUGIN_FORKS"}, - Value: 5, + Value: plugin.AnsibleForksDefault, Destination: &settings.Forks, }, &cli.StringFlag{ diff --git a/cmd/drone-ansible/main.go b/cmd/drone-ansible/main.go index ca22466..3155479 100644 --- a/cmd/drone-ansible/main.go +++ b/cmd/drone-ansible/main.go @@ -12,6 +12,7 @@ import ( "github.com/drone-plugins/drone-plugin-lib/urfave" ) +//nolint:gochecknoglobals var ( BuildVersion = "devel" BuildDate = "00000000" @@ -52,19 +53,11 @@ func run(settings *plugin.Settings) cli.ActionFunc { ) if err := plugin.Validate(); err != nil { - if e, ok := err.(errors.ExitCoder); ok { - return e - } - - return errors.ExitMessagef("validation failed: %w", err) + return fmt.Errorf("validation failed: %w", err) } if err := plugin.Execute(); err != nil { - if e, ok := err.(errors.ExitCoder); ok { - return e - } - - return errors.ExitMessagef("execution failed: %w", err) + return fmt.Errorf("execution failed: %w", err) } return nil diff --git a/go.mod b/go.mod index e4e5217..abb030a 100644 --- a/go.mod +++ b/go.mod @@ -1,18 +1,18 @@ module github.com/owncloud-ci/drone-ansible -go 1.19 +go 1.20 require ( github.com/drone-plugins/drone-plugin-lib v0.4.0 github.com/joho/godotenv v1.5.1 github.com/pkg/errors v0.9.1 github.com/urfave/cli/v2 v2.24.3 + golang.org/x/sys v0.5.0 ) require ( github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect github.com/russross/blackfriday/v2 v2.1.0 // indirect - github.com/sirupsen/logrus v1.8.1 // indirect + github.com/sirupsen/logrus v1.9.0 // indirect github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect - golang.org/x/sys v0.0.0-20220330033206-e17cdc41300f // indirect ) diff --git a/go.sum b/go.sum index 296e4f7..46d8aaf 100644 --- a/go.sum +++ b/go.sum @@ -2,6 +2,7 @@ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03 github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU= github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w= github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/drone-plugins/drone-plugin-lib v0.4.0 h1:qywEYGhquUuid6zNLmKia8CWY1TUa8jPQQ/G9ozfAmc= @@ -20,11 +21,13 @@ github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88= -github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE= -github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= +github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0= +github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= -github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/urfave/cli/v2 v2.2.0/go.mod h1:SE9GqnLQmjVa0iPEY0f1w3ygNIYcIJ0OKPMoW2caLfQ= github.com/urfave/cli/v2 v2.24.3 h1:7Q1w8VN8yE0MJEHP06bv89PjYsN4IHWED2s1v/Zlfm0= github.com/urfave/cli/v2 v2.24.3/go.mod h1:GHupkWPMM0M/sj1a2b4wUrWBPzazNrIjouW6fmdJLxc= @@ -35,11 +38,13 @@ golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20220330033206-e17cdc41300f h1:rlezHXNlxYWvBCzNses9Dlc7nGFaNMJeqLolcmQSSZY= -golang.org/x/sys v0.0.0-20220330033206-e17cdc41300f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU= +golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/tools v0.0.0-20190624222133-a101b041ded4/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gotest.tools/v3 v3.0.2/go.mod h1:3SzNCllyD9/Y+b5r9JIKQ474KzkZyqLqEfYqMsX94Bk= diff --git a/plugin/ansible.go b/plugin/ansible.go index fffbcd2..9aacb43 100644 --- a/plugin/ansible.go +++ b/plugin/ansible.go @@ -3,21 +3,30 @@ package plugin import ( "fmt" "os" - "os/exec" "path/filepath" "strconv" "strings" "github.com/pkg/errors" "github.com/urfave/cli/v2" + "golang.org/x/sys/execabs" ) -var ( +const ( + AnsibleForksDefault = 5 + ansibleFolder = "/etc/ansible" ansibleConfig = "/etc/ansible/ansible.cfg" + + pipBin = "/usr/bin/pip" + ansibleBin = "/usr/bin/ansible" + ansibleGalaxyBin = "/usr/bin/ansible-galaxy" + ansiblePlaybookBin = "/usr/bin/ansible-playbook" + + strictFilePerm = 0o600 ) -var ansibleContent = ` +const ansibleContent = ` [defaults] host_key_checking = False ` @@ -27,7 +36,7 @@ func (p *Plugin) ansibleConfig() error { return errors.Wrap(err, "failed to create ansible directory") } - if err := os.WriteFile(ansibleConfig, []byte(ansibleContent), 0o600); err != nil { + if err := os.WriteFile(ansibleConfig, []byte(ansibleContent), strictFilePerm); err != nil { return errors.Wrap(err, "failed to create ansible config") } @@ -49,6 +58,7 @@ func (p *Plugin) privateKey() error { } p.settings.PrivateKeyFile = tmpfile.Name() + return nil } @@ -67,6 +77,7 @@ func (p *Plugin) vaultPass() error { } p.settings.VaultPasswordFile = tmpfile.Name() + return nil } @@ -77,6 +88,7 @@ func (p *Plugin) playbooks() error { files, err := filepath.Glob(p) if err != nil { playbooks = append(playbooks, p) + continue } @@ -88,21 +100,22 @@ func (p *Plugin) playbooks() error { } p.settings.Playbooks = *cli.NewStringSlice(playbooks...) + return nil } -func (p *Plugin) versionCommand() *exec.Cmd { +func (p *Plugin) versionCommand() *execabs.Cmd { args := []string{ "--version", } - return exec.Command( - "ansible", + return execabs.Command( + ansibleBin, args..., ) } -func (p *Plugin) requirementsCommand() *exec.Cmd { +func (p *Plugin) requirementsCommand() *execabs.Cmd { args := []string{ "install", "--upgrade", @@ -110,13 +123,13 @@ func (p *Plugin) requirementsCommand() *exec.Cmd { p.settings.Requirements, } - return exec.Command( - "pip", + return execabs.Command( + pipBin, args..., ) } -func (p *Plugin) galaxyCommand() *exec.Cmd { +func (p *Plugin) galaxyCommand() *execabs.Cmd { args := []string{ "install", "--force", @@ -128,13 +141,13 @@ func (p *Plugin) galaxyCommand() *exec.Cmd { args = append(args, fmt.Sprintf("-%s", strings.Repeat("v", p.settings.Verbose))) } - return exec.Command( - "ansible-galaxy", + return execabs.Command( + ansibleGalaxyBin, args..., ) } -func (p *Plugin) ansibleCommand(inventory string) *exec.Cmd { +func (p *Plugin) ansibleCommand(inventory string) *execabs.Cmd { args := []string{ "--inventory", inventory, @@ -160,8 +173,8 @@ func (p *Plugin) ansibleCommand(inventory string) *exec.Cmd { args = append(args, "--list-hosts") args = append(args, p.settings.Playbooks.Value()...) - return exec.Command( - "ansible-playbook", + return execabs.Command( + ansiblePlaybookBin, args..., ) } @@ -170,8 +183,8 @@ func (p *Plugin) ansibleCommand(inventory string) *exec.Cmd { args = append(args, "--syntax-check") args = append(args, p.settings.Playbooks.Value()...) - return exec.Command( - "ansible-playbook", + return execabs.Command( + ansiblePlaybookBin, args..., ) } @@ -192,7 +205,7 @@ func (p *Plugin) ansibleCommand(inventory string) *exec.Cmd { args = append(args, "--force-handlers") } - if p.settings.Forks != 5 { + if p.settings.Forks != AnsibleForksDefault { args = append(args, "--forks", strconv.Itoa(p.settings.Forks)) } @@ -270,12 +283,12 @@ func (p *Plugin) ansibleCommand(inventory string) *exec.Cmd { args = append(args, p.settings.Playbooks.Value()...) - return exec.Command( - "ansible-playbook", + return execabs.Command( + ansiblePlaybookBin, args..., ) } -func trace(cmd *exec.Cmd) { +func trace(cmd *execabs.Cmd) { fmt.Println("$", strings.Join(cmd.Args, " ")) } diff --git a/plugin/impl.go b/plugin/impl.go index 82378de..7dddf97 100644 --- a/plugin/impl.go +++ b/plugin/impl.go @@ -2,10 +2,10 @@ package plugin import ( "os" - "os/exec" "github.com/pkg/errors" "github.com/urfave/cli/v2" + "golang.org/x/sys/execabs" ) // Settings for the Plugin. @@ -86,7 +86,7 @@ func (p *Plugin) Execute() error { defer os.Remove(p.settings.VaultPasswordFile) } - commands := []*exec.Cmd{ + commands := []*execabs.Cmd{ p.versionCommand(), } diff --git a/plugin/plugin.go b/plugin/plugin.go index 527b881..eb447d9 100644 --- a/plugin/plugin.go +++ b/plugin/plugin.go @@ -12,7 +12,7 @@ type Plugin struct { } // New initializes a plugin from the given Settings, Pipeline, and Network. -func New(settings Settings, pipeline drone.Pipeline, network drone.Network) drone.Plugin { +func New(settings Settings, pipeline drone.Pipeline, network drone.Network) *Plugin { return &Plugin{ settings: settings, pipeline: pipeline,