forked from TrueCloudLab/rclone
config: fix setting of non top level flags from environment variables
Before this fix, attempting to set a non top level environment variable would fail with "Couldn't find flag". This fixes it by passing in the flags that the env var is being set from. Fixes #3615
This commit is contained in:
parent
1fe1a19339
commit
6e378d7d32
1 changed files with 21 additions and 21 deletions
|
@ -13,11 +13,11 @@ import (
|
|||
|
||||
// setDefaultFromEnv constructs a name from the flag passed in and
|
||||
// sets the default from the environment if possible.
|
||||
func setDefaultFromEnv(name string) {
|
||||
func setDefaultFromEnv(flags *pflag.FlagSet, name string) {
|
||||
key := fs.OptionToEnv(name)
|
||||
newValue, found := os.LookupEnv(key)
|
||||
if found {
|
||||
flag := pflag.Lookup(name)
|
||||
flag := flags.Lookup(name)
|
||||
if flag == nil {
|
||||
log.Fatalf("Couldn't find flag %q", name)
|
||||
}
|
||||
|
@ -35,7 +35,7 @@ func setDefaultFromEnv(name string) {
|
|||
// It is a thin wrapper around pflag.StringP
|
||||
func StringP(name, shorthand string, value string, usage string) (out *string) {
|
||||
out = pflag.StringP(name, shorthand, value, usage)
|
||||
setDefaultFromEnv(name)
|
||||
setDefaultFromEnv(pflag.CommandLine, name)
|
||||
return out
|
||||
}
|
||||
|
||||
|
@ -44,7 +44,7 @@ func StringP(name, shorthand string, value string, usage string) (out *string) {
|
|||
// It is a thin wrapper around pflag.StringVarP
|
||||
func StringVarP(flags *pflag.FlagSet, p *string, name, shorthand string, value string, usage string) {
|
||||
flags.StringVarP(p, name, shorthand, value, usage)
|
||||
setDefaultFromEnv(name)
|
||||
setDefaultFromEnv(flags, name)
|
||||
}
|
||||
|
||||
// BoolP defines a flag which can be overridden by an environment variable
|
||||
|
@ -52,7 +52,7 @@ func StringVarP(flags *pflag.FlagSet, p *string, name, shorthand string, value s
|
|||
// It is a thin wrapper around pflag.BoolP
|
||||
func BoolP(name, shorthand string, value bool, usage string) (out *bool) {
|
||||
out = pflag.BoolP(name, shorthand, value, usage)
|
||||
setDefaultFromEnv(name)
|
||||
setDefaultFromEnv(pflag.CommandLine, name)
|
||||
return out
|
||||
}
|
||||
|
||||
|
@ -61,7 +61,7 @@ func BoolP(name, shorthand string, value bool, usage string) (out *bool) {
|
|||
// It is a thin wrapper around pflag.BoolVarP
|
||||
func BoolVarP(flags *pflag.FlagSet, p *bool, name, shorthand string, value bool, usage string) {
|
||||
flags.BoolVarP(p, name, shorthand, value, usage)
|
||||
setDefaultFromEnv(name)
|
||||
setDefaultFromEnv(flags, name)
|
||||
}
|
||||
|
||||
// IntP defines a flag which can be overridden by an environment variable
|
||||
|
@ -69,7 +69,7 @@ func BoolVarP(flags *pflag.FlagSet, p *bool, name, shorthand string, value bool,
|
|||
// It is a thin wrapper around pflag.IntP
|
||||
func IntP(name, shorthand string, value int, usage string) (out *int) {
|
||||
out = pflag.IntP(name, shorthand, value, usage)
|
||||
setDefaultFromEnv(name)
|
||||
setDefaultFromEnv(pflag.CommandLine, name)
|
||||
return out
|
||||
}
|
||||
|
||||
|
@ -78,7 +78,7 @@ func IntP(name, shorthand string, value int, usage string) (out *int) {
|
|||
// It is a thin wrapper around pflag.IntP
|
||||
func Int64P(name, shorthand string, value int64, usage string) (out *int64) {
|
||||
out = pflag.Int64P(name, shorthand, value, usage)
|
||||
setDefaultFromEnv(name)
|
||||
setDefaultFromEnv(pflag.CommandLine, name)
|
||||
return out
|
||||
}
|
||||
|
||||
|
@ -87,7 +87,7 @@ func Int64P(name, shorthand string, value int64, usage string) (out *int64) {
|
|||
// It is a thin wrapper around pflag.Int64VarP
|
||||
func IntVar64P(flags *pflag.FlagSet, p *int64, name, shorthand string, value int64, usage string) {
|
||||
flags.Int64VarP(p, name, shorthand, value, usage)
|
||||
setDefaultFromEnv(name)
|
||||
setDefaultFromEnv(flags, name)
|
||||
}
|
||||
|
||||
// IntVarP defines a flag which can be overridden by an environment variable
|
||||
|
@ -95,7 +95,7 @@ func IntVar64P(flags *pflag.FlagSet, p *int64, name, shorthand string, value int
|
|||
// It is a thin wrapper around pflag.IntVarP
|
||||
func IntVarP(flags *pflag.FlagSet, p *int, name, shorthand string, value int, usage string) {
|
||||
flags.IntVarP(p, name, shorthand, value, usage)
|
||||
setDefaultFromEnv(name)
|
||||
setDefaultFromEnv(flags, name)
|
||||
}
|
||||
|
||||
// Uint32VarP defines a flag which can be overridden by an environment variable
|
||||
|
@ -103,7 +103,7 @@ func IntVarP(flags *pflag.FlagSet, p *int, name, shorthand string, value int, us
|
|||
// It is a thin wrapper around pflag.Uint32VarP
|
||||
func Uint32VarP(flags *pflag.FlagSet, p *uint32, name, shorthand string, value uint32, usage string) {
|
||||
flags.Uint32VarP(p, name, shorthand, value, usage)
|
||||
setDefaultFromEnv(name)
|
||||
setDefaultFromEnv(flags, name)
|
||||
}
|
||||
|
||||
// Float64P defines a flag which can be overridden by an environment variable
|
||||
|
@ -111,7 +111,7 @@ func Uint32VarP(flags *pflag.FlagSet, p *uint32, name, shorthand string, value u
|
|||
// It is a thin wrapper around pflag.Float64P
|
||||
func Float64P(name, shorthand string, value float64, usage string) (out *float64) {
|
||||
out = pflag.Float64P(name, shorthand, value, usage)
|
||||
setDefaultFromEnv(name)
|
||||
setDefaultFromEnv(pflag.CommandLine, name)
|
||||
return out
|
||||
}
|
||||
|
||||
|
@ -120,7 +120,7 @@ func Float64P(name, shorthand string, value float64, usage string) (out *float64
|
|||
// It is a thin wrapper around pflag.Float64VarP
|
||||
func Float64VarP(flags *pflag.FlagSet, p *float64, name, shorthand string, value float64, usage string) {
|
||||
flags.Float64VarP(p, name, shorthand, value, usage)
|
||||
setDefaultFromEnv(name)
|
||||
setDefaultFromEnv(flags, name)
|
||||
}
|
||||
|
||||
// DurationP defines a flag which can be overridden by an environment variable
|
||||
|
@ -128,7 +128,7 @@ func Float64VarP(flags *pflag.FlagSet, p *float64, name, shorthand string, value
|
|||
// It is a thin wrapper around pflag.DurationP
|
||||
func DurationP(name, shorthand string, value time.Duration, usage string) (out *time.Duration) {
|
||||
out = pflag.DurationP(name, shorthand, value, usage)
|
||||
setDefaultFromEnv(name)
|
||||
setDefaultFromEnv(pflag.CommandLine, name)
|
||||
return out
|
||||
}
|
||||
|
||||
|
@ -137,7 +137,7 @@ func DurationP(name, shorthand string, value time.Duration, usage string) (out *
|
|||
// It is a thin wrapper around pflag.DurationVarP
|
||||
func DurationVarP(flags *pflag.FlagSet, p *time.Duration, name, shorthand string, value time.Duration, usage string) {
|
||||
flags.DurationVarP(p, name, shorthand, value, usage)
|
||||
setDefaultFromEnv(name)
|
||||
setDefaultFromEnv(flags, name)
|
||||
}
|
||||
|
||||
// VarP defines a flag which can be overridden by an environment variable
|
||||
|
@ -145,7 +145,7 @@ func DurationVarP(flags *pflag.FlagSet, p *time.Duration, name, shorthand string
|
|||
// It is a thin wrapper around pflag.VarP
|
||||
func VarP(value pflag.Value, name, shorthand, usage string) {
|
||||
pflag.VarP(value, name, shorthand, usage)
|
||||
setDefaultFromEnv(name)
|
||||
setDefaultFromEnv(pflag.CommandLine, name)
|
||||
}
|
||||
|
||||
// FVarP defines a flag which can be overridden by an environment variable
|
||||
|
@ -153,7 +153,7 @@ func VarP(value pflag.Value, name, shorthand, usage string) {
|
|||
// It is a thin wrapper around pflag.VarP
|
||||
func FVarP(flags *pflag.FlagSet, value pflag.Value, name, shorthand, usage string) {
|
||||
flags.VarP(value, name, shorthand, usage)
|
||||
setDefaultFromEnv(name)
|
||||
setDefaultFromEnv(flags, name)
|
||||
}
|
||||
|
||||
// StringArrayP defines a flag which can be overridden by an environment variable
|
||||
|
@ -163,7 +163,7 @@ func FVarP(flags *pflag.FlagSet, value pflag.Value, name, shorthand, usage strin
|
|||
// It is a thin wrapper around pflag.StringArrayP
|
||||
func StringArrayP(name, shorthand string, value []string, usage string) (out *[]string) {
|
||||
out = pflag.StringArrayP(name, shorthand, value, usage)
|
||||
setDefaultFromEnv(name)
|
||||
setDefaultFromEnv(pflag.CommandLine, name)
|
||||
return out
|
||||
}
|
||||
|
||||
|
@ -174,7 +174,7 @@ func StringArrayP(name, shorthand string, value []string, usage string) (out *[]
|
|||
// It is a thin wrapper around pflag.StringArrayVarP
|
||||
func StringArrayVarP(flags *pflag.FlagSet, p *[]string, name, shorthand string, value []string, usage string) {
|
||||
flags.StringArrayVarP(p, name, shorthand, value, usage)
|
||||
setDefaultFromEnv(name)
|
||||
setDefaultFromEnv(flags, name)
|
||||
}
|
||||
|
||||
// CountP defines a flag which can be overridden by an environment variable
|
||||
|
@ -182,7 +182,7 @@ func StringArrayVarP(flags *pflag.FlagSet, p *[]string, name, shorthand string,
|
|||
// It is a thin wrapper around pflag.CountP
|
||||
func CountP(name, shorthand string, usage string) (out *int) {
|
||||
out = pflag.CountP(name, shorthand, usage)
|
||||
setDefaultFromEnv(name)
|
||||
setDefaultFromEnv(pflag.CommandLine, name)
|
||||
return out
|
||||
}
|
||||
|
||||
|
@ -191,5 +191,5 @@ func CountP(name, shorthand string, usage string) (out *int) {
|
|||
// It is a thin wrapper around pflag.CountVarP
|
||||
func CountVarP(flags *pflag.FlagSet, p *int, name, shorthand string, usage string) {
|
||||
flags.CountVarP(p, name, shorthand, usage)
|
||||
setDefaultFromEnv(name)
|
||||
setDefaultFromEnv(flags, name)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue