diff --git a/altsrc.go b/altsrc.go deleted file mode 100644 index ac34bf6..0000000 --- a/altsrc.go +++ /dev/null @@ -1,3 +0,0 @@ -package altsrc - -//go:generate python ../generate-flag-types altsrc -i ../flag-types.json -o flag_generated.go diff --git a/flag.go b/flag.go index 858e54e..31b8a04 100644 --- a/flag.go +++ b/flag.go @@ -6,7 +6,7 @@ import ( "strconv" "syscall" - "gopkg.in/urfave/cli.v2" + "github.com/urfave/cli/v2" ) // FlagInputSourceExtension is an extension interface of cli.Flag that @@ -72,7 +72,7 @@ func (f *GenericFlag) ApplyInputSourceValue(context *cli.Context, isc InputSourc } if value != nil { for _, name := range f.Names() { - f.set.Set(name, value.String()) + _ = f.set.Set(name, value.String()) } } } @@ -135,7 +135,7 @@ func (f *BoolFlag) ApplyInputSourceValue(context *cli.Context, isc InputSourceCo } if value { for _, name := range f.Names() { - f.set.Set(name, strconv.FormatBool(value)) + _ = f.set.Set(name, strconv.FormatBool(value)) } } } @@ -153,7 +153,7 @@ func (f *StringFlag) ApplyInputSourceValue(context *cli.Context, isc InputSource } if value != "" { for _, name := range f.Names() { - f.set.Set(name, value) + _ = f.set.Set(name, value) } } } @@ -181,7 +181,7 @@ func (f *PathFlag) ApplyInputSourceValue(context *cli.Context, isc InputSourceCo value = filepath.Join(filepath.Dir(basePathAbs), value) } - f.set.Set(name, value) + _ = f.set.Set(name, value) } } } @@ -199,7 +199,7 @@ func (f *IntFlag) ApplyInputSourceValue(context *cli.Context, isc InputSourceCon } if value > 0 { for _, name := range f.Names() { - f.set.Set(name, strconv.FormatInt(int64(value), 10)) + _ = f.set.Set(name, strconv.FormatInt(int64(value), 10)) } } } @@ -217,7 +217,7 @@ func (f *DurationFlag) ApplyInputSourceValue(context *cli.Context, isc InputSour } if value > 0 { for _, name := range f.Names() { - f.set.Set(name, value.String()) + _ = f.set.Set(name, value.String()) } } } @@ -236,7 +236,7 @@ func (f *Float64Flag) ApplyInputSourceValue(context *cli.Context, isc InputSourc if value > 0 { floatStr := float64ToString(value) for _, name := range f.Names() { - f.set.Set(name, floatStr) + _ = f.set.Set(name, floatStr) } } } diff --git a/flag_generated.go b/flag_generated.go index 87c7c5d..de3e2b5 100644 --- a/flag_generated.go +++ b/flag_generated.go @@ -2,12 +2,9 @@ package altsrc import ( "flag" - - "gopkg.in/urfave/cli.v2" + "github.com/urfave/cli/v2" ) -// WARNING: This file is generated! - // BoolFlag is the flag type that wraps cli.BoolFlag to allow // for other values to be specified type BoolFlag struct { @@ -20,18 +17,11 @@ func NewBoolFlag(fl *cli.BoolFlag) *BoolFlag { return &BoolFlag{BoolFlag: fl, set: nil} } -// Apply saves the flagSet for later usage calls, then calls the -// wrapped BoolFlag.Apply -func (f *BoolFlag) Apply(set *flag.FlagSet) { +// Apply saves the flagSet for later usage calls, then calls +// the wrapped BoolFlag.Apply +func (f *BoolFlag) Apply(set *flag.FlagSet) error { f.set = set - f.BoolFlag.Apply(set) -} - -// ApplyWithError saves the flagSet for later usage calls, then calls the -// wrapped BoolFlag.ApplyWithError -func (f *BoolFlag) ApplyWithError(set *flag.FlagSet) error { - f.set = set - return f.BoolFlag.ApplyWithError(set) + return f.BoolFlag.Apply(set) } // DurationFlag is the flag type that wraps cli.DurationFlag to allow @@ -46,18 +36,11 @@ func NewDurationFlag(fl *cli.DurationFlag) *DurationFlag { return &DurationFlag{DurationFlag: fl, set: nil} } -// Apply saves the flagSet for later usage calls, then calls the -// wrapped DurationFlag.Apply -func (f *DurationFlag) Apply(set *flag.FlagSet) { - f.set = set - f.DurationFlag.Apply(set) -} - -// ApplyWithError saves the flagSet for later usage calls, then calls the -// wrapped DurationFlag.ApplyWithError -func (f *DurationFlag) ApplyWithError(set *flag.FlagSet) error { +// Apply saves the flagSet for later usage calls, then calls +// the wrapped DurationFlag.Apply +func (f *DurationFlag) Apply(set *flag.FlagSet) error { f.set = set - return f.DurationFlag.ApplyWithError(set) + return f.DurationFlag.Apply(set) } // Float64Flag is the flag type that wraps cli.Float64Flag to allow @@ -72,18 +55,11 @@ func NewFloat64Flag(fl *cli.Float64Flag) *Float64Flag { return &Float64Flag{Float64Flag: fl, set: nil} } -// Apply saves the flagSet for later usage calls, then calls the -// wrapped Float64Flag.Apply -func (f *Float64Flag) Apply(set *flag.FlagSet) { - f.set = set - f.Float64Flag.Apply(set) -} - -// ApplyWithError saves the flagSet for later usage calls, then calls the -// wrapped Float64Flag.ApplyWithError -func (f *Float64Flag) ApplyWithError(set *flag.FlagSet) error { +// Apply saves the flagSet for later usage calls, then calls +// the wrapped Float64Flag.Apply +func (f *Float64Flag) Apply(set *flag.FlagSet) error { f.set = set - return f.Float64Flag.ApplyWithError(set) + return f.Float64Flag.Apply(set) } // GenericFlag is the flag type that wraps cli.GenericFlag to allow @@ -98,18 +74,11 @@ func NewGenericFlag(fl *cli.GenericFlag) *GenericFlag { return &GenericFlag{GenericFlag: fl, set: nil} } -// Apply saves the flagSet for later usage calls, then calls the -// wrapped GenericFlag.Apply -func (f *GenericFlag) Apply(set *flag.FlagSet) { - f.set = set - f.GenericFlag.Apply(set) -} - -// ApplyWithError saves the flagSet for later usage calls, then calls the -// wrapped GenericFlag.ApplyWithError -func (f *GenericFlag) ApplyWithError(set *flag.FlagSet) error { +// Apply saves the flagSet for later usage calls, then calls +// the wrapped GenericFlag.Apply +func (f *GenericFlag) Apply(set *flag.FlagSet) error { f.set = set - return f.GenericFlag.ApplyWithError(set) + return f.GenericFlag.Apply(set) } // Int64Flag is the flag type that wraps cli.Int64Flag to allow @@ -124,18 +93,11 @@ func NewInt64Flag(fl *cli.Int64Flag) *Int64Flag { return &Int64Flag{Int64Flag: fl, set: nil} } -// Apply saves the flagSet for later usage calls, then calls the -// wrapped Int64Flag.Apply -func (f *Int64Flag) Apply(set *flag.FlagSet) { - f.set = set - f.Int64Flag.Apply(set) -} - -// ApplyWithError saves the flagSet for later usage calls, then calls the -// wrapped Int64Flag.ApplyWithError -func (f *Int64Flag) ApplyWithError(set *flag.FlagSet) error { +// Apply saves the flagSet for later usage calls, then calls +// the wrapped Int64Flag.Apply +func (f *Int64Flag) Apply(set *flag.FlagSet) error { f.set = set - return f.Int64Flag.ApplyWithError(set) + return f.Int64Flag.Apply(set) } // IntFlag is the flag type that wraps cli.IntFlag to allow @@ -150,18 +112,11 @@ func NewIntFlag(fl *cli.IntFlag) *IntFlag { return &IntFlag{IntFlag: fl, set: nil} } -// Apply saves the flagSet for later usage calls, then calls the -// wrapped IntFlag.Apply -func (f *IntFlag) Apply(set *flag.FlagSet) { +// Apply saves the flagSet for later usage calls, then calls +// the wrapped IntFlag.Apply +func (f *IntFlag) Apply(set *flag.FlagSet) error { f.set = set - f.IntFlag.Apply(set) -} - -// ApplyWithError saves the flagSet for later usage calls, then calls the -// wrapped IntFlag.ApplyWithError -func (f *IntFlag) ApplyWithError(set *flag.FlagSet) error { - f.set = set - return f.IntFlag.ApplyWithError(set) + return f.IntFlag.Apply(set) } // IntSliceFlag is the flag type that wraps cli.IntSliceFlag to allow @@ -176,18 +131,11 @@ func NewIntSliceFlag(fl *cli.IntSliceFlag) *IntSliceFlag { return &IntSliceFlag{IntSliceFlag: fl, set: nil} } -// Apply saves the flagSet for later usage calls, then calls the -// wrapped IntSliceFlag.Apply -func (f *IntSliceFlag) Apply(set *flag.FlagSet) { - f.set = set - f.IntSliceFlag.Apply(set) -} - -// ApplyWithError saves the flagSet for later usage calls, then calls the -// wrapped IntSliceFlag.ApplyWithError -func (f *IntSliceFlag) ApplyWithError(set *flag.FlagSet) error { +// Apply saves the flagSet for later usage calls, then calls +// the wrapped IntSliceFlag.Apply +func (f *IntSliceFlag) Apply(set *flag.FlagSet) error { f.set = set - return f.IntSliceFlag.ApplyWithError(set) + return f.IntSliceFlag.Apply(set) } // Int64SliceFlag is the flag type that wraps cli.Int64SliceFlag to allow @@ -202,18 +150,11 @@ func NewInt64SliceFlag(fl *cli.Int64SliceFlag) *Int64SliceFlag { return &Int64SliceFlag{Int64SliceFlag: fl, set: nil} } -// Apply saves the flagSet for later usage calls, then calls the -// wrapped Int64SliceFlag.Apply -func (f *Int64SliceFlag) Apply(set *flag.FlagSet) { +// Apply saves the flagSet for later usage calls, then calls +// the wrapped Int64SliceFlag.Apply +func (f *Int64SliceFlag) Apply(set *flag.FlagSet) error { f.set = set - f.Int64SliceFlag.Apply(set) -} - -// ApplyWithError saves the flagSet for later usage calls, then calls the -// wrapped Int64SliceFlag.ApplyWithError -func (f *Int64SliceFlag) ApplyWithError(set *flag.FlagSet) error { - f.set = set - return f.Int64SliceFlag.ApplyWithError(set) + return f.Int64SliceFlag.Apply(set) } // Float64SliceFlag is the flag type that wraps cli.Float64SliceFlag to allow @@ -230,16 +171,9 @@ func NewFloat64SliceFlag(fl *cli.Float64SliceFlag) *Float64SliceFlag { // Apply saves the flagSet for later usage calls, then calls the // wrapped Float64SliceFlag.Apply -func (f *Float64SliceFlag) Apply(set *flag.FlagSet) { - f.set = set - f.Float64SliceFlag.Apply(set) -} - -// ApplyWithError saves the flagSet for later usage calls, then calls the -// wrapped Float64SliceFlag.ApplyWithError -func (f *Float64SliceFlag) ApplyWithError(set *flag.FlagSet) error { +func (f *Float64SliceFlag) Apply(set *flag.FlagSet) error { f.set = set - return f.Float64SliceFlag.ApplyWithError(set) + return f.Float64SliceFlag.Apply(set) } // StringFlag is the flag type that wraps cli.StringFlag to allow @@ -254,18 +188,11 @@ func NewStringFlag(fl *cli.StringFlag) *StringFlag { return &StringFlag{StringFlag: fl, set: nil} } -// Apply saves the flagSet for later usage calls, then calls the -// wrapped StringFlag.Apply -func (f *StringFlag) Apply(set *flag.FlagSet) { +// Apply saves the flagSet for later usage calls, then calls +// the wrapped StringFlag.Apply +func (f *StringFlag) Apply(set *flag.FlagSet) error { f.set = set - f.StringFlag.Apply(set) -} - -// ApplyWithError saves the flagSet for later usage calls, then calls the -// wrapped StringFlag.ApplyWithError -func (f *StringFlag) ApplyWithError(set *flag.FlagSet) error { - f.set = set - return f.StringFlag.ApplyWithError(set) + return f.StringFlag.Apply(set) } // PathFlag is the flag type that wraps cli.PathFlag to allow @@ -282,16 +209,9 @@ func NewPathFlag(fl *cli.PathFlag) *PathFlag { // Apply saves the flagSet for later usage calls, then calls the // wrapped PathFlag.Apply -func (f *PathFlag) Apply(set *flag.FlagSet) { - f.set = set - f.PathFlag.Apply(set) -} - -// ApplyWithError saves the flagSet for later usage calls, then calls the -// wrapped PathFlag.ApplyWithError -func (f *PathFlag) ApplyWithError(set *flag.FlagSet) error { +func (f *PathFlag) Apply(set *flag.FlagSet) error { f.set = set - return f.PathFlag.ApplyWithError(set) + return f.PathFlag.Apply(set) } // StringSliceFlag is the flag type that wraps cli.StringSliceFlag to allow @@ -306,18 +226,11 @@ func NewStringSliceFlag(fl *cli.StringSliceFlag) *StringSliceFlag { return &StringSliceFlag{StringSliceFlag: fl, set: nil} } -// Apply saves the flagSet for later usage calls, then calls the -// wrapped StringSliceFlag.Apply -func (f *StringSliceFlag) Apply(set *flag.FlagSet) { +// Apply saves the flagSet for later usage calls, then calls +// the wrapped StringSliceFlag.Apply +func (f *StringSliceFlag) Apply(set *flag.FlagSet) error { f.set = set - f.StringSliceFlag.Apply(set) -} - -// ApplyWithError saves the flagSet for later usage calls, then calls the -// wrapped StringSliceFlag.ApplyWithError -func (f *StringSliceFlag) ApplyWithError(set *flag.FlagSet) error { - f.set = set - return f.StringSliceFlag.ApplyWithError(set) + return f.StringSliceFlag.Apply(set) } // Uint64Flag is the flag type that wraps cli.Uint64Flag to allow @@ -332,18 +245,11 @@ func NewUint64Flag(fl *cli.Uint64Flag) *Uint64Flag { return &Uint64Flag{Uint64Flag: fl, set: nil} } -// Apply saves the flagSet for later usage calls, then calls the -// wrapped Uint64Flag.Apply -func (f *Uint64Flag) Apply(set *flag.FlagSet) { - f.set = set - f.Uint64Flag.Apply(set) -} - -// ApplyWithError saves the flagSet for later usage calls, then calls the -// wrapped Uint64Flag.ApplyWithError -func (f *Uint64Flag) ApplyWithError(set *flag.FlagSet) error { +// Apply saves the flagSet for later usage calls, then calls +// the wrapped Uint64Flag.Apply +func (f *Uint64Flag) Apply(set *flag.FlagSet) error { f.set = set - return f.Uint64Flag.ApplyWithError(set) + return f.Uint64Flag.Apply(set) } // UintFlag is the flag type that wraps cli.UintFlag to allow @@ -358,16 +264,9 @@ func NewUintFlag(fl *cli.UintFlag) *UintFlag { return &UintFlag{UintFlag: fl, set: nil} } -// Apply saves the flagSet for later usage calls, then calls the -// wrapped UintFlag.Apply -func (f *UintFlag) Apply(set *flag.FlagSet) { - f.set = set - f.UintFlag.Apply(set) -} - -// ApplyWithError saves the flagSet for later usage calls, then calls the -// wrapped UintFlag.ApplyWithError -func (f *UintFlag) ApplyWithError(set *flag.FlagSet) error { +// Apply saves the flagSet for later usage calls, then calls +// the wrapped UintFlag.Apply +func (f *UintFlag) Apply(set *flag.FlagSet) error { f.set = set - return f.UintFlag.ApplyWithError(set) + return f.UintFlag.Apply(set) } diff --git a/flag_test.go b/flag_test.go index 087e607..b5fcbf5 100644 --- a/flag_test.go +++ b/flag_test.go @@ -3,13 +3,12 @@ package altsrc import ( "flag" "fmt" + "github.com/urfave/cli/v2" "os" "runtime" "strings" "testing" "time" - - "gopkg.in/urfave/cli.v2" ) type testApplyInputSource struct { @@ -252,30 +251,30 @@ func TestDurationApplyInputSourceMethodSet(t *testing.T) { c := runTest(t, testApplyInputSource{ Flag: NewDurationFlag(&cli.DurationFlag{Name: "test"}), FlagName: "test", - MapValue: time.Duration(30 * time.Second), + MapValue: 30 * time.Second, }) - expect(t, time.Duration(30*time.Second), c.Duration("test")) + expect(t, 30*time.Second, c.Duration("test")) } func TestDurationApplyInputSourceMethodContextSet(t *testing.T) { c := runTest(t, testApplyInputSource{ Flag: NewDurationFlag(&cli.DurationFlag{Name: "test"}), FlagName: "test", - MapValue: time.Duration(30 * time.Second), - ContextValueString: time.Duration(15 * time.Second).String(), + MapValue: 30 * time.Second, + ContextValueString: (15 * time.Second).String(), }) - expect(t, time.Duration(15*time.Second), c.Duration("test")) + expect(t, 15*time.Second, c.Duration("test")) } func TestDurationApplyInputSourceMethodEnvVarSet(t *testing.T) { c := runTest(t, testApplyInputSource{ Flag: NewDurationFlag(&cli.DurationFlag{Name: "test", EnvVars: []string{"TEST"}}), FlagName: "test", - MapValue: time.Duration(30 * time.Second), + MapValue: 30 * time.Second, EnvVarName: "TEST", - EnvVarValue: time.Duration(15 * time.Second).String(), + EnvVarValue: (15 * time.Second).String(), }) - expect(t, time.Duration(15*time.Second), c.Duration("test")) + expect(t, 15*time.Second, c.Duration("test")) } func TestFloat64ApplyInputSourceMethodSet(t *testing.T) { @@ -316,19 +315,19 @@ func runTest(t *testing.T, test testApplyInputSource) *cli.Context { set := flag.NewFlagSet(test.FlagSetName, flag.ContinueOnError) c := cli.NewContext(nil, set, nil) if test.EnvVarName != "" && test.EnvVarValue != "" { - os.Setenv(test.EnvVarName, test.EnvVarValue) + _ = os.Setenv(test.EnvVarName, test.EnvVarValue) defer os.Setenv(test.EnvVarName, "") } test.Flag.Apply(set) if test.ContextValue != nil { - flag := set.Lookup(test.FlagName) - flag.Value = test.ContextValue + f := set.Lookup(test.FlagName) + f.Value = test.ContextValue } if test.ContextValueString != "" { - set.Set(test.FlagName, test.ContextValueString) + _ = set.Set(test.FlagName, test.ContextValueString) } - test.Flag.ApplyInputSourceValue(c, inputSource) + _ = test.Flag.ApplyInputSourceValue(c, inputSource) return c } diff --git a/input_source_context.go b/input_source_context.go index bb0afdb..5f60574 100644 --- a/input_source_context.go +++ b/input_source_context.go @@ -1,9 +1,8 @@ package altsrc import ( + "github.com/urfave/cli/v2" "time" - - "gopkg.in/urfave/cli.v2" ) // InputSourceContext is an interface used to allow diff --git a/json_command_test.go b/json_command_test.go index ccb5041..bd8e6cf 100644 --- a/json_command_test.go +++ b/json_command_test.go @@ -2,11 +2,10 @@ package altsrc import ( "flag" + "github.com/urfave/cli/v2" "io/ioutil" "os" "testing" - - "gopkg.in/urfave/cli.v2" ) const ( @@ -22,7 +21,7 @@ func TestCommandJSONFileTest(t *testing.T) { app := &cli.App{} set := flag.NewFlagSet("test", 0) test := []string{"test-cmd", "--load", fileName} - set.Parse(test) + _ = set.Parse(test) c := cli.NewContext(app, set, nil) @@ -52,11 +51,11 @@ func TestCommandJSONFileTestGlobalEnvVarWins(t *testing.T) { app := &cli.App{} set := flag.NewFlagSet("test", 0) - os.Setenv("THE_TEST", "10") + _ = os.Setenv("THE_TEST", "10") defer os.Setenv("THE_TEST", "") test := []string{"test-cmd", "--load", fileName} - set.Parse(test) + _ = set.Parse(test) c := cli.NewContext(app, set, nil) @@ -87,11 +86,11 @@ func TestCommandJSONFileTestGlobalEnvVarWinsNested(t *testing.T) { app := &cli.App{} set := flag.NewFlagSet("test", 0) - os.Setenv("THE_TEST", "10") + _ = os.Setenv("THE_TEST", "10") defer os.Setenv("THE_TEST", "") test := []string{"test-cmd", "--load", fileName} - set.Parse(test) + _ = set.Parse(test) c := cli.NewContext(app, set, nil) @@ -123,7 +122,7 @@ func TestCommandJSONFileTestSpecifiedFlagWins(t *testing.T) { app := &cli.App{} set := flag.NewFlagSet("test", 0) test := []string{"test-cmd", "--load", fileName, "--test", "7"} - set.Parse(test) + _ = set.Parse(test) c := cli.NewContext(app, set, nil) @@ -155,7 +154,7 @@ func TestCommandJSONFileTestSpecifiedFlagWinsNested(t *testing.T) { app := &cli.App{} set := flag.NewFlagSet("test", 0) test := []string{"test-cmd", "--load", fileName, "--top.test", "7"} - set.Parse(test) + _ = set.Parse(test) c := cli.NewContext(app, set, nil) @@ -187,7 +186,7 @@ func TestCommandJSONFileTestDefaultValueFileWins(t *testing.T) { app := &cli.App{} set := flag.NewFlagSet("test", 0) test := []string{"test-cmd", "--load", fileName} - set.Parse(test) + _ = set.Parse(test) c := cli.NewContext(app, set, nil) @@ -219,7 +218,7 @@ func TestCommandJSONFileTestDefaultValueFileWinsNested(t *testing.T) { app := &cli.App{} set := flag.NewFlagSet("test", 0) test := []string{"test-cmd", "--load", fileName} - set.Parse(test) + _ = set.Parse(test) c := cli.NewContext(app, set, nil) @@ -250,11 +249,11 @@ func TestCommandJSONFileFlagHasDefaultGlobalEnvJSONSetGlobalEnvWins(t *testing.T app := &cli.App{} set := flag.NewFlagSet("test", 0) - os.Setenv("THE_TEST", "11") + _ = os.Setenv("THE_TEST", "11") defer os.Setenv("THE_TEST", "") test := []string{"test-cmd", "--load", fileName} - set.Parse(test) + _ = set.Parse(test) c := cli.NewContext(app, set, nil) @@ -284,11 +283,11 @@ func TestCommandJSONFileFlagHasDefaultGlobalEnvJSONSetGlobalEnvWinsNested(t *tes app := &cli.App{} set := flag.NewFlagSet("test", 0) - os.Setenv("THE_TEST", "11") + _ = os.Setenv("THE_TEST", "11") defer os.Setenv("THE_TEST", "") test := []string{"test-cmd", "--load", fileName} - set.Parse(test) + _ = set.Parse(test) c := cli.NewContext(app, set, nil) diff --git a/json_source_context.go b/json_source_context.go index 34c7eb9..41602ad 100644 --- a/json_source_context.go +++ b/json_source_context.go @@ -3,12 +3,11 @@ package altsrc import ( "encoding/json" "fmt" + "github.com/urfave/cli/v2" "io" "io/ioutil" "strings" "time" - - "gopkg.in/urfave/cli.v2" ) // NewJSONSourceFromFlagFunc returns a func that takes a cli.Context @@ -29,13 +28,8 @@ func NewJSONSourceFromFile(f string) (InputSourceContext, error) { if err != nil { return nil, err } - s, err := newJSONSource(data) - if err != nil { - return nil, err - } - s.file = f - return s, nil + return NewJSONSource(data) } // NewJSONSourceFromReader returns an InputSourceContext suitable for @@ -51,10 +45,6 @@ func NewJSONSourceFromReader(r io.Reader) (InputSourceContext, error) { // NewJSONSource returns an InputSourceContext suitable for retrieving // config variables from raw JSON data. func NewJSONSource(data []byte) (InputSourceContext, error) { - return newJSONSource(data) -} - -func newJSONSource(data []byte) (*jsonSource, error) { var deserialized map[string]interface{} if err := json.Unmarshal(data, &deserialized); err != nil { return nil, err @@ -77,9 +67,9 @@ func (x *jsonSource) Int(name string) (int, error) { case int: return v, nil case float64: - return int(float64(v)), nil + return int(v), nil case float32: - return int(float32(v)), nil + return int(v), nil } } diff --git a/map_input_source.go b/map_input_source.go index 41221b1..66bd6e8 100644 --- a/map_input_source.go +++ b/map_input_source.go @@ -2,11 +2,10 @@ package altsrc import ( "fmt" + "github.com/urfave/cli/v2" "reflect" "strings" "time" - - "gopkg.in/urfave/cli.v2" ) // MapInputSource implements InputSourceContext to return @@ -23,15 +22,15 @@ func nestedVal(name string, tree map[interface{}]interface{}) (interface{}, bool if sections := strings.Split(name, "."); len(sections) > 1 { node := tree for _, section := range sections[:len(sections)-1] { - if child, ok := node[section]; !ok { + child, ok := node[section] + if !ok { + return nil, false + } + ctype, ok := child.(map[interface{}]interface{}) + if !ok { return nil, false - } else { - if ctype, ok := child.(map[interface{}]interface{}); !ok { - return nil, false - } else { - node = ctype - } } + node = ctype } if val, ok := node[sections[len(sections)-1]]; ok { return val, true diff --git a/toml_command_test.go b/toml_command_test.go index 2f28d22..84558aa 100644 --- a/toml_command_test.go +++ b/toml_command_test.go @@ -7,20 +7,19 @@ package altsrc import ( "flag" + "github.com/urfave/cli/v2" "io/ioutil" "os" "testing" - - "gopkg.in/urfave/cli.v2" ) func TestCommandTomFileTest(t *testing.T) { - app := (&cli.App{}) + app := &cli.App{} set := flag.NewFlagSet("test", 0) - ioutil.WriteFile("current.toml", []byte("test = 15"), 0666) + _ = ioutil.WriteFile("current.toml", []byte("test = 15"), 0666) defer os.Remove("current.toml") test := []string{"test-cmd", "--load", "current.toml"} - set.Parse(test) + _ = set.Parse(test) c := cli.NewContext(app, set, nil) @@ -45,15 +44,15 @@ func TestCommandTomFileTest(t *testing.T) { } func TestCommandTomlFileTestGlobalEnvVarWins(t *testing.T) { - app := (&cli.App{}) + app := &cli.App{} set := flag.NewFlagSet("test", 0) - ioutil.WriteFile("current.toml", []byte("test = 15"), 0666) + _ = ioutil.WriteFile("current.toml", []byte("test = 15"), 0666) defer os.Remove("current.toml") - os.Setenv("THE_TEST", "10") + _ = os.Setenv("THE_TEST", "10") defer os.Setenv("THE_TEST", "") test := []string{"test-cmd", "--load", "current.toml"} - set.Parse(test) + _ = set.Parse(test) c := cli.NewContext(app, set, nil) @@ -79,15 +78,15 @@ func TestCommandTomlFileTestGlobalEnvVarWins(t *testing.T) { } func TestCommandTomlFileTestGlobalEnvVarWinsNested(t *testing.T) { - app := (&cli.App{}) + app := &cli.App{} set := flag.NewFlagSet("test", 0) - ioutil.WriteFile("current.toml", []byte("[top]\ntest = 15"), 0666) + _ = ioutil.WriteFile("current.toml", []byte("[top]\ntest = 15"), 0666) defer os.Remove("current.toml") - os.Setenv("THE_TEST", "10") + _ = os.Setenv("THE_TEST", "10") defer os.Setenv("THE_TEST", "") test := []string{"test-cmd", "--load", "current.toml"} - set.Parse(test) + _ = set.Parse(test) c := cli.NewContext(app, set, nil) @@ -113,13 +112,13 @@ func TestCommandTomlFileTestGlobalEnvVarWinsNested(t *testing.T) { } func TestCommandTomlFileTestSpecifiedFlagWins(t *testing.T) { - app := (&cli.App{}) + app := &cli.App{} set := flag.NewFlagSet("test", 0) - ioutil.WriteFile("current.toml", []byte("test = 15"), 0666) + _ = ioutil.WriteFile("current.toml", []byte("test = 15"), 0666) defer os.Remove("current.toml") test := []string{"test-cmd", "--load", "current.toml", "--test", "7"} - set.Parse(test) + _ = set.Parse(test) c := cli.NewContext(app, set, nil) @@ -145,14 +144,14 @@ func TestCommandTomlFileTestSpecifiedFlagWins(t *testing.T) { } func TestCommandTomlFileTestSpecifiedFlagWinsNested(t *testing.T) { - app := (&cli.App{}) + app := &cli.App{} set := flag.NewFlagSet("test", 0) - ioutil.WriteFile("current.toml", []byte(`[top] + _ = ioutil.WriteFile("current.toml", []byte(`[top] test = 15`), 0666) defer os.Remove("current.toml") test := []string{"test-cmd", "--load", "current.toml", "--top.test", "7"} - set.Parse(test) + _ = set.Parse(test) c := cli.NewContext(app, set, nil) @@ -178,13 +177,13 @@ func TestCommandTomlFileTestSpecifiedFlagWinsNested(t *testing.T) { } func TestCommandTomlFileTestDefaultValueFileWins(t *testing.T) { - app := (&cli.App{}) + app := &cli.App{} set := flag.NewFlagSet("test", 0) - ioutil.WriteFile("current.toml", []byte("test = 15"), 0666) + _ = ioutil.WriteFile("current.toml", []byte("test = 15"), 0666) defer os.Remove("current.toml") test := []string{"test-cmd", "--load", "current.toml"} - set.Parse(test) + _ = set.Parse(test) c := cli.NewContext(app, set, nil) @@ -210,13 +209,13 @@ func TestCommandTomlFileTestDefaultValueFileWins(t *testing.T) { } func TestCommandTomlFileTestDefaultValueFileWinsNested(t *testing.T) { - app := (&cli.App{}) + app := &cli.App{} set := flag.NewFlagSet("test", 0) - ioutil.WriteFile("current.toml", []byte("[top]\ntest = 15"), 0666) + _ = ioutil.WriteFile("current.toml", []byte("[top]\ntest = 15"), 0666) defer os.Remove("current.toml") test := []string{"test-cmd", "--load", "current.toml"} - set.Parse(test) + _ = set.Parse(test) c := cli.NewContext(app, set, nil) @@ -244,14 +243,14 @@ func TestCommandTomlFileTestDefaultValueFileWinsNested(t *testing.T) { func TestCommandTomlFileFlagHasDefaultGlobalEnvTomlSetGlobalEnvWins(t *testing.T) { app := (&cli.App{}) set := flag.NewFlagSet("test", 0) - ioutil.WriteFile("current.toml", []byte("test = 15"), 0666) + _ = ioutil.WriteFile("current.toml", []byte("test = 15"), 0666) defer os.Remove("current.toml") - os.Setenv("THE_TEST", "11") + _ = os.Setenv("THE_TEST", "11") defer os.Setenv("THE_TEST", "") test := []string{"test-cmd", "--load", "current.toml"} - set.Parse(test) + _ = set.Parse(test) c := cli.NewContext(app, set, nil) @@ -276,16 +275,16 @@ func TestCommandTomlFileFlagHasDefaultGlobalEnvTomlSetGlobalEnvWins(t *testing.T } func TestCommandTomlFileFlagHasDefaultGlobalEnvTomlSetGlobalEnvWinsNested(t *testing.T) { - app := (&cli.App{}) + app := &cli.App{} set := flag.NewFlagSet("test", 0) - ioutil.WriteFile("current.toml", []byte("[top]\ntest = 15"), 0666) + _ = ioutil.WriteFile("current.toml", []byte("[top]\ntest = 15"), 0666) defer os.Remove("current.toml") - os.Setenv("THE_TEST", "11") + _ = os.Setenv("THE_TEST", "11") defer os.Setenv("THE_TEST", "") test := []string{"test-cmd", "--load", "current.toml"} - set.Parse(test) + _ = set.Parse(test) c := cli.NewContext(app, set, nil) diff --git a/toml_file_loader.go b/toml_file_loader.go index 1cb2d7b..3f410fc 100644 --- a/toml_file_loader.go +++ b/toml_file_loader.go @@ -10,7 +10,7 @@ import ( "reflect" "github.com/BurntSushi/toml" - "gopkg.in/urfave/cli.v2" + "github.com/urfave/cli/v2" ) type tomlMap struct { @@ -28,7 +28,7 @@ func unmarshalMap(i interface{}) (ret map[interface{}]interface{}, err error) { case reflect.String: ret[key] = val.(string) case reflect.Int: - ret[key] = int(val.(int)) + ret[key] = val.(int) case reflect.Int8: ret[key] = int(val.(int8)) case reflect.Int16: @@ -50,7 +50,7 @@ func unmarshalMap(i interface{}) (ret map[interface{}]interface{}, err error) { case reflect.Float32: ret[key] = float64(val.(float32)) case reflect.Float64: - ret[key] = float64(val.(float64)) + ret[key] = val.(float64) case reflect.Map: if tmp, err := unmarshalMap(val); err == nil { ret[key] = tmp @@ -66,9 +66,9 @@ func unmarshalMap(i interface{}) (ret map[interface{}]interface{}, err error) { return ret, nil } -func (self *tomlMap) UnmarshalTOML(i interface{}) error { +func (tm *tomlMap) UnmarshalTOML(i interface{}) error { if tmp, err := unmarshalMap(i); err == nil { - self.Map = tmp + tm.Map = tmp } else { return err } diff --git a/yaml_command_test.go b/yaml_command_test.go index 5290e84..2190c2b 100644 --- a/yaml_command_test.go +++ b/yaml_command_test.go @@ -10,17 +10,16 @@ import ( "io/ioutil" "os" "testing" - - "gopkg.in/urfave/cli.v2" + "github.com/urfave/cli/v2" ) func TestCommandYamlFileTest(t *testing.T) { app := &cli.App{} set := flag.NewFlagSet("test", 0) - ioutil.WriteFile("current.yaml", []byte("test: 15"), 0666) + _ = ioutil.WriteFile("current.yaml", []byte("test: 15"), 0666) defer os.Remove("current.yaml") test := []string{"test-cmd", "--load", "current.yaml"} - set.Parse(test) + _ = set.Parse(test) c := cli.NewContext(app, set, nil) @@ -47,13 +46,13 @@ func TestCommandYamlFileTest(t *testing.T) { func TestCommandYamlFileTestGlobalEnvVarWins(t *testing.T) { app := &cli.App{} set := flag.NewFlagSet("test", 0) - ioutil.WriteFile("current.yaml", []byte("test: 15"), 0666) + _ = ioutil.WriteFile("current.yaml", []byte("test: 15"), 0666) defer os.Remove("current.yaml") - os.Setenv("THE_TEST", "10") + _ = os.Setenv("THE_TEST", "10") defer os.Setenv("THE_TEST", "") test := []string{"test-cmd", "--load", "current.yaml"} - set.Parse(test) + _ = set.Parse(test) c := cli.NewContext(app, set, nil) @@ -81,14 +80,14 @@ func TestCommandYamlFileTestGlobalEnvVarWins(t *testing.T) { func TestCommandYamlFileTestGlobalEnvVarWinsNested(t *testing.T) { app := &cli.App{} set := flag.NewFlagSet("test", 0) - ioutil.WriteFile("current.yaml", []byte(`top: + _ = ioutil.WriteFile("current.yaml", []byte(`top: test: 15`), 0666) defer os.Remove("current.yaml") - os.Setenv("THE_TEST", "10") + _ = os.Setenv("THE_TEST", "10") defer os.Setenv("THE_TEST", "") test := []string{"test-cmd", "--load", "current.yaml"} - set.Parse(test) + _ = set.Parse(test) c := cli.NewContext(app, set, nil) @@ -116,11 +115,11 @@ func TestCommandYamlFileTestGlobalEnvVarWinsNested(t *testing.T) { func TestCommandYamlFileTestSpecifiedFlagWins(t *testing.T) { app := &cli.App{} set := flag.NewFlagSet("test", 0) - ioutil.WriteFile("current.yaml", []byte("test: 15"), 0666) + _ = ioutil.WriteFile("current.yaml", []byte("test: 15"), 0666) defer os.Remove("current.yaml") test := []string{"test-cmd", "--load", "current.yaml", "--test", "7"} - set.Parse(test) + _ = set.Parse(test) c := cli.NewContext(app, set, nil) @@ -148,12 +147,12 @@ func TestCommandYamlFileTestSpecifiedFlagWins(t *testing.T) { func TestCommandYamlFileTestSpecifiedFlagWinsNested(t *testing.T) { app := &cli.App{} set := flag.NewFlagSet("test", 0) - ioutil.WriteFile("current.yaml", []byte(`top: + _ = ioutil.WriteFile("current.yaml", []byte(`top: test: 15`), 0666) defer os.Remove("current.yaml") test := []string{"test-cmd", "--load", "current.yaml", "--top.test", "7"} - set.Parse(test) + _ = set.Parse(test) c := cli.NewContext(app, set, nil) @@ -181,11 +180,11 @@ func TestCommandYamlFileTestSpecifiedFlagWinsNested(t *testing.T) { func TestCommandYamlFileTestDefaultValueFileWins(t *testing.T) { app := &cli.App{} set := flag.NewFlagSet("test", 0) - ioutil.WriteFile("current.yaml", []byte("test: 15"), 0666) + _ = ioutil.WriteFile("current.yaml", []byte("test: 15"), 0666) defer os.Remove("current.yaml") test := []string{"test-cmd", "--load", "current.yaml"} - set.Parse(test) + _ = set.Parse(test) c := cli.NewContext(app, set, nil) @@ -213,12 +212,12 @@ func TestCommandYamlFileTestDefaultValueFileWins(t *testing.T) { func TestCommandYamlFileTestDefaultValueFileWinsNested(t *testing.T) { app := &cli.App{} set := flag.NewFlagSet("test", 0) - ioutil.WriteFile("current.yaml", []byte(`top: + _ = ioutil.WriteFile("current.yaml", []byte(`top: test: 15`), 0666) defer os.Remove("current.yaml") test := []string{"test-cmd", "--load", "current.yaml"} - set.Parse(test) + _ = set.Parse(test) c := cli.NewContext(app, set, nil) @@ -246,14 +245,14 @@ func TestCommandYamlFileTestDefaultValueFileWinsNested(t *testing.T) { func TestCommandYamlFileFlagHasDefaultGlobalEnvYamlSetGlobalEnvWins(t *testing.T) { app := &cli.App{} set := flag.NewFlagSet("test", 0) - ioutil.WriteFile("current.yaml", []byte("test: 15"), 0666) + _ = ioutil.WriteFile("current.yaml", []byte("test: 15"), 0666) defer os.Remove("current.yaml") - os.Setenv("THE_TEST", "11") + _ = os.Setenv("THE_TEST", "11") defer os.Setenv("THE_TEST", "") test := []string{"test-cmd", "--load", "current.yaml"} - set.Parse(test) + _ = set.Parse(test) c := cli.NewContext(app, set, nil) @@ -280,15 +279,15 @@ func TestCommandYamlFileFlagHasDefaultGlobalEnvYamlSetGlobalEnvWins(t *testing.T func TestCommandYamlFileFlagHasDefaultGlobalEnvYamlSetGlobalEnvWinsNested(t *testing.T) { app := &cli.App{} set := flag.NewFlagSet("test", 0) - ioutil.WriteFile("current.yaml", []byte(`top: + _ = ioutil.WriteFile("current.yaml", []byte(`top: test: 15`), 0666) defer os.Remove("current.yaml") - os.Setenv("THE_TEST", "11") + _ = os.Setenv("THE_TEST", "11") defer os.Setenv("THE_TEST", "") test := []string{"test-cmd", "--load", "current.yaml"} - set.Parse(test) + _ = set.Parse(test) c := cli.NewContext(app, set, nil) diff --git a/yaml_file_loader.go b/yaml_file_loader.go index 37c8d9c..9b1dc05 100644 --- a/yaml_file_loader.go +++ b/yaml_file_loader.go @@ -14,7 +14,7 @@ import ( "runtime" "strings" - "gopkg.in/urfave/cli.v2" + "github.com/urfave/cli/v2" "gopkg.in/yaml.v2" ) @@ -86,7 +86,7 @@ func loadDataFrom(filePath string) ([]byte, error) { return nil, fmt.Errorf("Cannot read from file: '%s' because it does not exist.", filePath) } return ioutil.ReadFile(filePath) - } else { - return nil, fmt.Errorf("unable to determine how to load from path %s", filePath) } + + return nil, fmt.Errorf("unable to determine how to load from path %s", filePath) }