Report the source of a value when we cannot parse it
If you allow a flag to be set from environment variables or files and a parse error occurs from one of them, it is very useful for the error message to mention where the value came from. Without this, it can be difficult to notice an error caused by an unexpected environment variable being set. Implements #1167.
This commit is contained in:
8
flag.go
8
flag.go
@@ -372,17 +372,17 @@ func hasFlag(flags []Flag, fl Flag) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func flagFromEnvOrFile(envVars []string, filePath string) (val string, ok bool) {
|
||||
func flagFromEnvOrFile(envVars []string, filePath string) (val string, ok bool, source string) {
|
||||
for _, envVar := range envVars {
|
||||
envVar = strings.TrimSpace(envVar)
|
||||
if val, ok := syscall.Getenv(envVar); ok {
|
||||
return val, true
|
||||
return val, true, fmt.Sprintf("from environment variable %q", envVar)
|
||||
}
|
||||
}
|
||||
for _, fileVar := range strings.Split(filePath, ",") {
|
||||
if data, err := ioutil.ReadFile(fileVar); err == nil {
|
||||
return string(data), true
|
||||
return string(data), true, fmt.Sprintf("from file %q", filePath)
|
||||
}
|
||||
}
|
||||
return "", false
|
||||
return "", false, ""
|
||||
}
|
||||
|
Reference in New Issue
Block a user