From dc5f4fcba0f0ea0580ace5690da11a13815decce Mon Sep 17 00:00:00 2001 From: Igor Postelnik Date: Fri, 12 Feb 2021 09:48:06 -0600 Subject: [PATCH] Added script to generate altsrc flag definitions, restricted list of types to the ones that provide implementation of FlagInputSourceExtension. The original program to generate altsrc flags is nowhere to be found, so replaced with fg.py. The types of altsrc flags are now limited to the subset that provide FlagInputSourceExtension implementation and have matching methods in InputSourceContext interface. Removed types where never updated from the input source. --- altsrc/fg.py | 45 ++++++++++++++ altsrc/flag_generated.go | 124 ++++++--------------------------------- 2 files changed, 64 insertions(+), 105 deletions(-) create mode 100644 altsrc/fg.py diff --git a/altsrc/fg.py b/altsrc/fg.py new file mode 100644 index 0000000000..f0d150f1be --- /dev/null +++ b/altsrc/fg.py @@ -0,0 +1,45 @@ +# Only types that provide implementation of FlagInputSourceExtension can be listed here +# please keep list sorted alphabetically +flag_types = [ + "Bool", + "Duration", + "Float64", + "Generic", + "Int", + "IntSlice", + "Path", + "String", + "StringSlice", +] + +print('''// Code generated by fg.py; DO NOT EDIT. + +package altsrc + +import ( + "flag" + + "github.com/urfave/cli/v2" +)''') + +for t in flag_types: + print(f''' +// {t}Flag is the flag type that wraps cli.{t}Flag to allow +// for other values to be specified +type {t}Flag struct {{ + *cli.{t}Flag + set *flag.FlagSet +}} +var _ FlagInputSourceExtension = (*{t}Flag)(nil) + +// New{t}Flag creates a new {t}Flag +func New{t}Flag(fl *cli.{t}Flag) *{t}Flag {{ + return &{t}Flag{{{t}Flag: fl, set: nil}} +}} + +// Apply saves the flagSet for later usage calls, then calls +// the wrapped {t}Flag.Apply +func (f *{t}Flag) Apply(set *flag.FlagSet) error {{ + f.set = set + return f.{t}Flag.Apply(set) +}}''') \ No newline at end of file diff --git a/altsrc/flag_generated.go b/altsrc/flag_generated.go index c9ecea82e9..bdaf5a5ff0 100644 --- a/altsrc/flag_generated.go +++ b/altsrc/flag_generated.go @@ -1,4 +1,4 @@ -// Code generated by fg; DO NOT EDIT. +// Code generated by fg.py; DO NOT EDIT. package altsrc @@ -14,6 +14,7 @@ type BoolFlag struct { *cli.BoolFlag set *flag.FlagSet } +var _ FlagInputSourceExtension = (*BoolFlag)(nil) // NewBoolFlag creates a new BoolFlag func NewBoolFlag(fl *cli.BoolFlag) *BoolFlag { @@ -33,6 +34,7 @@ type DurationFlag struct { *cli.DurationFlag set *flag.FlagSet } +var _ FlagInputSourceExtension = (*DurationFlag)(nil) // NewDurationFlag creates a new DurationFlag func NewDurationFlag(fl *cli.DurationFlag) *DurationFlag { @@ -52,6 +54,7 @@ type Float64Flag struct { *cli.Float64Flag set *flag.FlagSet } +var _ FlagInputSourceExtension = (*Float64Flag)(nil) // NewFloat64Flag creates a new Float64Flag func NewFloat64Flag(fl *cli.Float64Flag) *Float64Flag { @@ -71,6 +74,7 @@ type GenericFlag struct { *cli.GenericFlag set *flag.FlagSet } +var _ FlagInputSourceExtension = (*GenericFlag)(nil) // NewGenericFlag creates a new GenericFlag func NewGenericFlag(fl *cli.GenericFlag) *GenericFlag { @@ -84,31 +88,13 @@ func (f *GenericFlag) Apply(set *flag.FlagSet) error { return f.GenericFlag.Apply(set) } -// Int64Flag is the flag type that wraps cli.Int64Flag to allow -// for other values to be specified -type Int64Flag struct { - *cli.Int64Flag - set *flag.FlagSet -} - -// NewInt64Flag creates a new Int64Flag -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) error { - f.set = set - return f.Int64Flag.Apply(set) -} - // IntFlag is the flag type that wraps cli.IntFlag to allow // for other values to be specified type IntFlag struct { *cli.IntFlag set *flag.FlagSet } +var _ FlagInputSourceExtension = (*IntFlag)(nil) // NewIntFlag creates a new IntFlag func NewIntFlag(fl *cli.IntFlag) *IntFlag { @@ -128,6 +114,7 @@ type IntSliceFlag struct { *cli.IntSliceFlag set *flag.FlagSet } +var _ FlagInputSourceExtension = (*IntSliceFlag)(nil) // NewIntSliceFlag creates a new IntSliceFlag func NewIntSliceFlag(fl *cli.IntSliceFlag) *IntSliceFlag { @@ -141,42 +128,24 @@ func (f *IntSliceFlag) Apply(set *flag.FlagSet) error { return f.IntSliceFlag.Apply(set) } -// Int64SliceFlag is the flag type that wraps cli.Int64SliceFlag to allow +// PathFlag is the flag type that wraps cli.PathFlag to allow // for other values to be specified -type Int64SliceFlag struct { - *cli.Int64SliceFlag +type PathFlag struct { + *cli.PathFlag set *flag.FlagSet } +var _ FlagInputSourceExtension = (*PathFlag)(nil) -// NewInt64SliceFlag creates a new Int64SliceFlag -func NewInt64SliceFlag(fl *cli.Int64SliceFlag) *Int64SliceFlag { - return &Int64SliceFlag{Int64SliceFlag: fl, set: nil} +// NewPathFlag creates a new PathFlag +func NewPathFlag(fl *cli.PathFlag) *PathFlag { + return &PathFlag{PathFlag: fl, set: nil} } // 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 - return f.Int64SliceFlag.Apply(set) -} - -// Float64SliceFlag is the flag type that wraps cli.Float64SliceFlag to allow -// for other values to be specified -type Float64SliceFlag struct { - *cli.Float64SliceFlag - set *flag.FlagSet -} - -// NewFloat64SliceFlag creates a new Float64SliceFlag -func NewFloat64SliceFlag(fl *cli.Float64SliceFlag) *Float64SliceFlag { - return &Float64SliceFlag{Float64SliceFlag: fl, set: nil} -} - -// Apply saves the flagSet for later usage calls, then calls the -// wrapped Float64SliceFlag.Apply -func (f *Float64SliceFlag) Apply(set *flag.FlagSet) error { +// the wrapped PathFlag.Apply +func (f *PathFlag) Apply(set *flag.FlagSet) error { f.set = set - return f.Float64SliceFlag.Apply(set) + return f.PathFlag.Apply(set) } // StringFlag is the flag type that wraps cli.StringFlag to allow @@ -185,6 +154,7 @@ type StringFlag struct { *cli.StringFlag set *flag.FlagSet } +var _ FlagInputSourceExtension = (*StringFlag)(nil) // NewStringFlag creates a new StringFlag func NewStringFlag(fl *cli.StringFlag) *StringFlag { @@ -198,31 +168,13 @@ func (f *StringFlag) Apply(set *flag.FlagSet) error { return f.StringFlag.Apply(set) } -// PathFlag is the flag type that wraps cli.PathFlag to allow -// for other values to be specified -type PathFlag struct { - *cli.PathFlag - set *flag.FlagSet -} - -// NewPathFlag creates a new PathFlag -func NewPathFlag(fl *cli.PathFlag) *PathFlag { - return &PathFlag{PathFlag: fl, set: nil} -} - -// Apply saves the flagSet for later usage calls, then calls the -// wrapped PathFlag.Apply -func (f *PathFlag) Apply(set *flag.FlagSet) error { - f.set = set - return f.PathFlag.Apply(set) -} - // StringSliceFlag is the flag type that wraps cli.StringSliceFlag to allow // for other values to be specified type StringSliceFlag struct { *cli.StringSliceFlag set *flag.FlagSet } +var _ FlagInputSourceExtension = (*StringSliceFlag)(nil) // NewStringSliceFlag creates a new StringSliceFlag func NewStringSliceFlag(fl *cli.StringSliceFlag) *StringSliceFlag { @@ -235,41 +187,3 @@ func (f *StringSliceFlag) Apply(set *flag.FlagSet) error { f.set = set return f.StringSliceFlag.Apply(set) } - -// Uint64Flag is the flag type that wraps cli.Uint64Flag to allow -// for other values to be specified -type Uint64Flag struct { - *cli.Uint64Flag - set *flag.FlagSet -} - -// NewUint64Flag creates a new Uint64Flag -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) error { - f.set = set - return f.Uint64Flag.Apply(set) -} - -// UintFlag is the flag type that wraps cli.UintFlag to allow -// for other values to be specified -type UintFlag struct { - *cli.UintFlag - set *flag.FlagSet -} - -// NewUintFlag creates a new UintFlag -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) error { - f.set = set - return f.UintFlag.Apply(set) -}