Repository: urfave/cli
Pull request #1456 (2022-08-16 22:36:09)

Fix:(issue_1455) Allow bool flags from input altsrc
<!--
  This template provides some ideas of things to include in your PR description.
  To start, try providing a short summary of your changes in the Title above.
  If a section of the PR template does not apply to this PR, then delete that section.
 -->

## What type of PR is this?


<!--
  Delete any of the following that do not apply:
 -->

- bug

## What this PR does / why we need it:


<!--
  What goal is this change working towards?
  Provide a bullet pointed summary of how each file was changed.
  Briefly explain any decisions you made with respect to the changes.
  Include anything here that you didn't include in *Release Notes*
  above, such as changes to CI or changes to internal methods.
-->
Allow bool values to be set from altsrc files

## Which issue(s) this PR fixes:

<!--
If this PR fixes one of more issues, list them here.
One line each, like so:

Fixes #1455 
-->

## Special notes for your reviewer:

_(fill-in or delete this section)_

<!--
   Is there any particular feedback you would / wouldn't like?
   Which parts of the code should reviewers focus on?
-->

## Testing

_(fill-in or delete this section)_

<!--
  Describe how you tested this change.
-->

Updated unit tests 

## Release Notes

_(REQUIRED)_
<!--
  If this PR makes user facing changes, please describe them here. This
  description will be copied into the release notes/changelog, whenever the
  next version is released. Keep this section short, and focus on high level
  changes.

  Put your text between the block. To omit notes, use NONE within the block.
-->

```release-note

```



Patch:
diff --git a/altsrc/flag.go b/altsrc/flag.go
index db95949..9801644 100644
--- a/altsrc/flag.go
+++ b/altsrc/flag.go
@@ -126,10 +126,8 @@ func (f *BoolFlag) ApplyInputSourceValue(cCtx *cli.Context, isc InputSourceConte
 		if err != nil {
 			return err
 		}
-		if value {
-			for _, name := range f.Names() {
-				_ = f.set.Set(name, strconv.FormatBool(value))
-			}
+		for _, name := range f.Names() {
+			_ = f.set.Set(name, strconv.FormatBool(value))
 		}
 	}
 	return nil
diff --git a/go.mod b/go.mod
index 7fa4542..09ef147 100644
--- a/go.mod
+++ b/go.mod
@@ -9,4 +9,7 @@ require (
 	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
+)
diff --git a/go.sum b/go.sum
index 0756e41..96058c7 100644
--- a/go.sum
+++ b/go.sum
@@ -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/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU=
 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/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
 gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=


Test patch:
diff --git a/altsrc/json_command_test.go b/altsrc/json_command_test.go
index bd0022b..297fc9a 100644
--- a/altsrc/json_command_test.go
+++ b/altsrc/json_command_test.go
@@ -11,7 +11,7 @@ import (
 
 const (
 	fileName   = "current.json"
-	simpleJSON = `{"test": 15}`
+	simpleJSON = `{"test": 15, "testb": false}`
 	nestedJSON = `{"top": {"test": 15}}`
 )
 
@@ -34,11 +34,16 @@ func TestCommandJSONFileTest(t *testing.T) {
 		Action: func(c *cli.Context) error {
 			val := c.Int("test")
 			expect(t, val, 15)
+
+			valb := c.Bool("testb")
+			expect(t, valb, false)
 			return nil
 		},
 		Flags: []cli.Flag{
 			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"))
 	err := command.Run(c)