From 34b9ec1c4c9ab2bb016800fa19a96cfd60d0168a Mon Sep 17 00:00:00 2001 From: Erin Call Date: Thu, 26 Dec 2019 10:47:42 -0800 Subject: [PATCH 1/4] Run the Help step by default [#15] --- internal/helm/plan.go | 2 +- internal/helm/plan_test.go | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/internal/helm/plan.go b/internal/helm/plan.go index 1d4ced9..d5c8835 100644 --- a/internal/helm/plan.go +++ b/internal/helm/plan.go @@ -72,7 +72,7 @@ func determineSteps(cfg Config) *func(Config) []Step { case "delete": return &uninstall default: - panic("not implemented") + return &help } } } diff --git a/internal/helm/plan_test.go b/internal/helm/plan_test.go index e5a1a96..eebf51b 100644 --- a/internal/helm/plan_test.go +++ b/internal/helm/plan_test.go @@ -255,3 +255,10 @@ func (suite *PlanTestSuite) TestDeterminePlanHelpCommand() { stepsMaker := determineSteps(cfg) suite.Same(&help, stepsMaker) } + +func (suite *PlanTestSuite) TestDeterminePlanHelpOnUnknown() { + cfg := Config{} + + stepsMaker := determineSteps(cfg) + suite.Same(&help, stepsMaker) +} From 6d28b7b28ad52596e953594a42bfbf329ba05640 Mon Sep 17 00:00:00 2001 From: Erin Call Date: Thu, 26 Dec 2019 11:29:33 -0800 Subject: [PATCH 2/4] Return an error on unknown commands [#15] I'm probably overthinking this--explicitly calling help is a strange and unusual case--but it doesn't really hurt, so I'm going for it. --- internal/helm/plan.go | 1 + internal/helm/plan_test.go | 8 +------- internal/run/config.go | 1 + internal/run/help.go | 11 +++++++++-- internal/run/help_test.go | 32 +++++++++++++++++++++++++++----- 5 files changed, 39 insertions(+), 14 deletions(-) diff --git a/internal/helm/plan.go b/internal/helm/plan.go index d5c8835..0f32c1e 100644 --- a/internal/helm/plan.go +++ b/internal/helm/plan.go @@ -26,6 +26,7 @@ func NewPlan(cfg Config) (*Plan, error) { p := Plan{ cfg: cfg, runCfg: run.Config{ + HelmCommand: string(cfg.Command), Debug: cfg.Debug, KubeConfig: cfg.KubeConfig, Values: cfg.Values, diff --git a/internal/helm/plan_test.go b/internal/helm/plan_test.go index eebf51b..cf3871d 100644 --- a/internal/helm/plan_test.go +++ b/internal/helm/plan_test.go @@ -40,6 +40,7 @@ func (suite *PlanTestSuite) TestNewPlan() { } runCfg := run.Config{ + HelmCommand: "help", Debug: false, KubeConfig: "/branch/.sfere/profig", Values: "steadfastness,forthrightness", @@ -255,10 +256,3 @@ func (suite *PlanTestSuite) TestDeterminePlanHelpCommand() { stepsMaker := determineSteps(cfg) suite.Same(&help, stepsMaker) } - -func (suite *PlanTestSuite) TestDeterminePlanHelpOnUnknown() { - cfg := Config{} - - stepsMaker := determineSteps(cfg) - suite.Same(&help, stepsMaker) -} diff --git a/internal/run/config.go b/internal/run/config.go index 09b9642..3d5b3f9 100644 --- a/internal/run/config.go +++ b/internal/run/config.go @@ -6,6 +6,7 @@ import ( // Config contains configuration applicable to all helm commands type Config struct { + HelmCommand string Debug bool KubeConfig string Values string diff --git a/internal/run/help.go b/internal/run/help.go index 8597815..a4a116e 100644 --- a/internal/run/help.go +++ b/internal/run/help.go @@ -10,8 +10,15 @@ type Help struct { } // Execute executes the `helm help` command. -func (h *Help) Execute(_ Config) error { - return h.cmd.Run() +func (h *Help) Execute(cfg Config) error { + if err := h.cmd.Run(); err != nil { + return fmt.Errorf("while running '%s': %w", h.cmd.String(), err) + } + + if cfg.HelmCommand == "help" { + return nil + } + return fmt.Errorf("unknown command '%s'", cfg.HelmCommand) } // Prepare gets the Help ready to execute. diff --git a/internal/run/help_test.go b/internal/run/help_test.go index 1824578..ecca2bb 100644 --- a/internal/run/help_test.go +++ b/internal/run/help_test.go @@ -38,9 +38,6 @@ func (suite *HelpTestSuite) TestPrepare() { Stdout(&stdout) mCmd.EXPECT(). Stderr(&stderr) - mCmd.EXPECT(). - Run(). - Times(1) cfg := Config{ Stdout: &stdout, @@ -49,8 +46,33 @@ func (suite *HelpTestSuite) TestPrepare() { h := Help{} err := h.Prepare(cfg) - suite.Require().Nil(err) - h.Execute(cfg) + suite.NoError(err) +} + +func (suite *HelpTestSuite) TestExecute() { + ctrl := gomock.NewController(suite.T()) + defer ctrl.Finish() + mCmd := NewMockcmd(ctrl) + originalCommand := command + command = func(_ string, _ ...string) cmd { + return mCmd + } + defer func() { command = originalCommand }() + + mCmd.EXPECT(). + Run(). + Times(2) + + cfg := Config{ + HelmCommand: "help", + } + help := Help{ + cmd: mCmd, + } + suite.NoError(help.Execute(cfg)) + + cfg.HelmCommand = "get down on friday" + suite.EqualError(help.Execute(cfg), "unknown command 'get down on friday'") } func (suite *HelpTestSuite) TestPrepareDebugFlag() { From 41e9e42239ac7aa5785ab80859eddf2d0a13812d Mon Sep 17 00:00:00 2001 From: Erin Call Date: Thu, 26 Dec 2019 11:31:45 -0800 Subject: [PATCH 3/4] Emit a trailing newline on execution error [#15] Just something I noticed while testing the help command's error case. --- cmd/drone-helm/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/drone-helm/main.go b/cmd/drone-helm/main.go index 61673b2..3ca31dd 100644 --- a/cmd/drone-helm/main.go +++ b/cmd/drone-helm/main.go @@ -28,7 +28,7 @@ func main() { // Expect the plan to go off the rails if err != nil { - fmt.Fprintf(os.Stderr, err.Error()) + fmt.Fprintf(os.Stderr, "%s\n", err.Error()) // Throw away the plan os.Exit(1) } From 167b53691b9eed6f1446c643fe2217aa0f0617f5 Mon Sep 17 00:00:00 2001 From: Erin Call Date: Thu, 26 Dec 2019 12:23:56 -0800 Subject: [PATCH 4/4] Put HelmCommand in Help, not run.Config [#15] --- internal/helm/plan.go | 5 +++-- internal/helm/plan_test.go | 1 - internal/run/config.go | 1 - internal/run/help.go | 7 ++++--- internal/run/help_test.go | 9 ++++----- 5 files changed, 11 insertions(+), 12 deletions(-) diff --git a/internal/helm/plan.go b/internal/helm/plan.go index 72fd48a..a82ffea 100644 --- a/internal/helm/plan.go +++ b/internal/helm/plan.go @@ -29,7 +29,6 @@ func NewPlan(cfg Config) (*Plan, error) { p := Plan{ cfg: cfg, runCfg: run.Config{ - HelmCommand: string(cfg.Command), Debug: cfg.Debug, Values: cfg.Values, StringValues: cfg.StringValues, @@ -132,7 +131,9 @@ var lint = func(cfg Config) []Step { } var help = func(cfg Config) []Step { - help := &run.Help{} + help := &run.Help{ + HelmCommand: cfg.Command, + } return []Step{help} } diff --git a/internal/helm/plan_test.go b/internal/helm/plan_test.go index fc70ec9..ce5311a 100644 --- a/internal/helm/plan_test.go +++ b/internal/helm/plan_test.go @@ -43,7 +43,6 @@ func (suite *PlanTestSuite) TestNewPlan() { } runCfg := run.Config{ - HelmCommand: "help", Debug: false, Values: "steadfastness,forthrightness", StringValues: "tensile_strength,flexibility", diff --git a/internal/run/config.go b/internal/run/config.go index 25fcd6f..4f9b99a 100644 --- a/internal/run/config.go +++ b/internal/run/config.go @@ -6,7 +6,6 @@ import ( // Config contains configuration applicable to all helm commands type Config struct { - HelmCommand string Debug bool Values string StringValues string diff --git a/internal/run/help.go b/internal/run/help.go index a4a116e..f2d6c59 100644 --- a/internal/run/help.go +++ b/internal/run/help.go @@ -6,7 +6,8 @@ import ( // Help is a step in a helm Plan that calls `helm help`. type Help struct { - cmd cmd + HelmCommand string + cmd cmd } // Execute executes the `helm help` command. @@ -15,10 +16,10 @@ func (h *Help) Execute(cfg Config) error { return fmt.Errorf("while running '%s': %w", h.cmd.String(), err) } - if cfg.HelmCommand == "help" { + if h.HelmCommand == "help" { return nil } - return fmt.Errorf("unknown command '%s'", cfg.HelmCommand) + return fmt.Errorf("unknown command '%s'", h.HelmCommand) } // Prepare gets the Help ready to execute. diff --git a/internal/run/help_test.go b/internal/run/help_test.go index ecca2bb..19c49d2 100644 --- a/internal/run/help_test.go +++ b/internal/run/help_test.go @@ -63,15 +63,14 @@ func (suite *HelpTestSuite) TestExecute() { Run(). Times(2) - cfg := Config{ - HelmCommand: "help", - } + cfg := Config{} help := Help{ - cmd: mCmd, + HelmCommand: "help", + cmd: mCmd, } suite.NoError(help.Execute(cfg)) - cfg.HelmCommand = "get down on friday" + help.HelmCommand = "get down on friday" suite.EqualError(help.Execute(cfg), "unknown command 'get down on friday'") }