From 8d660362524388f908d6bd6a604bc5cf8d7d119a Mon Sep 17 00:00:00 2001 From: Erin Call Date: Mon, 9 Dec 2019 10:52:41 -0800 Subject: [PATCH] Brush all the lint off this code I wrote in a haze --- README.md | 1 + go.mod | 2 ++ go.sum | 8 ++++++++ internal/helm/config.go | 17 +++++++++++------ internal/helm/config_test.go | 6 +++--- internal/helm/plan.go | 4 ++++ internal/run/cmd.go | 4 ++-- internal/run/help.go | 5 ++++- internal/run/help_test.go | 8 ++++---- internal/run/upgrade.go | 5 ++++- internal/run/upgrade_test.go | 8 ++++---- 11 files changed, 47 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 83524de..c6c6e9b 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,7 @@ TODO: * [x] Make a `.drone.yml` that's sufficient for building an image * [x] Make a `Dockerfile` that's sufficient for launching the built image * [x] Make `cmd/drone-helm/main.go` actually invoke `helm` +* [x] Make `golint` part of the build process (and make it pass) * [ ] Flesh out `helm upgrade` until it's capable of working * [ ] Implement `helm lint` * [ ] Implement `helm delete` diff --git a/go.mod b/go.mod index 709639b..c060a9c 100644 --- a/go.mod +++ b/go.mod @@ -6,4 +6,6 @@ require ( github.com/golang/mock v1.3.1 github.com/kelseyhightower/envconfig v1.4.0 github.com/stretchr/testify v1.4.0 + golang.org/x/lint v0.0.0-20191125180803-fdd1cda4f05f // indirect + golang.org/x/tools v0.0.0-20191206204035-259af5ff87bd // indirect ) diff --git a/go.sum b/go.sum index 901a1c3..f8cbd31 100644 --- a/go.sum +++ b/go.sum @@ -10,12 +10,20 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+ github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/lint v0.0.0-20191125180803-fdd1cda4f05f h1:J5lckAjkw6qYlOZNj90mLYNTEKDvWeuc1yieZ8qUzUE= +golang.org/x/lint v0.0.0-20191125180803-fdd1cda4f05f/go.mod h1:5qLYkcX4OjUUV8bRuDixDT3tpyyb+LUpUlRWLxfhWrs= golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= 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/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/tools v0.0.0-20190425150028-36563e24a262 h1:qsl9y/CJx34tuA7QCPNp86JNJe4spst6Ff8MjvPUdPg= golang.org/x/tools v0.0.0-20190425150028-36563e24a262/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= +golang.org/x/tools v0.0.0-20191125144606-a911d9008d1f h1:kDxGY2VmgABOe55qheT/TFqUMtcTHnomIPS1iv3G4Ms= +golang.org/x/tools v0.0.0-20191125144606-a911d9008d1f/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.0.0-20191206204035-259af5ff87bd h1:Zc7EU2PqpsNeIfOoVA7hvQX4cS3YDJEs5KlfatT3hLo= +golang.org/x/tools v0.0.0-20191206204035-259af5ff87bd/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= diff --git a/internal/helm/config.go b/internal/helm/config.go index cffa2f8..e2513e8 100644 --- a/internal/helm/config.go +++ b/internal/helm/config.go @@ -5,9 +5,14 @@ import ( "strings" ) +// The Config struct captures the `settings` and `environment` blocks inthe application's drone +// config. Configuration in drone's `settings` block arrives as uppercase env vars matching the +// config key, prefixed with `PLUGIN_`. Config from the `environment` block is *not* prefixed; any +// keys that are likely to be in that block (i.e. things that use `from_secret` need an explicit +// `envconfig:` tag so that envconfig will look for a non-prefixed env var. type Config struct { // Configuration for drone-helm itself - Command HelmCommand `envconfig:"HELM_COMMAND"` // Helm command to run + Command helmCommand `envconfig:"HELM_COMMAND"` // Helm command to run DroneEvent string `envconfig:"DRONE_BUILD_EVENT"` // Drone event that invoked this plugin. UpdateDependencies bool `split_words:"true"` // call `helm dependency update` before the main command Repos []string `envconfig:"HELM_REPOS"` // call `helm repo add` before the main command @@ -37,14 +42,14 @@ type Config struct { Force bool `` // } -type HelmCommand string +type helmCommand string -// HelmCommand.Decode checks the given value against the list of known commands and generates a helpful error if the command is unknown. -func (cmd *HelmCommand) Decode(value string) error { +// helmCommand.Decode checks the given value against the list of known commands and generates a helpful error if the command is unknown. +func (cmd *helmCommand) Decode(value string) error { known := []string{"upgrade", "delete", "lint", "help"} for _, c := range known { if value == c { - *cmd = HelmCommand(value) + *cmd = helmCommand(value) return nil } } @@ -53,6 +58,6 @@ func (cmd *HelmCommand) Decode(value string) error { return nil } known[len(known)-1] = fmt.Sprintf("or %s", known[len(known)-1]) - return fmt.Errorf("Unknown command '%s'. If specified, command must be %s.", + return fmt.Errorf("unknown command '%s'. If specified, command must be %s", value, strings.Join(known, ", ")) } diff --git a/internal/helm/config_test.go b/internal/helm/config_test.go index d4ff7dd..1df2d4f 100644 --- a/internal/helm/config_test.go +++ b/internal/helm/config_test.go @@ -14,7 +14,7 @@ func TestConfigTestSuite(t *testing.T) { } func (suite *ConfigTestSuite) TestHelmCommandDecodeSuccess() { - cmd := HelmCommand("") + cmd := helmCommand("") err := cmd.Decode("upgrade") suite.Require().Nil(err) @@ -22,7 +22,7 @@ func (suite *ConfigTestSuite) TestHelmCommandDecodeSuccess() { } func (suite *ConfigTestSuite) TestHelmCommandDecodeFailure() { - cmd := HelmCommand("") + cmd := helmCommand("") err := cmd.Decode("execute order 66") - suite.EqualError(err, "Unknown command 'execute order 66'. If specified, command must be upgrade, delete, lint, or help.") + suite.EqualError(err, "unknown command 'execute order 66'. If specified, command must be upgrade, delete, lint, or help") } diff --git a/internal/helm/plan.go b/internal/helm/plan.go index 91bffbb..52b280d 100644 --- a/internal/helm/plan.go +++ b/internal/helm/plan.go @@ -5,14 +5,17 @@ import ( "github.com/pelotech/drone-helm3/internal/run" ) +// A Step is one step in the plan. type Step interface { Run() error } +// A Plan is a series of steps to perform. type Plan struct { steps []Step } +// NewPlan makes a plan for running a helm operation. func NewPlan(cfg Config) (*Plan, error) { p := Plan{} switch cfg.Command { @@ -44,6 +47,7 @@ func NewPlan(cfg Config) (*Plan, error) { return &p, nil } +// Execute runs each step in the plan, aborting and reporting on error func (p *Plan) Execute() error { for _, step := range p.steps { if err := step.Run(); err != nil { diff --git a/internal/run/cmd.go b/internal/run/cmd.go index c11789f..a2bcc9a 100644 --- a/internal/run/cmd.go +++ b/internal/run/cmd.go @@ -7,7 +7,7 @@ import ( "syscall" ) -const HELM_BIN = "/usr/bin/helm" +const helmBin = "/usr/bin/helm" // The cmd interface provides a generic form of exec.Cmd so that it can be mocked out in tests. type cmd interface { @@ -43,7 +43,7 @@ type execCmd struct { *exec.Cmd } -var Command = func(path string, args ...string) cmd { +var command = func(path string, args ...string) cmd { return &execCmd{ Cmd: exec.Command(path, args...), } diff --git a/internal/run/help.go b/internal/run/help.go index 49b57e4..842dcc0 100644 --- a/internal/run/help.go +++ b/internal/run/help.go @@ -4,18 +4,21 @@ import ( "os" ) +// Help is a step in a helm Plan that calls `helm help`. type Help struct { cmd cmd } +// Run launches the command. func (h *Help) Run() error { return h.cmd.Run() } +// NewHelp returns a new Help. func NewHelp() *Help { h := Help{} - h.cmd = Command(HELM_BIN, "help") + h.cmd = command(helmBin, "help") h.cmd.Stdout(os.Stdout) h.cmd.Stderr(os.Stderr) diff --git a/internal/run/help_test.go b/internal/run/help_test.go index 8e99b93..815404f 100644 --- a/internal/run/help_test.go +++ b/internal/run/help_test.go @@ -11,14 +11,14 @@ func TestHelp(t *testing.T) { defer ctrl.Finish() mCmd := NewMockcmd(ctrl) - originalCommand := Command + originalCommand := command - Command = func(path string, args ...string) cmd { - assert.Equal(t, HELM_BIN, path) + command = func(path string, args ...string) cmd { + assert.Equal(t, helmBin, path) assert.Equal(t, []string{"help"}, args) return mCmd } - defer func() { Command = originalCommand }() + defer func() { command = originalCommand }() mCmd.EXPECT(). Stdout(gomock.Any()) diff --git a/internal/run/upgrade.go b/internal/run/upgrade.go index 2b4c9fb..636c4da 100644 --- a/internal/run/upgrade.go +++ b/internal/run/upgrade.go @@ -4,21 +4,24 @@ import ( "os" ) +// Upgrade is a step in a helm Plan that calls `helm upgrade`. type Upgrade struct { Chart string Release string cmd cmd } +// Run launches the command. func (u *Upgrade) Run() error { return u.cmd.Run() } +// NewUpgrade creates a new Upgrade. func NewUpgrade(release, chart string) *Upgrade { u := Upgrade{ Chart: chart, Release: release, - cmd: Command(HELM_BIN, "upgrade", "--install", release, chart), + cmd: command(helmBin, "upgrade", "--install", release, chart), } u.cmd.Stdout(os.Stdout) diff --git a/internal/run/upgrade_test.go b/internal/run/upgrade_test.go index f8ad66f..41384f5 100644 --- a/internal/run/upgrade_test.go +++ b/internal/run/upgrade_test.go @@ -11,15 +11,15 @@ func TestNewUpgrade(t *testing.T) { defer ctrl.Finish() mCmd := NewMockcmd(ctrl) - originalCommand := Command + originalCommand := command - Command = func(path string, args ...string) cmd { - assert.Equal(t, HELM_BIN, path) + command = func(path string, args ...string) cmd { + assert.Equal(t, helmBin, path) assert.Equal(t, []string{"upgrade", "--install", "jonas_brothers_only_human", "at40"}, args) return mCmd } - defer func() { Command = originalCommand }() + defer func() { command = originalCommand }() mCmd.EXPECT(). Stdout(gomock.Any())