Allow FilePath to take []string
This commit is contained in:
parent
18a556e192
commit
f971fca2b2
22
flag.go
22
flag.go
@ -125,9 +125,9 @@ func (f GenericFlag) Apply(set *flag.FlagSet) {
|
|||||||
// provided by the user for parsing by the flag
|
// provided by the user for parsing by the flag
|
||||||
func (f GenericFlag) ApplyWithError(set *flag.FlagSet) error {
|
func (f GenericFlag) ApplyWithError(set *flag.FlagSet) error {
|
||||||
val := f.Value
|
val := f.Value
|
||||||
if envVal, ok := flagFromFileEnv(f.FilePath, f.EnvVar); ok {
|
if fileEnvVal, ok := flagFromFileEnv(f.FilePath, f.EnvVar); ok {
|
||||||
if err := val.Set(envVal); err != nil {
|
if err := val.Set(fileEnvVal); err != nil {
|
||||||
return fmt.Errorf("could not parse %s as value for flag %s: %s", envVal, f.Name, err)
|
return fmt.Errorf("could not parse %s as value for flag %s: %s", fileEnvVal, f.Name, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -658,8 +658,6 @@ func stringifyFlag(f Flag) string {
|
|||||||
),
|
),
|
||||||
)
|
)
|
||||||
case Int64SliceFlag:
|
case Int64SliceFlag:
|
||||||
// return FlagEnvHinter(fv.FieldByName("EnvVar").String(),
|
|
||||||
// stringifyInt64SliceFlag(f.(Int64SliceFlag)))
|
|
||||||
return FlagFileHinter(
|
return FlagFileHinter(
|
||||||
fv.FieldByName("FilePath").String(),
|
fv.FieldByName("FilePath").String(),
|
||||||
FlagEnvHinter(
|
FlagEnvHinter(
|
||||||
@ -668,8 +666,6 @@ func stringifyFlag(f Flag) string {
|
|||||||
),
|
),
|
||||||
)
|
)
|
||||||
case StringSliceFlag:
|
case StringSliceFlag:
|
||||||
// return FlagEnvHinter(fv.FieldByName("EnvVar").String(),
|
|
||||||
// stringifyStringSliceFlag(f.(StringSliceFlag)))
|
|
||||||
return FlagFileHinter(
|
return FlagFileHinter(
|
||||||
fv.FieldByName("FilePath").String(),
|
fv.FieldByName("FilePath").String(),
|
||||||
FlagEnvHinter(
|
FlagEnvHinter(
|
||||||
@ -763,16 +759,16 @@ func stringifySliceFlag(usage, name string, defaultVals []string) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func flagFromFileEnv(filePath, envName string) (val string, ok bool) {
|
func flagFromFileEnv(filePath, envName string) (val string, ok bool) {
|
||||||
if filePath != "" {
|
|
||||||
if data, err := ioutil.ReadFile(filePath); err == nil {
|
|
||||||
return string(data), true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for _, envVar := range strings.Split(envName, ",") {
|
for _, envVar := range strings.Split(envName, ",") {
|
||||||
envVar = strings.TrimSpace(envVar)
|
envVar = strings.TrimSpace(envVar)
|
||||||
if envVal, ok := syscall.Getenv(envVar); ok {
|
if envVal, ok := syscall.Getenv(envVar); ok {
|
||||||
return envVal, true
|
return envVal, true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return
|
for _, fileVar := range strings.Split(filePath, ",") {
|
||||||
|
if data, err := ioutil.ReadFile(fileVar); err == nil {
|
||||||
|
return string(data), true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return "", false
|
||||||
}
|
}
|
||||||
|
@ -1316,7 +1316,7 @@ func TestFlagFromFile(t *testing.T) {
|
|||||||
{"file-does-not-exist", "APP_BAR", ""},
|
{"file-does-not-exist", "APP_BAR", ""},
|
||||||
{"file-does-not-exist", "APP_FOO", "123"},
|
{"file-does-not-exist", "APP_FOO", "123"},
|
||||||
{"file-does-not-exist", "APP_FOO,APP_BAR", "123"},
|
{"file-does-not-exist", "APP_FOO,APP_BAR", "123"},
|
||||||
{temp.Name(), "APP_FOO", "abc"},
|
{temp.Name(), "APP_FOO", "123"},
|
||||||
{temp.Name(), "APP_BAR", "abc"},
|
{temp.Name(), "APP_BAR", "abc"},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user