Merge pull request #1553 from dearchap/altsrc_generation
Chore: Add altsrc flag definition generation
This commit is contained in:
commit
46043dde65
18
altsrc/flag-spec.yaml
Normal file
18
altsrc/flag-spec.yaml
Normal file
@ -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:
|
@ -1,4 +1,4 @@
|
||||
// Code generated by fg; DO NOT EDIT.
|
||||
// WARNING: this file is generated. DO NOT EDIT
|
||||
|
||||
package altsrc
|
||||
|
||||
@ -65,6 +65,25 @@ func (f *Float64Flag) Apply(set *flag.FlagSet) error {
|
||||
return f.Float64Flag.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 {
|
||||
f.set = set
|
||||
return f.Float64SliceFlag.Apply(set)
|
||||
}
|
||||
|
||||
// GenericFlag is the flag type that wraps cli.GenericFlag to allow
|
||||
// for other values to be specified
|
||||
type GenericFlag struct {
|
||||
@ -84,25 +103,6 @@ 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 {
|
||||
@ -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,23 +160,42 @@ 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)
|
||||
}
|
||||
|
||||
// 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)
|
||||
}
|
||||
|
||||
// StringFlag is the flag type that wraps cli.StringFlag to allow
|
||||
@ -198,25 +217,6 @@ 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 {
|
||||
@ -236,6 +236,25 @@ func (f *StringSliceFlag) Apply(set *flag.FlagSet) error {
|
||||
return f.StringSliceFlag.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)
|
||||
}
|
||||
|
||||
// Uint64Flag is the flag type that wraps cli.Uint64Flag to allow
|
||||
// for other values to be specified
|
||||
type Uint64Flag struct {
|
||||
@ -255,21 +274,4 @@ func (f *Uint64Flag) Apply(set *flag.FlagSet) error {
|
||||
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)
|
||||
}
|
||||
// vim:ro
|
||||
|
@ -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
cmd/urfave-cli-genflags/generated_altsrc.gotmpl
Normal file
34
cmd/urfave-cli-genflags/generated_altsrc.gotmpl
Normal file
@ -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
|
||||
*/}}
|
@ -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,23 +169,32 @@ func runGenFlags(cCtx *cli.Context) error {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := os.WriteFile(cCtx.Path("generated-output"), genBuf.Bytes(), 0644); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if _, err := sh(cCtx.Context, cCtx.Path("goimports"), "-w", cCtx.Path("generated-output")); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if cCtx.IsSet("altsrc") {
|
||||
return nil
|
||||
}
|
||||
|
||||
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-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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user