Skip to content

Commit

Permalink
Merge pull request urfave#1696 from skelouse/v2-maint
Browse files Browse the repository at this point in the history
Fix linting issues
  • Loading branch information
dearchap authored Nov 30, 2023
2 parents 0e5f95d + 837ae14 commit d391482
Show file tree
Hide file tree
Showing 13 changed files with 329 additions and 158 deletions.
58 changes: 43 additions & 15 deletions altsrc/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ func (f *GenericFlag) ApplyInputSourceValue(cCtx *cli.Context, isc InputSourceCo
continue
}
for _, n := range f.Names() {
_ = f.set.Set(n, value.String())
if err := f.set.Set(n, value.String()); err != nil {
return err
}
}
}

Expand Down Expand Up @@ -109,10 +111,12 @@ func (f *StringSliceFlag) ApplyInputSourceValue(cCtx *cli.Context, isc InputSour
continue
}
underlyingFlag.Value = &sliceValue
f.set.Set(n, sliceValue.Serialize())
if err := f.set.Set(n, sliceValue.Serialize()); err != nil {
return err
}
}
if f.Destination != nil {
f.Destination.Set(sliceValue.Serialize())
_ = f.Destination.Set(sliceValue.Serialize())
}
}
return nil
Expand Down Expand Up @@ -143,7 +147,9 @@ func (f *IntSliceFlag) ApplyInputSourceValue(cCtx *cli.Context, isc InputSourceC
underlyingFlag.Value = &sliceValue
}
if f.Destination != nil {
f.Destination.Set(sliceValue.Serialize())
if err := f.Destination.Set(sliceValue.Serialize()); err != nil {
return err
}
}
}
return nil
Expand Down Expand Up @@ -174,7 +180,9 @@ func (f *Int64SliceFlag) ApplyInputSourceValue(cCtx *cli.Context, isc InputSourc
underlyingFlag.Value = &sliceValue
}
if f.Destination != nil {
f.Destination.Set(sliceValue.Serialize())
if err := f.Destination.Set(sliceValue.Serialize()); err != nil {
return err
}
}
}
return nil
Expand Down Expand Up @@ -205,7 +213,9 @@ func (f *Float64SliceFlag) ApplyInputSourceValue(cCtx *cli.Context, isc InputSou
underlyingFlag.Value = &sliceValue
}
if f.Destination != nil {
f.Destination.Set(sliceValue.Serialize())
if err := f.Destination.Set(sliceValue.Serialize()); err != nil {
return err
}
}
}
return nil
Expand All @@ -225,7 +235,9 @@ func (f *BoolFlag) ApplyInputSourceValue(cCtx *cli.Context, isc InputSourceConte
return err
}
for _, n := range f.Names() {
_ = f.set.Set(n, strconv.FormatBool(value))
if err := f.set.Set(n, strconv.FormatBool(value)); err != nil {
return err
}
}
}
return nil
Expand All @@ -245,7 +257,9 @@ func (f *StringFlag) ApplyInputSourceValue(cCtx *cli.Context, isc InputSourceCon
return err
}
for _, n := range f.Names() {
_ = f.set.Set(n, value)
if err := f.set.Set(n, value); err != nil {
return err
}
}
}
return nil
Expand Down Expand Up @@ -275,7 +289,9 @@ func (f *PathFlag) ApplyInputSourceValue(cCtx *cli.Context, isc InputSourceConte
}
value = filepath.Join(filepath.Dir(basePathAbs), value)
}
_ = f.set.Set(n, value)
if err := f.set.Set(n, value); err != nil {
return err
}
}
}
return nil
Expand All @@ -295,7 +311,9 @@ func (f *IntFlag) ApplyInputSourceValue(cCtx *cli.Context, isc InputSourceContex
return err
}
for _, n := range f.Names() {
_ = f.set.Set(n, strconv.FormatInt(int64(value), 10))
if err := f.set.Set(n, strconv.FormatInt(int64(value), 10)); err != nil {
return err
}
}
}
return nil
Expand All @@ -314,7 +332,9 @@ func (f *Int64Flag) ApplyInputSourceValue(cCtx *cli.Context, isc InputSourceCont
return err
}
for _, n := range f.Names() {
_ = f.set.Set(n, strconv.FormatInt(value, 10))
if err := f.set.Set(n, strconv.FormatInt(value, 10)); err != nil {
return err
}
}
}
return nil
Expand All @@ -333,7 +353,9 @@ func (f *UintFlag) ApplyInputSourceValue(cCtx *cli.Context, isc InputSourceConte
return err
}
for _, n := range f.Names() {
_ = f.set.Set(n, strconv.FormatUint(uint64(value), 10))
if err := f.set.Set(n, strconv.FormatUint(uint64(value), 10)); err != nil {
return err
}
}
}
return nil
Expand All @@ -352,7 +374,9 @@ func (f *Uint64Flag) ApplyInputSourceValue(cCtx *cli.Context, isc InputSourceCon
return err
}
for _, n := range f.Names() {
_ = f.set.Set(n, strconv.FormatUint(value, 10))
if err := f.set.Set(n, strconv.FormatUint(value, 10)); err != nil {
return err
}
}
}
return nil
Expand All @@ -372,7 +396,9 @@ func (f *DurationFlag) ApplyInputSourceValue(cCtx *cli.Context, isc InputSourceC
return err
}
for _, n := range f.Names() {
_ = f.set.Set(n, value.String())
if err := f.set.Set(n, value.String()); err != nil {
return err
}
}
}
return nil
Expand All @@ -393,7 +419,9 @@ func (f *Float64Flag) ApplyInputSourceValue(cCtx *cli.Context, isc InputSourceCo
}
floatStr := float64ToString(value)
for _, n := range f.Names() {
_ = f.set.Set(n, floatStr)
if err := f.set.Set(n, floatStr); err != nil {
return err
}
}
}
return nil
Expand Down
18 changes: 18 additions & 0 deletions altsrc/flag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,24 @@ func TestGenericApplyInputSourceValue(t *testing.T) {
refute(t, v, c.Generic("test"))
}

func TestGenericApplyInputSourceValueError(t *testing.T) {
set := flag.NewFlagSet("", flag.ContinueOnError)
c := cli.NewContext(nil, set, nil)

testFlag := &GenericFlag{
GenericFlag: &cli.GenericFlag{
Name: "test",
Value: &cli.StringSlice{},
},
set: set,
}

err := testFlag.ApplyInputSourceValue(c, NewMapInputSource("", map[interface{}]interface{}{
"test": testFlag.Value,
}))
expect(t, err, fmt.Errorf("no such flag -test"))
}

func TestGenericApplyInputSourceMethodContextSet(t *testing.T) {
p := &Parser{"abc", "def"}
tis := testApplyInputSource{
Expand Down
24 changes: 0 additions & 24 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -458,30 +458,6 @@ func (a *App) handleExitCoder(cCtx *Context, err error) {
}
}

func (a *App) commandNames() []string {
var cmdNames []string

for _, cmd := range a.Commands {
cmdNames = append(cmdNames, cmd.Names()...)
}

return cmdNames
}

func (a *App) validCommandName(checkCmdName string) bool {
valid := false
allCommandNames := a.commandNames()

for _, cmdName := range allCommandNames {
if checkCmdName == cmdName {
valid = true
break
}
}

return valid
}

func (a *App) argsWithDefaultCommand(oldArgs Args) Args {
if a.DefaultCommand != "" {
rawArgs := append([]string{a.DefaultCommand}, oldArgs.Slice()...)
Expand Down
64 changes: 33 additions & 31 deletions app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ func ExampleApp_Run() {
Authors: []*Author{{Name: "Oliver Allen", Email: "[email protected]"}},
}

app.Run(os.Args)
if err := app.Run(os.Args); err != nil {
return
}
// Output:
// Hello Jeremy
}
Expand Down Expand Up @@ -2719,8 +2721,8 @@ func TestFlagAction(t *testing.T) {
if v == "" {
return fmt.Errorf("empty string")
}
c.App.Writer.Write([]byte(v + " "))
return nil
_, err := c.App.Writer.Write([]byte(v + " "))
return err
},
}
app := &App{
Expand Down Expand Up @@ -2750,8 +2752,8 @@ func TestFlagAction(t *testing.T) {
if v[0] == "err" {
return fmt.Errorf("error string slice")
}
c.App.Writer.Write([]byte(fmt.Sprintf("%v ", v)))
return nil
_, err := c.App.Writer.Write([]byte(fmt.Sprintf("%v ", v)))
return err
},
},
&BoolFlag{
Expand All @@ -2760,8 +2762,8 @@ func TestFlagAction(t *testing.T) {
if !v {
return fmt.Errorf("value is false")
}
c.App.Writer.Write([]byte(fmt.Sprintf("%t ", v)))
return nil
_, err := c.App.Writer.Write([]byte(fmt.Sprintf("%t ", v)))
return err
},
},
&DurationFlag{
Expand All @@ -2770,8 +2772,8 @@ func TestFlagAction(t *testing.T) {
if v == 0 {
return fmt.Errorf("empty duration")
}
c.App.Writer.Write([]byte(v.String() + " "))
return nil
_, err := c.App.Writer.Write([]byte(v.String() + " "))
return err
},
},
&Float64Flag{
Expand All @@ -2780,8 +2782,8 @@ func TestFlagAction(t *testing.T) {
if v < 0 {
return fmt.Errorf("negative float64")
}
c.App.Writer.Write([]byte(strconv.FormatFloat(v, 'f', -1, 64) + " "))
return nil
_, err := c.App.Writer.Write([]byte(strconv.FormatFloat(v, 'f', -1, 64) + " "))
return err
},
},
&Float64SliceFlag{
Expand All @@ -2790,8 +2792,8 @@ func TestFlagAction(t *testing.T) {
if len(v) > 0 && v[0] < 0 {
return fmt.Errorf("invalid float64 slice")
}
c.App.Writer.Write([]byte(fmt.Sprintf("%v ", v)))
return nil
_, err := c.App.Writer.Write([]byte(fmt.Sprintf("%v ", v)))
return err
},
},
&GenericFlag{
Expand All @@ -2806,8 +2808,8 @@ func TestFlagAction(t *testing.T) {
}
}

c.App.Writer.Write([]byte(fmt.Sprintf("%v ", v)))
return nil
_, err := c.App.Writer.Write([]byte(fmt.Sprintf("%v ", v)))
return err
},
},
&IntFlag{
Expand All @@ -2816,8 +2818,8 @@ func TestFlagAction(t *testing.T) {
if v < 0 {
return fmt.Errorf("negative int")
}
c.App.Writer.Write([]byte(fmt.Sprintf("%v ", v)))
return nil
_, err := c.App.Writer.Write([]byte(fmt.Sprintf("%v ", v)))
return err
},
},
&IntSliceFlag{
Expand All @@ -2826,8 +2828,8 @@ func TestFlagAction(t *testing.T) {
if len(v) > 0 && v[0] < 0 {
return fmt.Errorf("invalid int slice")
}
c.App.Writer.Write([]byte(fmt.Sprintf("%v ", v)))
return nil
_, err := c.App.Writer.Write([]byte(fmt.Sprintf("%v ", v)))
return err
},
},
&Int64Flag{
Expand All @@ -2836,8 +2838,8 @@ func TestFlagAction(t *testing.T) {
if v < 0 {
return fmt.Errorf("negative int64")
}
c.App.Writer.Write([]byte(fmt.Sprintf("%v ", v)))
return nil
_, err := c.App.Writer.Write([]byte(fmt.Sprintf("%v ", v)))
return err
},
},
&Int64SliceFlag{
Expand All @@ -2846,8 +2848,8 @@ func TestFlagAction(t *testing.T) {
if len(v) > 0 && v[0] < 0 {
return fmt.Errorf("invalid int64 slice")
}
c.App.Writer.Write([]byte(fmt.Sprintf("%v ", v)))
return nil
_, err := c.App.Writer.Write([]byte(fmt.Sprintf("%v ", v)))
return err
},
},
&PathFlag{
Expand All @@ -2856,8 +2858,8 @@ func TestFlagAction(t *testing.T) {
if v == "" {
return fmt.Errorf("empty path")
}
c.App.Writer.Write([]byte(fmt.Sprintf("%v ", v)))
return nil
_, err := c.App.Writer.Write([]byte(fmt.Sprintf("%v ", v)))
return err
},
},
&TimestampFlag{
Expand All @@ -2867,8 +2869,8 @@ func TestFlagAction(t *testing.T) {
if v.IsZero() {
return fmt.Errorf("zero timestamp")
}
c.App.Writer.Write([]byte(v.Format(time.RFC3339) + " "))
return nil
_, err := c.App.Writer.Write([]byte(v.Format(time.RFC3339) + " "))
return err
},
},
&UintFlag{
Expand All @@ -2877,8 +2879,8 @@ func TestFlagAction(t *testing.T) {
if v == 0 {
return fmt.Errorf("zero uint")
}
c.App.Writer.Write([]byte(fmt.Sprintf("%v ", v)))
return nil
_, err := c.App.Writer.Write([]byte(fmt.Sprintf("%v ", v)))
return err
},
},
&Uint64Flag{
Expand All @@ -2887,8 +2889,8 @@ func TestFlagAction(t *testing.T) {
if v == 0 {
return fmt.Errorf("zero uint64")
}
c.App.Writer.Write([]byte(fmt.Sprintf("%v ", v)))
return nil
_, err := c.App.Writer.Write([]byte(fmt.Sprintf("%v ", v)))
return err
},
},
},
Expand Down
2 changes: 1 addition & 1 deletion category.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func newFlagCategoriesFromFlags(fs []Flag) FlagCategories {
}
}

if categorized == true {
if categorized {
for _, fl := range fs {
if cf, ok := fl.(CategorizableFlag); ok {
if cf.GetCategory() == "" {
Expand Down
Loading

0 comments on commit d391482

Please sign in to comment.