Modify nesting flag apply
This commit is contained in:
parent
9a33ae888c
commit
d520bfce53
127
altsrc/flag.go
127
altsrc/flag.go
@ -64,98 +64,113 @@ func InitInputSourceWithContext(flags []cli.Flag, createInputSource func(cCtx *c
|
|||||||
|
|
||||||
// ApplyInputSourceValue applies a generic value to the flagSet if required
|
// ApplyInputSourceValue applies a generic value to the flagSet if required
|
||||||
func (f *GenericFlag) ApplyInputSourceValue(cCtx *cli.Context, isc InputSourceContext) error {
|
func (f *GenericFlag) ApplyInputSourceValue(cCtx *cli.Context, isc InputSourceContext) error {
|
||||||
if f.set != nil && !cCtx.IsSet(f.Name) && !isEnvVarSet(f.EnvVars) {
|
if f.set == nil || cCtx.IsSet(f.Name) || isEnvVarSet(f.EnvVars) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
for _, name := range f.GenericFlag.Names() {
|
for _, name := range f.GenericFlag.Names() {
|
||||||
if isc.isSet(name) {
|
if !isc.isSet(name) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
value, err := isc.Generic(name)
|
value, err := isc.Generic(name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if value != nil {
|
if value == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
for _, n := range f.Names() {
|
for _, n := range f.Names() {
|
||||||
_ = f.set.Set(n, value.String())
|
_ = f.set.Set(n, value.String())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// ApplyInputSourceValue applies a StringSlice value to the flagSet if required
|
// ApplyInputSourceValue applies a StringSlice value to the flagSet if required
|
||||||
func (f *StringSliceFlag) ApplyInputSourceValue(cCtx *cli.Context, isc InputSourceContext) error {
|
func (f *StringSliceFlag) ApplyInputSourceValue(cCtx *cli.Context, isc InputSourceContext) error {
|
||||||
if f.set != nil && !cCtx.IsSet(f.Name) && !isEnvVarSet(f.EnvVars){
|
if f.set == nil || cCtx.IsSet(f.Name) || isEnvVarSet(f.EnvVars) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
for _, name := range f.StringSliceFlag.Names() {
|
for _, name := range f.StringSliceFlag.Names() {
|
||||||
if isc.isSet(name) {
|
if !isc.isSet(name) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
value, err := isc.StringSlice(name)
|
value, err := isc.StringSlice(name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if value != nil {
|
if value == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
var sliceValue = *(cli.NewStringSlice(value...))
|
var sliceValue = *(cli.NewStringSlice(value...))
|
||||||
for _, n := range f.Names() {
|
for _, n := range f.Names() {
|
||||||
underlyingFlag := f.set.Lookup(n)
|
underlyingFlag := f.set.Lookup(n)
|
||||||
if underlyingFlag != nil {
|
if underlyingFlag == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
underlyingFlag.Value = &sliceValue
|
underlyingFlag.Value = &sliceValue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// ApplyInputSourceValue applies a IntSlice value if required
|
// ApplyInputSourceValue applies a IntSlice value if required
|
||||||
func (f *IntSliceFlag) ApplyInputSourceValue(cCtx *cli.Context, isc InputSourceContext) error {
|
func (f *IntSliceFlag) ApplyInputSourceValue(cCtx *cli.Context, isc InputSourceContext) error {
|
||||||
if f.set != nil && !cCtx.IsSet(f.Name) && !isEnvVarSet(f.EnvVars) {
|
if f.set == nil || cCtx.IsSet(f.Name) || isEnvVarSet(f.EnvVars) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
for _, name := range f.IntSliceFlag.Names() {
|
for _, name := range f.IntSliceFlag.Names() {
|
||||||
if isc.isSet(name) {
|
if !isc.isSet(name) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
value, err := isc.IntSlice(name)
|
value, err := isc.IntSlice(name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if value != nil {
|
if value == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
var sliceValue = *(cli.NewIntSlice(value...))
|
var sliceValue = *(cli.NewIntSlice(value...))
|
||||||
for _, n := range f.Names() {
|
for _, n := range f.Names() {
|
||||||
underlyingFlag := f.set.Lookup(n)
|
underlyingFlag := f.set.Lookup(n)
|
||||||
if underlyingFlag != nil {
|
if underlyingFlag == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
underlyingFlag.Value = &sliceValue
|
underlyingFlag.Value = &sliceValue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// ApplyInputSourceValue applies a Bool value to the flagSet if required
|
// ApplyInputSourceValue applies a Bool value to the flagSet if required
|
||||||
func (f *BoolFlag) ApplyInputSourceValue(cCtx *cli.Context, isc InputSourceContext) error {
|
func (f *BoolFlag) ApplyInputSourceValue(cCtx *cli.Context, isc InputSourceContext) error {
|
||||||
if f.set != nil && !cCtx.IsSet(f.Name) && !isEnvVarSet(f.EnvVars) {
|
if f.set == nil || cCtx.IsSet(f.Name) || isEnvVarSet(f.EnvVars) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
for _, name := range f.BoolFlag.Names() {
|
for _, name := range f.BoolFlag.Names() {
|
||||||
if isc.isSet(name) {
|
if !isc.isSet(name) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
value, err := isc.Bool(name)
|
value, err := isc.Bool(name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if value {
|
|
||||||
for _, n := range f.Names() {
|
for _, n := range f.Names() {
|
||||||
_ = f.set.Set(n, strconv.FormatBool(value))
|
_ = f.set.Set(n, strconv.FormatBool(value))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// ApplyInputSourceValue applies a String value to the flagSet if required
|
// ApplyInputSourceValue applies a String value to the flagSet if required
|
||||||
func (f *StringFlag) ApplyInputSourceValue(cCtx *cli.Context, isc InputSourceContext) error {
|
func (f *StringFlag) ApplyInputSourceValue(cCtx *cli.Context, isc InputSourceContext) error {
|
||||||
if f.set != nil && !(cCtx.IsSet(f.Name) || isEnvVarSet(f.EnvVars)) {
|
if f.set == nil || cCtx.IsSet(f.Name) || isEnvVarSet(f.EnvVars) {
|
||||||
for _, name := range f.StringFlag.Names(){
|
return nil
|
||||||
if isc.isSet(name) {
|
}
|
||||||
|
for _, name := range f.StringFlag.Names() {
|
||||||
|
if !isc.isSet(name) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
value, err := isc.String(name)
|
value, err := isc.String(name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -164,46 +179,48 @@ func (f *StringFlag) ApplyInputSourceValue(cCtx *cli.Context, isc InputSourceCon
|
|||||||
_ = f.set.Set(n, value)
|
_ = f.set.Set(n, value)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// ApplyInputSourceValue applies a Path value to the flagSet if required
|
// ApplyInputSourceValue applies a Path value to the flagSet if required
|
||||||
func (f *PathFlag) ApplyInputSourceValue(cCtx *cli.Context, isc InputSourceContext) error {
|
func (f *PathFlag) ApplyInputSourceValue(cCtx *cli.Context, isc InputSourceContext) error {
|
||||||
if f.set != nil && !(cCtx.IsSet(f.Name) || isEnvVarSet(f.EnvVars)) {
|
if f.set == nil || cCtx.IsSet(f.Name) || isEnvVarSet(f.EnvVars) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
for _, name := range f.PathFlag.Names() {
|
for _, name := range f.PathFlag.Names() {
|
||||||
if isc.isSet(name) {
|
if !isc.isSet(name) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
value, err := isc.String(name)
|
value, err := isc.String(name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if value != "" {
|
if value == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
for _, n := range f.Names() {
|
for _, n := range f.Names() {
|
||||||
|
|
||||||
if !filepath.IsAbs(value) && isc.Source() != "" {
|
if !filepath.IsAbs(value) && isc.Source() != "" {
|
||||||
basePathAbs, err := filepath.Abs(isc.Source())
|
basePathAbs, err := filepath.Abs(isc.Source())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
value = filepath.Join(filepath.Dir(basePathAbs), value)
|
value = filepath.Join(filepath.Dir(basePathAbs), value)
|
||||||
}
|
}
|
||||||
|
|
||||||
_ = f.set.Set(n, value)
|
_ = f.set.Set(n, value)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// ApplyInputSourceValue applies a int value to the flagSet if required
|
// ApplyInputSourceValue applies a int value to the flagSet if required
|
||||||
func (f *IntFlag) ApplyInputSourceValue(cCtx *cli.Context, isc InputSourceContext) error {
|
func (f *IntFlag) ApplyInputSourceValue(cCtx *cli.Context, isc InputSourceContext) error {
|
||||||
if f.set != nil && !(cCtx.IsSet(f.Name) || isEnvVarSet(f.EnvVars)) {
|
if f.set == nil || cCtx.IsSet(f.Name) || isEnvVarSet(f.EnvVars) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
for _, name := range f.IntFlag.Names() {
|
for _, name := range f.IntFlag.Names() {
|
||||||
if isc.isSet(name) {
|
if !isc.isSet(name) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
value, err := isc.Int(name)
|
value, err := isc.Int(name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -212,16 +229,18 @@ func (f *IntFlag) ApplyInputSourceValue(cCtx *cli.Context, isc InputSourceContex
|
|||||||
_ = f.set.Set(n, strconv.FormatInt(int64(value), 10))
|
_ = f.set.Set(n, strconv.FormatInt(int64(value), 10))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// ApplyInputSourceValue applies a Duration value to the flagSet if required
|
// ApplyInputSourceValue applies a Duration value to the flagSet if required
|
||||||
func (f *DurationFlag) ApplyInputSourceValue(cCtx *cli.Context, isc InputSourceContext) error {
|
func (f *DurationFlag) ApplyInputSourceValue(cCtx *cli.Context, isc InputSourceContext) error {
|
||||||
if f.set != nil && !(cCtx.IsSet(f.Name) || isEnvVarSet(f.EnvVars)) {
|
if f.set == nil || cCtx.IsSet(f.Name) || isEnvVarSet(f.EnvVars) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
for _, name := range f.DurationFlag.Names() {
|
for _, name := range f.DurationFlag.Names() {
|
||||||
if isc.isSet(name) {
|
if !isc.isSet(name) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
value, err := isc.Duration(name)
|
value, err := isc.Duration(name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -230,16 +249,18 @@ func (f *DurationFlag) ApplyInputSourceValue(cCtx *cli.Context, isc InputSourceC
|
|||||||
_ = f.set.Set(n, value.String())
|
_ = f.set.Set(n, value.String())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// ApplyInputSourceValue applies a Float64 value to the flagSet if required
|
// ApplyInputSourceValue applies a Float64 value to the flagSet if required
|
||||||
func (f *Float64Flag) ApplyInputSourceValue(cCtx *cli.Context, isc InputSourceContext) error {
|
func (f *Float64Flag) ApplyInputSourceValue(cCtx *cli.Context, isc InputSourceContext) error {
|
||||||
if f.set != nil && !(cCtx.IsSet(f.Name) || isEnvVarSet(f.EnvVars)) {
|
if f.set == nil || cCtx.IsSet(f.Name) || isEnvVarSet(f.EnvVars) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
for _, name := range f.Float64Flag.Names() {
|
for _, name := range f.Float64Flag.Names() {
|
||||||
if isc.isSet(name) {
|
if !isc.isSet(name) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
value, err := isc.Float64(name)
|
value, err := isc.Float64(name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -249,8 +270,6 @@ func (f *Float64Flag) ApplyInputSourceValue(cCtx *cli.Context, isc InputSourceCo
|
|||||||
_ = f.set.Set(n, floatStr)
|
_ = f.set.Set(n, floatStr)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user