Switch from multi-return with exit codes to ExitCoder check

This commit is contained in:
Dan Buch
2016-04-27 09:12:34 -04:00
parent b40b62794d
commit b7329f4968
11 changed files with 291 additions and 243 deletions

View File

@@ -39,13 +39,13 @@ func ApplyInputSourceValues(context *cli.Context, inputSourceContext InputSource
// input source based on the func provided. If there is no error it will then apply the new input source to any flags
// that are supported by the input source
func InitInputSource(flags []cli.Flag, createInputSource func() (InputSourceContext, error)) cli.BeforeFunc {
return func(context *cli.Context) (int, error) {
return func(context *cli.Context) error {
inputSource, err := createInputSource()
if err != nil {
return cli.DefaultErrorExitCode, fmt.Errorf("Unable to create input source: inner error: \n'%v'", err.Error())
return fmt.Errorf("Unable to create input source: inner error: \n'%v'", err.Error())
}
return cli.DefaultSuccessExitCode, ApplyInputSourceValues(context, inputSource, flags)
return ApplyInputSourceValues(context, inputSource, flags)
}
}
@@ -53,13 +53,13 @@ func InitInputSource(flags []cli.Flag, createInputSource func() (InputSourceCont
// input source based on the func provided with potentially using existing cli.Context values to initialize itself. If there is
// no error it will then apply the new input source to any flags that are supported by the input source
func InitInputSourceWithContext(flags []cli.Flag, createInputSource func(context *cli.Context) (InputSourceContext, error)) cli.BeforeFunc {
return func(context *cli.Context) (int, error) {
return func(context *cli.Context) error {
inputSource, err := createInputSource(context)
if err != nil {
return cli.DefaultErrorExitCode, fmt.Errorf("Unable to create input source with context: inner error: \n'%v'", err.Error())
return fmt.Errorf("Unable to create input source with context: inner error: \n'%v'", err.Error())
}
return cli.DefaultSuccessExitCode, ApplyInputSourceValues(context, inputSource, flags)
return ApplyInputSourceValues(context, inputSource, flags)
}
}

View File

@@ -29,17 +29,17 @@ func TestCommandYamlFileTest(t *testing.T) {
Aliases: []string{"tc"},
Usage: "this is for testing",
Description: "testing",
Action: func(c *cli.Context) int {
Action: func(c *cli.Context) error {
val := c.Int("test")
expect(t, val, 15)
return 0
return nil
},
Flags: []cli.Flag{
NewIntFlag(cli.IntFlag{Name: "test"}),
cli.StringFlag{Name: "load"}},
}
command.Before = InitInputSourceWithContext(command.Flags, NewYamlSourceFromFlagFunc("load"))
_, err := command.Run(c)
err := command.Run(c)
expect(t, err, nil)
}
@@ -62,10 +62,10 @@ func TestCommandYamlFileTestGlobalEnvVarWins(t *testing.T) {
Aliases: []string{"tc"},
Usage: "this is for testing",
Description: "testing",
Action: func(c *cli.Context) int {
Action: func(c *cli.Context) error {
val := c.Int("test")
expect(t, val, 10)
return 0
return nil
},
Flags: []cli.Flag{
NewIntFlag(cli.IntFlag{Name: "test", EnvVar: "THE_TEST"}),
@@ -73,7 +73,7 @@ func TestCommandYamlFileTestGlobalEnvVarWins(t *testing.T) {
}
command.Before = InitInputSourceWithContext(command.Flags, NewYamlSourceFromFlagFunc("load"))
_, err := command.Run(c)
err := command.Run(c)
expect(t, err, nil)
}
@@ -94,10 +94,10 @@ func TestCommandYamlFileTestSpecifiedFlagWins(t *testing.T) {
Aliases: []string{"tc"},
Usage: "this is for testing",
Description: "testing",
Action: func(c *cli.Context) int {
Action: func(c *cli.Context) error {
val := c.Int("test")
expect(t, val, 7)
return 0
return nil
},
Flags: []cli.Flag{
NewIntFlag(cli.IntFlag{Name: "test"}),
@@ -105,7 +105,7 @@ func TestCommandYamlFileTestSpecifiedFlagWins(t *testing.T) {
}
command.Before = InitInputSourceWithContext(command.Flags, NewYamlSourceFromFlagFunc("load"))
_, err := command.Run(c)
err := command.Run(c)
expect(t, err, nil)
}
@@ -126,10 +126,10 @@ func TestCommandYamlFileTestDefaultValueFileWins(t *testing.T) {
Aliases: []string{"tc"},
Usage: "this is for testing",
Description: "testing",
Action: func(c *cli.Context) int {
Action: func(c *cli.Context) error {
val := c.Int("test")
expect(t, val, 15)
return 0
return nil
},
Flags: []cli.Flag{
NewIntFlag(cli.IntFlag{Name: "test", Value: 7}),
@@ -137,7 +137,7 @@ func TestCommandYamlFileTestDefaultValueFileWins(t *testing.T) {
}
command.Before = InitInputSourceWithContext(command.Flags, NewYamlSourceFromFlagFunc("load"))
_, err := command.Run(c)
err := command.Run(c)
expect(t, err, nil)
}
@@ -161,17 +161,17 @@ func TestCommandYamlFileFlagHasDefaultGlobalEnvYamlSetGlobalEnvWins(t *testing.T
Aliases: []string{"tc"},
Usage: "this is for testing",
Description: "testing",
Action: func(c *cli.Context) int {
Action: func(c *cli.Context) error {
val := c.Int("test")
expect(t, val, 11)
return 0
return nil
},
Flags: []cli.Flag{
NewIntFlag(cli.IntFlag{Name: "test", Value: 7, EnvVar: "THE_TEST"}),
cli.StringFlag{Name: "load"}},
}
command.Before = InitInputSourceWithContext(command.Flags, NewYamlSourceFromFlagFunc("load"))
_, err := command.Run(c)
err := command.Run(c)
expect(t, err, nil)
}