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.
This commit is contained in:
parent
34b9ec1c4c
commit
6d28b7b28a
|
@ -26,6 +26,7 @@ func NewPlan(cfg Config) (*Plan, error) {
|
||||||
p := Plan{
|
p := Plan{
|
||||||
cfg: cfg,
|
cfg: cfg,
|
||||||
runCfg: run.Config{
|
runCfg: run.Config{
|
||||||
|
HelmCommand: string(cfg.Command),
|
||||||
Debug: cfg.Debug,
|
Debug: cfg.Debug,
|
||||||
KubeConfig: cfg.KubeConfig,
|
KubeConfig: cfg.KubeConfig,
|
||||||
Values: cfg.Values,
|
Values: cfg.Values,
|
||||||
|
|
|
@ -40,6 +40,7 @@ func (suite *PlanTestSuite) TestNewPlan() {
|
||||||
}
|
}
|
||||||
|
|
||||||
runCfg := run.Config{
|
runCfg := run.Config{
|
||||||
|
HelmCommand: "help",
|
||||||
Debug: false,
|
Debug: false,
|
||||||
KubeConfig: "/branch/.sfere/profig",
|
KubeConfig: "/branch/.sfere/profig",
|
||||||
Values: "steadfastness,forthrightness",
|
Values: "steadfastness,forthrightness",
|
||||||
|
@ -255,10 +256,3 @@ func (suite *PlanTestSuite) TestDeterminePlanHelpCommand() {
|
||||||
stepsMaker := determineSteps(cfg)
|
stepsMaker := determineSteps(cfg)
|
||||||
suite.Same(&help, stepsMaker)
|
suite.Same(&help, stepsMaker)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (suite *PlanTestSuite) TestDeterminePlanHelpOnUnknown() {
|
|
||||||
cfg := Config{}
|
|
||||||
|
|
||||||
stepsMaker := determineSteps(cfg)
|
|
||||||
suite.Same(&help, stepsMaker)
|
|
||||||
}
|
|
||||||
|
|
|
@ -6,6 +6,7 @@ import (
|
||||||
|
|
||||||
// Config contains configuration applicable to all helm commands
|
// Config contains configuration applicable to all helm commands
|
||||||
type Config struct {
|
type Config struct {
|
||||||
|
HelmCommand string
|
||||||
Debug bool
|
Debug bool
|
||||||
KubeConfig string
|
KubeConfig string
|
||||||
Values string
|
Values string
|
||||||
|
|
|
@ -10,8 +10,15 @@ type Help struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Execute executes the `helm help` command.
|
// Execute executes the `helm help` command.
|
||||||
func (h *Help) Execute(_ Config) error {
|
func (h *Help) Execute(cfg Config) error {
|
||||||
return h.cmd.Run()
|
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.
|
// Prepare gets the Help ready to execute.
|
||||||
|
|
|
@ -38,9 +38,6 @@ func (suite *HelpTestSuite) TestPrepare() {
|
||||||
Stdout(&stdout)
|
Stdout(&stdout)
|
||||||
mCmd.EXPECT().
|
mCmd.EXPECT().
|
||||||
Stderr(&stderr)
|
Stderr(&stderr)
|
||||||
mCmd.EXPECT().
|
|
||||||
Run().
|
|
||||||
Times(1)
|
|
||||||
|
|
||||||
cfg := Config{
|
cfg := Config{
|
||||||
Stdout: &stdout,
|
Stdout: &stdout,
|
||||||
|
@ -49,8 +46,33 @@ func (suite *HelpTestSuite) TestPrepare() {
|
||||||
|
|
||||||
h := Help{}
|
h := Help{}
|
||||||
err := h.Prepare(cfg)
|
err := h.Prepare(cfg)
|
||||||
suite.Require().Nil(err)
|
suite.NoError(err)
|
||||||
h.Execute(cfg)
|
}
|
||||||
|
|
||||||
|
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() {
|
func (suite *HelpTestSuite) TestPrepareDebugFlag() {
|
||||||
|
|
Loading…
Reference in a new issue