Skip to content

Commit

Permalink
Merge commit '46043dd' into v3-porting
Browse files Browse the repository at this point in the history
  • Loading branch information
meatballhat committed Nov 7, 2022
2 parents 8b891ea + 46043dd commit 75f2d1a
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 86 deletions.
18 changes: 18 additions & 0 deletions altsrc/flag-spec.yaml
Original file line number Diff line number Diff line change
@@ -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:
152 changes: 77 additions & 75 deletions altsrc/flag_generated.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Code generated by fg; DO NOT EDIT.
// WARNING: this file is generated. DO NOT EDIT

package altsrc

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
3 changes: 2 additions & 1 deletion cmd/urfave-cli-genflags/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
34 changes: 34 additions & 0 deletions cmd/urfave-cli-genflags/generated_altsrc.gotmpl
Original file line number Diff line number Diff line change
@@ -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
*/}}
35 changes: 25 additions & 10 deletions cmd/urfave-cli-genflags/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)

Expand All @@ -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"},
Expand Down Expand Up @@ -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
}
Expand All @@ -163,20 +169,29 @@ func runGenFlags(cCtx *cli.Context) error {
return err
}

genTestBuf := &bytes.Buffer{}
if err := genTestTmpl.Execute(genTestBuf, spec); err != nil {
if err := os.WriteFile(cCtx.Path("generated-output"), genBuf.Bytes(), 0644); err != nil {
return err
}

if err := os.WriteFile(cCtx.Path("generated-output"), genBuf.Bytes(), 0644); err != nil {
if _, err := sh(cCtx.Context, cCtx.Path("goimports"), "-w", cCtx.Path("generated-output")); err != nil {
return err
}

if err := os.WriteFile(cCtx.Path("generated-test-output"), genTestBuf.Bytes(), 0644); err != nil {
if cCtx.IsSet("altsrc") {
return nil
}

genTestTmpl, err := template.New("gen_test").Parse(TestTemplateString)
if err != nil {
return err
}

if _, err := sh(cCtx.Context, cCtx.Path("goimports"), "-w", cCtx.Path("generated-output")); err != nil {
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
}

Expand Down

0 comments on commit 75f2d1a

Please sign in to comment.