Fix:(issue_1455) Allow bool flags from input altsrc

This commit is contained in:
Naveen Gogineni 2022-08-16 18:34:34 -04:00
parent 7357e10160
commit f394c3779d
5 changed files with 30 additions and 19 deletions

View File

@ -124,13 +124,14 @@ func (f *BoolFlag) ApplyInputSourceValue(cCtx *cli.Context, isc InputSourceConte
if f.set != nil && !cCtx.IsSet(f.Name) && !isEnvVarSet(f.EnvVars) && isc.isSet(f.BoolFlag.Name) { if f.set != nil && !cCtx.IsSet(f.Name) && !isEnvVarSet(f.EnvVars) && isc.isSet(f.BoolFlag.Name) {
value, err := isc.Bool(f.BoolFlag.Name) value, err := isc.Bool(f.BoolFlag.Name)
if err != nil { if err != nil {
fmt.Println(err)
return err return err
} }
if value { for _, name := range f.Names() {
for _, name := range f.Names() { _ = f.set.Set(name, strconv.FormatBool(value))
_ = f.set.Set(name, strconv.FormatBool(value))
}
} }
} else {
fmt.Println("not fill")
} }
return nil return nil
} }

View File

@ -11,7 +11,7 @@ import (
const ( const (
fileName = "current.json" fileName = "current.json"
simpleJSON = `{"test": 15}` simpleJSON = `{"test": 15, "testb": false}`
nestedJSON = `{"top": {"test": 15}}` nestedJSON = `{"top": {"test": 15}}`
) )
@ -34,11 +34,16 @@ func TestCommandJSONFileTest(t *testing.T) {
Action: func(c *cli.Context) error { Action: func(c *cli.Context) error {
val := c.Int("test") val := c.Int("test")
expect(t, val, 15) expect(t, val, 15)
valb := c.Bool("testb")
expect(t, valb, false)
return nil return nil
}, },
Flags: []cli.Flag{ Flags: []cli.Flag{
NewIntFlag(&cli.IntFlag{Name: "test"}), NewIntFlag(&cli.IntFlag{Name: "test"}),
&cli.StringFlag{Name: "load"}}, &cli.StringFlag{Name: "load"},
NewBoolFlag(&cli.BoolFlag{Name: "testb", Value: true}),
},
} }
command.Before = InitInputSourceWithContext(command.Flags, NewJSONSourceFromFlagFunc("load")) command.Before = InitInputSourceWithContext(command.Flags, NewJSONSourceFromFlagFunc("load"))
err := command.Run(c) err := command.Run(c)

5
go.mod
View File

@ -9,4 +9,7 @@ require (
gopkg.in/yaml.v3 v3.0.1 gopkg.in/yaml.v3 v3.0.1
) )
require github.com/russross/blackfriday/v2 v2.1.0 // indirect require (
github.com/russross/blackfriday/v2 v2.1.0 // indirect
golang.org/x/text v0.3.7 // indirect
)

2
go.sum
View File

@ -6,6 +6,8 @@ github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU= github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU=
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8= github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8=
golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= 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/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

View File

@ -5,24 +5,24 @@ line Go applications. cli is designed to be easy to understand and write,
the most simple cli application can be written as follows: the most simple cli application can be written as follows:
func main() { func main() {
(&cli.App{}).Run(os.Args) (&cli.App{}).Run(os.Args)
} }
Of course this application does not do much, so let's make this an actual Of course this application does not do much, so let's make this an actual
application: application:
func main() { func main() {
app := &cli.App{ app := &cli.App{
Name: "greet", Name: "greet",
Usage: "say a greeting", Usage: "say a greeting",
Action: func(c *cli.Context) error { Action: func(c *cli.Context) error {
fmt.Println("Greetings") fmt.Println("Greetings")
return nil return nil
}, },
} }
app.Run(os.Args) app.Run(os.Args)
} }
VARIABLES VARIABLES