Merge branch 'develop' of https://github.com/txgruppi/cli into txgruppi-develop

This commit is contained in:
Dan Buch
2016-04-25 18:10:10 -04:00
11 changed files with 346 additions and 197 deletions

View File

@@ -38,28 +38,28 @@ func ApplyInputSourceValues(context *cli.Context, inputSourceContext InputSource
// InitInputSource is used to to setup an InputSourceContext on a cli.Command Before method. It will create a new
// 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)) func(context *cli.Context) error {
return func(context *cli.Context) error {
func InitInputSource(flags []cli.Flag, createInputSource func() (InputSourceContext, error)) cli.BeforeFn {
return func(context *cli.Context) (int, error) {
inputSource, err := createInputSource()
if err != nil {
return fmt.Errorf("Unable to create input source: inner error: \n'%v'", err.Error())
return cli.DefaultErrorExitCode, fmt.Errorf("Unable to create input source: inner error: \n'%v'", err.Error())
}
return ApplyInputSourceValues(context, inputSource, flags)
return cli.DefaultSuccessExitCode, ApplyInputSourceValues(context, inputSource, flags)
}
}
// InitInputSourceWithContext is used to to setup an InputSourceContext on a cli.Command Before method. It will create a new
// 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)) func(context *cli.Context) error {
return func(context *cli.Context) error {
func InitInputSourceWithContext(flags []cli.Flag, createInputSource func(context *cli.Context) (InputSourceContext, error)) cli.BeforeFn {
return func(context *cli.Context) (int, error) {
inputSource, err := createInputSource(context)
if err != nil {
return fmt.Errorf("Unable to create input source with context: inner error: \n'%v'", err.Error())
return cli.DefaultErrorExitCode, fmt.Errorf("Unable to create input source with context: inner error: \n'%v'", err.Error())
}
return ApplyInputSourceValues(context, inputSource, flags)
return cli.DefaultSuccessExitCode, ApplyInputSourceValues(context, inputSource, flags)
}
}

View File

@@ -29,16 +29,17 @@ func TestCommandYamlFileTest(t *testing.T) {
Aliases: []string{"tc"},
Usage: "this is for testing",
Description: "testing",
Action: func(c *cli.Context) {
Action: func(c *cli.Context) int {
val := c.Int("test")
expect(t, val, 15)
return 0
},
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)
}
@@ -61,9 +62,10 @@ func TestCommandYamlFileTestGlobalEnvVarWins(t *testing.T) {
Aliases: []string{"tc"},
Usage: "this is for testing",
Description: "testing",
Action: func(c *cli.Context) {
Action: func(c *cli.Context) int {
val := c.Int("test")
expect(t, val, 10)
return 0
},
Flags: []cli.Flag{
NewIntFlag(cli.IntFlag{Name: "test", EnvVar: "THE_TEST"}),
@@ -71,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)
}
@@ -92,9 +94,10 @@ func TestCommandYamlFileTestSpecifiedFlagWins(t *testing.T) {
Aliases: []string{"tc"},
Usage: "this is for testing",
Description: "testing",
Action: func(c *cli.Context) {
Action: func(c *cli.Context) int {
val := c.Int("test")
expect(t, val, 7)
return 0
},
Flags: []cli.Flag{
NewIntFlag(cli.IntFlag{Name: "test"}),
@@ -102,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)
}
@@ -123,9 +126,10 @@ func TestCommandYamlFileTestDefaultValueFileWins(t *testing.T) {
Aliases: []string{"tc"},
Usage: "this is for testing",
Description: "testing",
Action: func(c *cli.Context) {
Action: func(c *cli.Context) int {
val := c.Int("test")
expect(t, val, 15)
return 0
},
Flags: []cli.Flag{
NewIntFlag(cli.IntFlag{Name: "test", Value: 7}),
@@ -133,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)
}
@@ -157,16 +161,17 @@ func TestCommandYamlFileFlagHasDefaultGlobalEnvYamlSetGlobalEnvWins(t *testing.T
Aliases: []string{"tc"},
Usage: "this is for testing",
Description: "testing",
Action: func(c *cli.Context) {
Action: func(c *cli.Context) int {
val := c.Int("test")
expect(t, val, 11)
return 0
},
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)
}