From a06ae2824120b3540ed5045e67a4e5d2128427ae Mon Sep 17 00:00:00 2001 From: Wendell Sun Date: Sun, 22 May 2022 23:07:10 +0800 Subject: [PATCH] Rebase main, update flag-spec.yaml to add Action field --- altsrc/yaml_file_loader.go | 2 +- flag-spec.yaml | 40 ++++++++++++++++++++------ flag_int_slice.go | 28 ++---------------- go.mod | 2 +- go.sum | 6 ++-- internal/genflags/cmd/genflags/main.go | 2 +- zz_generated.flags.go | 30 +++++++++++++++++++ 7 files changed, 69 insertions(+), 41 deletions(-) diff --git a/altsrc/yaml_file_loader.go b/altsrc/yaml_file_loader.go index 4ace1f23ec..315db1885f 100644 --- a/altsrc/yaml_file_loader.go +++ b/altsrc/yaml_file_loader.go @@ -11,7 +11,7 @@ import ( "github.com/urfave/cli/v2" - "gopkg.in/yaml.v2" + "gopkg.in/yaml.v3" ) type yamlSourceContext struct { diff --git a/flag-spec.yaml b/flag-spec.yaml index d85fa30bd1..cf3436323b 100644 --- a/flag-spec.yaml +++ b/flag-spec.yaml @@ -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: {} diff --git a/flag_int_slice.go b/flag_int_slice.go index 03b43ede66..09b23334fd 100644 --- a/flag_int_slice.go +++ b/flag_int_slice.go @@ -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 { @@ -181,11 +158,11 @@ 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 { @@ -193,7 +170,6 @@ func (f *IntSliceFlag) RunAction(c *Context) error { } return nil ->>>>>>> e132f01 (feat: flag action) } // IntSlice looks up the value of a local IntSliceFlag, returns diff --git a/go.mod b/go.mod index 6343421515..cb81a4017a 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 8521fc3e33..0660d39c24 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/internal/genflags/cmd/genflags/main.go b/internal/genflags/cmd/genflags/main.go index cad25088f6..4212e60da1 100644 --- a/internal/genflags/cmd/genflags/main.go +++ b/internal/genflags/cmd/genflags/main.go @@ -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 ( diff --git a/zz_generated.flags.go b/zz_generated.flags.go index 3cae978c0c..6e68a368b0 100644 --- a/zz_generated.flags.go +++ b/zz_generated.flags.go @@ -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 @@ -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) @@ -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 @@ -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 @@ -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) @@ -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 @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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)