From f8a7cfe246749733549d345606dc446b2056fe65 Mon Sep 17 00:00:00 2001 From: Naveen Gogineni Date: Sat, 29 Oct 2022 19:24:31 -0400 Subject: [PATCH 1/2] Chore: Add altsrc flag definition generation --- altsrc/flag-spec.yaml | 18 +++ altsrc/flag_generated.go | 152 +++++++++--------- cmd/urfave-cli-genflags/Makefile | 3 +- .../generated_altsrc.gotmpl | 34 ++++ cmd/urfave-cli-genflags/main.go | 46 ++++-- 5 files changed, 160 insertions(+), 93 deletions(-) create mode 100644 altsrc/flag-spec.yaml create mode 100644 cmd/urfave-cli-genflags/generated_altsrc.gotmpl diff --git a/altsrc/flag-spec.yaml b/altsrc/flag-spec.yaml new file mode 100644 index 0000000..8c7db6f --- /dev/null +++ b/altsrc/flag-spec.yaml @@ -0,0 +1,18 @@ +# NOTE: this file is used by the tool defined in +# ./cmd/urfave-cli-genflags/main.go which uses the +# `Spec` type that maps to this file structure. +flag_types: + Bool: + Duration: + Float64: + Generic: + Int64: + Int: + IntSlice: + Int64Slice: + Float64Slice: + String: + Path: + StringSlice: + Uint64: + Uint: \ No newline at end of file diff --git a/altsrc/flag_generated.go b/altsrc/flag_generated.go index c9ecea8..9490417 100644 --- a/altsrc/flag_generated.go +++ b/altsrc/flag_generated.go @@ -1,4 +1,4 @@ -// Code generated by fg; DO NOT EDIT. +// WARNING: this file is generated. DO NOT EDIT package altsrc @@ -65,42 +65,42 @@ func (f *Float64Flag) Apply(set *flag.FlagSet) error { return f.Float64Flag.Apply(set) } -// GenericFlag is the flag type that wraps cli.GenericFlag to allow +// Float64SliceFlag is the flag type that wraps cli.Float64SliceFlag to allow // for other values to be specified -type GenericFlag struct { - *cli.GenericFlag +type Float64SliceFlag struct { + *cli.Float64SliceFlag set *flag.FlagSet } -// NewGenericFlag creates a new GenericFlag -func NewGenericFlag(fl *cli.GenericFlag) *GenericFlag { - return &GenericFlag{GenericFlag: fl, set: nil} +// 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 GenericFlag.Apply -func (f *GenericFlag) Apply(set *flag.FlagSet) error { +// the wrapped Float64SliceFlag.Apply +func (f *Float64SliceFlag) Apply(set *flag.FlagSet) error { f.set = set - return f.GenericFlag.Apply(set) + return f.Float64SliceFlag.Apply(set) } -// Int64Flag is the flag type that wraps cli.Int64Flag to allow +// GenericFlag is the flag type that wraps cli.GenericFlag to allow // for other values to be specified -type Int64Flag struct { - *cli.Int64Flag +type GenericFlag struct { + *cli.GenericFlag set *flag.FlagSet } -// NewInt64Flag creates a new Int64Flag -func NewInt64Flag(fl *cli.Int64Flag) *Int64Flag { - return &Int64Flag{Int64Flag: fl, set: nil} +// NewGenericFlag creates a new GenericFlag +func NewGenericFlag(fl *cli.GenericFlag) *GenericFlag { + return &GenericFlag{GenericFlag: 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 { +// the wrapped GenericFlag.Apply +func (f *GenericFlag) Apply(set *flag.FlagSet) error { f.set = set - return f.Int64Flag.Apply(set) + return f.GenericFlag.Apply(set) } // IntFlag is the flag type that wraps cli.IntFlag to allow @@ -122,23 +122,23 @@ func (f *IntFlag) Apply(set *flag.FlagSet) error { return f.IntFlag.Apply(set) } -// IntSliceFlag is the flag type that wraps cli.IntSliceFlag to allow +// Int64Flag is the flag type that wraps cli.Int64Flag to allow // for other values to be specified -type IntSliceFlag struct { - *cli.IntSliceFlag +type Int64Flag struct { + *cli.Int64Flag set *flag.FlagSet } -// NewIntSliceFlag creates a new IntSliceFlag -func NewIntSliceFlag(fl *cli.IntSliceFlag) *IntSliceFlag { - return &IntSliceFlag{IntSliceFlag: fl, set: nil} +// 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 IntSliceFlag.Apply -func (f *IntSliceFlag) Apply(set *flag.FlagSet) error { +// the wrapped Int64Flag.Apply +func (f *Int64Flag) Apply(set *flag.FlagSet) error { f.set = set - return f.IntSliceFlag.Apply(set) + return f.Int64Flag.Apply(set) } // Int64SliceFlag is the flag type that wraps cli.Int64SliceFlag to allow @@ -160,61 +160,61 @@ func (f *Int64SliceFlag) Apply(set *flag.FlagSet) error { return f.Int64SliceFlag.Apply(set) } -// Float64SliceFlag is the flag type that wraps cli.Float64SliceFlag to allow +// IntSliceFlag is the flag type that wraps cli.IntSliceFlag to allow // for other values to be specified -type Float64SliceFlag struct { - *cli.Float64SliceFlag +type IntSliceFlag struct { + *cli.IntSliceFlag set *flag.FlagSet } -// NewFloat64SliceFlag creates a new Float64SliceFlag -func NewFloat64SliceFlag(fl *cli.Float64SliceFlag) *Float64SliceFlag { - return &Float64SliceFlag{Float64SliceFlag: fl, set: nil} +// NewIntSliceFlag creates a new IntSliceFlag +func NewIntSliceFlag(fl *cli.IntSliceFlag) *IntSliceFlag { + return &IntSliceFlag{IntSliceFlag: 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 { +// 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.Float64SliceFlag.Apply(set) + return f.IntSliceFlag.Apply(set) } -// StringFlag is the flag type that wraps cli.StringFlag to allow +// PathFlag is the flag type that wraps cli.PathFlag to allow // for other values to be specified -type StringFlag struct { - *cli.StringFlag +type PathFlag struct { + *cli.PathFlag set *flag.FlagSet } -// NewStringFlag creates a new StringFlag -func NewStringFlag(fl *cli.StringFlag) *StringFlag { - return &StringFlag{StringFlag: 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 StringFlag.Apply -func (f *StringFlag) Apply(set *flag.FlagSet) error { +// the wrapped PathFlag.Apply +func (f *PathFlag) Apply(set *flag.FlagSet) error { f.set = set - return f.StringFlag.Apply(set) + return f.PathFlag.Apply(set) } -// PathFlag is the flag type that wraps cli.PathFlag to allow +// StringFlag is the flag type that wraps cli.StringFlag to allow // for other values to be specified -type PathFlag struct { - *cli.PathFlag +type StringFlag struct { + *cli.StringFlag set *flag.FlagSet } -// NewPathFlag creates a new PathFlag -func NewPathFlag(fl *cli.PathFlag) *PathFlag { - return &PathFlag{PathFlag: fl, set: nil} +// NewStringFlag creates a new StringFlag +func NewStringFlag(fl *cli.StringFlag) *StringFlag { + return &StringFlag{StringFlag: 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 { +// 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 - return f.PathFlag.Apply(set) + return f.StringFlag.Apply(set) } // StringSliceFlag is the flag type that wraps cli.StringSliceFlag to allow @@ -236,40 +236,42 @@ func (f *StringSliceFlag) Apply(set *flag.FlagSet) error { return f.StringSliceFlag.Apply(set) } -// Uint64Flag is the flag type that wraps cli.Uint64Flag to allow +// UintFlag is the flag type that wraps cli.UintFlag to allow // for other values to be specified -type Uint64Flag struct { - *cli.Uint64Flag +type UintFlag struct { + *cli.UintFlag set *flag.FlagSet } -// NewUint64Flag creates a new Uint64Flag -func NewUint64Flag(fl *cli.Uint64Flag) *Uint64Flag { - return &Uint64Flag{Uint64Flag: fl, set: nil} +// 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 Uint64Flag.Apply -func (f *Uint64Flag) Apply(set *flag.FlagSet) error { +// the wrapped UintFlag.Apply +func (f *UintFlag) Apply(set *flag.FlagSet) error { f.set = set - return f.Uint64Flag.Apply(set) + return f.UintFlag.Apply(set) } -// UintFlag is the flag type that wraps cli.UintFlag to allow +// Uint64Flag is the flag type that wraps cli.Uint64Flag to allow // for other values to be specified -type UintFlag struct { - *cli.UintFlag +type Uint64Flag struct { + *cli.Uint64Flag set *flag.FlagSet } -// NewUintFlag creates a new UintFlag -func NewUintFlag(fl *cli.UintFlag) *UintFlag { - return &UintFlag{UintFlag: fl, set: nil} +// 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 UintFlag.Apply -func (f *UintFlag) Apply(set *flag.FlagSet) error { +// the wrapped Uint64Flag.Apply +func (f *Uint64Flag) Apply(set *flag.FlagSet) error { f.set = set - return f.UintFlag.Apply(set) + return f.Uint64Flag.Apply(set) } + +// vim:ro diff --git a/cmd/urfave-cli-genflags/Makefile b/cmd/urfave-cli-genflags/Makefile index acede8e..8f33ca2 100644 --- a/cmd/urfave-cli-genflags/Makefile +++ b/cmd/urfave-cli-genflags/Makefile @@ -25,4 +25,5 @@ show-cover: .PHONY: run run: build - ./urfave-cli-genflags + ./urfave-cli-genflags + ./urfave-cli-genflags -f altsrc/flag-spec.yaml -o altsrc/flag_generated.go -p altsrc -a diff --git a/cmd/urfave-cli-genflags/generated_altsrc.gotmpl b/cmd/urfave-cli-genflags/generated_altsrc.gotmpl new file mode 100644 index 0000000..737eb82 --- /dev/null +++ b/cmd/urfave-cli-genflags/generated_altsrc.gotmpl @@ -0,0 +1,34 @@ +// WARNING: this file is generated. DO NOT EDIT + +package {{.PackageName}} + +import ( + "flag" + "github.com/urfave/cli/v2" +) + +{{range .SortedFlagTypes}} +// {{.TypeName}} is the flag type that wraps cli.{{.TypeName}} to allow +// for other values to be specified +type {{.TypeName}} struct { + *cli.{{.TypeName}} + set *flag.FlagSet +} + +// New{{.TypeName}} creates a new {{.TypeName}} +func New{{.TypeName}}(fl *cli.{{.TypeName}}) *{{.TypeName}} { + return &{{.TypeName}}{{"{"}}{{.TypeName}}: fl, set: nil} +} + +// Apply saves the flagSet for later usage calls, then calls +// the wrapped {{.TypeName}}.Apply +func (f *{{.TypeName}}) Apply(set *flag.FlagSet) error { + f.set = set + return f.{{.TypeName}}.Apply(set) +} +{{end}}{{/* /range .SortedFlagTypes */}} + +// vim{{/* 👻 */}}:ro +{{/* +vim:filetype=gotexttmpl +*/}} diff --git a/cmd/urfave-cli-genflags/main.go b/cmd/urfave-cli-genflags/main.go index 54cc83e..2706614 100644 --- a/cmd/urfave-cli-genflags/main.go +++ b/cmd/urfave-cli-genflags/main.go @@ -32,6 +32,9 @@ var ( //go:embed generated_test.gotmpl TestTemplateString string + //go:embed generated_altsrc.gotmpl + AltsrcTemplateString string + titler = cases.Title(language.Und, cases.NoLower) ) @@ -58,6 +61,10 @@ func main() { Name: "genflags", Usage: "Generate flag types for urfave/cli", Flags: []cli.Flag{ + &cli.BoolFlag{ + Name: "altsrc", + Aliases: []string{"a"}, + }, &cli.PathFlag{ Name: "flag-spec-yaml", Aliases: []string{"f"}, @@ -148,12 +155,11 @@ func runGenFlags(cCtx *cli.Context) error { spec.UrfaveCLITestNamespace = "cli." } - genTmpl, err := template.New("gen").Parse(TemplateString) - if err != nil { - return err + templateString := TemplateString + if cCtx.IsSet("altsrc") { + templateString = AltsrcTemplateString } - - genTestTmpl, err := template.New("gen_test").Parse(TestTemplateString) + genTmpl, err := template.New("gen").Parse(templateString) if err != nil { return err } @@ -163,27 +169,33 @@ func runGenFlags(cCtx *cli.Context) error { return err } - genTestBuf := &bytes.Buffer{} - if err := genTestTmpl.Execute(genTestBuf, spec); err != nil { - return err - } - if err := os.WriteFile(cCtx.Path("generated-output"), genBuf.Bytes(), 0644); err != nil { return err } - if err := os.WriteFile(cCtx.Path("generated-test-output"), genTestBuf.Bytes(), 0644); err != nil { - return err - } - if _, err := sh(cCtx.Context, cCtx.Path("goimports"), "-w", cCtx.Path("generated-output")); err != nil { return err } - if _, err := sh(cCtx.Context, cCtx.Path("goimports"), "-w", cCtx.Path("generated-test-output")); err != nil { - return err - } + if !cCtx.IsSet("altsrc") { + genTestTmpl, err := template.New("gen_test").Parse(TestTemplateString) + if err != nil { + return err + } + + genTestBuf := &bytes.Buffer{} + if err := genTestTmpl.Execute(genTestBuf, spec); err != nil { + return err + } + + if err := os.WriteFile(cCtx.Path("generated-test-output"), genTestBuf.Bytes(), 0644); err != nil { + return err + } + if _, err := sh(cCtx.Context, cCtx.Path("goimports"), "-w", cCtx.Path("generated-test-output")); err != nil { + return err + } + } return nil } From 45dc37629c02aab24826c9c18e6aec973056939a Mon Sep 17 00:00:00 2001 From: Naveen Gogineni Date: Fri, 4 Nov 2022 08:36:32 -0400 Subject: [PATCH 2/2] Code review comment --- cmd/urfave-cli-genflags/main.go | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/cmd/urfave-cli-genflags/main.go b/cmd/urfave-cli-genflags/main.go index 2706614..240b597 100644 --- a/cmd/urfave-cli-genflags/main.go +++ b/cmd/urfave-cli-genflags/main.go @@ -177,25 +177,28 @@ func runGenFlags(cCtx *cli.Context) error { return err } - if !cCtx.IsSet("altsrc") { - genTestTmpl, err := template.New("gen_test").Parse(TestTemplateString) - if err != nil { - return err - } + if cCtx.IsSet("altsrc") { + return nil + } - genTestBuf := &bytes.Buffer{} - if err := genTestTmpl.Execute(genTestBuf, spec); err != nil { - return err - } + genTestTmpl, err := template.New("gen_test").Parse(TestTemplateString) + if err != nil { + return err + } - if err := os.WriteFile(cCtx.Path("generated-test-output"), genTestBuf.Bytes(), 0644); err != nil { - return err - } + genTestBuf := &bytes.Buffer{} + if err := genTestTmpl.Execute(genTestBuf, spec); err != nil { + return err + } - if _, err := sh(cCtx.Context, cCtx.Path("goimports"), "-w", cCtx.Path("generated-test-output")); err != nil { - return err - } + if err := os.WriteFile(cCtx.Path("generated-test-output"), genTestBuf.Bytes(), 0644); err != nil { + return err } + + if _, err := sh(cCtx.Context, cCtx.Path("goimports"), "-w", cCtx.Path("generated-test-output")); err != nil { + return err + } + return nil }