Merge pull request #1086 from akramarenkov/fix-altsrc-nil-source-flag

Fix bug 'Unable to load file' in altsrc
This commit is contained in:
lynn [they] 2020-03-30 16:25:52 -07:00 committed by GitHub
commit 39f9fbd723
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 23 additions and 5 deletions

View File

@ -0,0 +1,6 @@
package altsrc
// defaultInputSource creates a default InputSourceContext.
func defaultInputSource() (InputSourceContext, error) {
return &MapInputSource{file: "", valueMap: map[interface{}]interface{}{}}, nil
}

View File

@ -17,7 +17,11 @@ import (
// by the given flag. // by the given flag.
func NewJSONSourceFromFlagFunc(flag string) func(c *cli.Context) (InputSourceContext, error) { func NewJSONSourceFromFlagFunc(flag string) func(c *cli.Context) (InputSourceContext, error) {
return func(context *cli.Context) (InputSourceContext, error) { return func(context *cli.Context) (InputSourceContext, error) {
return NewJSONSourceFromFile(context.String(flag)) if context.IsSet(flag) {
return NewJSONSourceFromFile(context.String(flag))
}
return defaultInputSource()
} }
} }

View File

@ -87,8 +87,12 @@ func NewTomlSourceFromFile(file string) (InputSourceContext, error) {
// NewTomlSourceFromFlagFunc creates a new TOML InputSourceContext from a provided flag name and source context. // NewTomlSourceFromFlagFunc creates a new TOML InputSourceContext from a provided flag name and source context.
func NewTomlSourceFromFlagFunc(flagFileName string) func(context *cli.Context) (InputSourceContext, error) { func NewTomlSourceFromFlagFunc(flagFileName string) func(context *cli.Context) (InputSourceContext, error) {
return func(context *cli.Context) (InputSourceContext, error) { return func(context *cli.Context) (InputSourceContext, error) {
filePath := context.String(flagFileName) if context.IsSet(flagFileName) {
return NewTomlSourceFromFile(filePath) filePath := context.String(flagFileName)
return NewTomlSourceFromFile(filePath)
}
return defaultInputSource()
} }
} }

View File

@ -33,8 +33,12 @@ func NewYamlSourceFromFile(file string) (InputSourceContext, error) {
// NewYamlSourceFromFlagFunc creates a new Yaml InputSourceContext from a provided flag name and source context. // NewYamlSourceFromFlagFunc creates a new Yaml InputSourceContext from a provided flag name and source context.
func NewYamlSourceFromFlagFunc(flagFileName string) func(context *cli.Context) (InputSourceContext, error) { func NewYamlSourceFromFlagFunc(flagFileName string) func(context *cli.Context) (InputSourceContext, error) {
return func(context *cli.Context) (InputSourceContext, error) { return func(context *cli.Context) (InputSourceContext, error) {
filePath := context.String(flagFileName) if context.IsSet(flagFileName) {
return NewYamlSourceFromFile(filePath) filePath := context.String(flagFileName)
return NewYamlSourceFromFile(filePath)
}
return defaultInputSource()
} }
} }