Skip to content

Commit

Permalink
Rebase main, update flag-spec.yaml to add Action field
Browse files Browse the repository at this point in the history
  • Loading branch information
xwjdsh committed May 24, 2022
1 parent 38aa644 commit a06ae28
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 41 deletions.
2 changes: 1 addition & 1 deletion altsrc/yaml_file_loader.go
Expand Up @@ -11,7 +11,7 @@ import (

"github.com/urfave/cli/v2"

"gopkg.in/yaml.v2"
"gopkg.in/yaml.v3"
)

type yamlSourceContext struct {
Expand Down
40 changes: 32 additions & 8 deletions flag-spec.yaml
Expand Up @@ -3,46 +3,70 @@
# `genflags.Spec` type that maps to this file structure.

flag_types:
bool: {}
float64: {}
int64: {}
int: {}
time.Duration: {}
uint64: {}
uint: {}

bool:
struct_fields:
- { name: Action, type: "func(*Context, bool) error" }
float64:
struct_fields:
- { name: Action, type: "func(*Context, float64) error" }
int64:
struct_fields:
- { name: Action, type: "func(*Context, int64) error" }
int:
struct_fields:
- { name: Action, type: "func(*Context, int) error" }
time.Duration:
struct_fields:
- { name: Action, type: "func(*Context, time.Duration) error" }
uint64:
struct_fields:
- { name: Action, type: "func(*Context, uint64) error" }
uint:
struct_fields:
- { name: Action, type: "func(*Context, uint) error" }
string:
struct_fields:
- { name: TakesFile, type: bool }
- { name: Action, type: "func(*Context, string) error" }
Generic:
struct_fields:
- { name: TakesFile, type: bool }
- { name: Action, type: "func(*Context, interface{}) error" }
Path:
struct_fields:
- { name: TakesFile, type: bool }
- { name: Action, type: "func(*Context, Path) error" }

Float64Slice:
value_pointer: true
skip_interfaces:
- fmt.Stringer
struct_fields:
- { name: Action, type: "func(*Context, []float64) error" }
Int64Slice:
value_pointer: true
skip_interfaces:
- fmt.Stringer
struct_fields:
- { name: Action, type: "func(*Context, []int64) error" }
IntSlice:
value_pointer: true
skip_interfaces:
- fmt.Stringer
struct_fields:
- { name: Action, type: "func(*Context, []int) error" }
StringSlice:
value_pointer: true
skip_interfaces:
- fmt.Stringer
struct_fields:
- { name: TakesFile, type: bool }
- { name: Action, type: "func(*Context, []string) error" }
Timestamp:
value_pointer: true
struct_fields:
- { name: Layout, type: string }
- { name: Action, type: "func(*Context, *time.Time) error" }

# TODO: enable UintSlice
# UintSlice: {}
Expand Down
28 changes: 2 additions & 26 deletions flag_int_slice.go
Expand Up @@ -87,29 +87,6 @@ func (i *IntSlice) Get() interface{} {
return *i
}

<<<<<<< HEAD
=======
// IntSliceFlag is a flag with type *IntSlice
type IntSliceFlag struct {
Name string
Aliases []string
Usage string
EnvVars []string
FilePath string
Required bool
Hidden bool
Value *IntSlice
DefaultText string
HasBeenSet bool
Action func(*Context, []int) error
}

// IsSet returns whether or not the flag has been set through env or file
func (f *IntSliceFlag) IsSet() bool {
return f.HasBeenSet
}

>>>>>>> e132f01 (feat: flag action)
// String returns a readable representation of this value
// (for usage defaults)
func (f *IntSliceFlag) String() string {
Expand Down Expand Up @@ -181,19 +158,18 @@ func (f *IntSliceFlag) Apply(set *flag.FlagSet) error {
return nil
}

<<<<<<< HEAD
// Get returns the flag’s value in the given Context.
func (f *IntSliceFlag) Get(ctx *Context) []int {
return ctx.IntSlice(f.Name)
=======
}

// RunAction executes flag action if set
func (f *IntSliceFlag) RunAction(c *Context) error {
if f.Action != nil {
return f.Action(c, c.IntSlice(f.Name))
}

return nil
>>>>>>> e132f01 (feat: flag action)
}

// IntSlice looks up the value of a local IntSliceFlag, returns
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Expand Up @@ -7,7 +7,7 @@ require (
github.com/cpuguy83/go-md2man/v2 v2.0.1
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673
golang.org/x/text v0.3.7
gopkg.in/yaml.v2 v2.4.0
gopkg.in/yaml.v3 v3.0.0
)

require github.com/russross/blackfriday/v2 v2.1.0 // indirect
6 changes: 2 additions & 4 deletions go.sum
Expand Up @@ -8,9 +8,7 @@ github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRT
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8=
golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e h1:FDhOuMEY4JVRztM/gsbk+IKUQ8kj74bxZrgw87eMMVc=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0 h1:hjy8E9ON/egN1tAYqKb61G10WtihqetD4sz2H+8nIeA=
gopkg.in/yaml.v3 v3.0.0/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
2 changes: 1 addition & 1 deletion internal/genflags/cmd/genflags/main.go
Expand Up @@ -15,7 +15,7 @@ import (

"github.com/urfave/cli/v2"
"github.com/urfave/cli/v2/internal/genflags"
"gopkg.in/yaml.v2"
"gopkg.in/yaml.v3"
)

const (
Expand Down
30 changes: 30 additions & 0 deletions zz_generated.flags.go
Expand Up @@ -22,6 +22,8 @@ type Float64SliceFlag struct {

Aliases []string
EnvVars []string

Action func(*Context, []float64) error
}

// IsSet returns whether or not the flag has been set through env or file
Expand Down Expand Up @@ -64,6 +66,8 @@ type GenericFlag struct {
EnvVars []string

TakesFile bool

Action func(*Context, interface{}) error
}

// String returns a readable representation of this value (for usage defaults)
Expand Down Expand Up @@ -109,6 +113,8 @@ type Int64SliceFlag struct {

Aliases []string
EnvVars []string

Action func(*Context, []int64) error
}

// IsSet returns whether or not the flag has been set through env or file
Expand Down Expand Up @@ -149,6 +155,8 @@ type IntSliceFlag struct {

Aliases []string
EnvVars []string

Action func(*Context, []int) error
}

// IsSet returns whether or not the flag has been set through env or file
Expand Down Expand Up @@ -191,6 +199,8 @@ type PathFlag struct {
EnvVars []string

TakesFile bool

Action func(*Context, Path) error
}

// String returns a readable representation of this value (for usage defaults)
Expand Down Expand Up @@ -238,6 +248,8 @@ type StringSliceFlag struct {
EnvVars []string

TakesFile bool

Action func(*Context, []string) error
}

// IsSet returns whether or not the flag has been set through env or file
Expand Down Expand Up @@ -280,6 +292,8 @@ type TimestampFlag struct {
EnvVars []string

Layout string

Action func(*Context, *time.Time) error
}

// String returns a readable representation of this value (for usage defaults)
Expand Down Expand Up @@ -325,6 +339,8 @@ type BoolFlag struct {

Aliases []string
EnvVars []string

Action func(*Context, bool) error
}

// String returns a readable representation of this value (for usage defaults)
Expand Down Expand Up @@ -370,6 +386,8 @@ type Float64Flag struct {

Aliases []string
EnvVars []string

Action func(*Context, float64) error
}

// String returns a readable representation of this value (for usage defaults)
Expand Down Expand Up @@ -415,6 +433,8 @@ type IntFlag struct {

Aliases []string
EnvVars []string

Action func(*Context, int) error
}

// String returns a readable representation of this value (for usage defaults)
Expand Down Expand Up @@ -460,6 +480,8 @@ type Int64Flag struct {

Aliases []string
EnvVars []string

Action func(*Context, int64) error
}

// String returns a readable representation of this value (for usage defaults)
Expand Down Expand Up @@ -507,6 +529,8 @@ type StringFlag struct {
EnvVars []string

TakesFile bool

Action func(*Context, string) error
}

// String returns a readable representation of this value (for usage defaults)
Expand Down Expand Up @@ -552,6 +576,8 @@ type DurationFlag struct {

Aliases []string
EnvVars []string

Action func(*Context, time.Duration) error
}

// String returns a readable representation of this value (for usage defaults)
Expand Down Expand Up @@ -597,6 +623,8 @@ type UintFlag struct {

Aliases []string
EnvVars []string

Action func(*Context, uint) error
}

// String returns a readable representation of this value (for usage defaults)
Expand Down Expand Up @@ -642,6 +670,8 @@ type Uint64Flag struct {

Aliases []string
EnvVars []string

Action func(*Context, uint64) error
}

// String returns a readable representation of this value (for usage defaults)
Expand Down

0 comments on commit a06ae28

Please sign in to comment.