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,8 +17,12 @@ 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) {
if context.IsSet(flag) {
return NewJSONSourceFromFile(context.String(flag)) return NewJSONSourceFromFile(context.String(flag))
} }
return defaultInputSource()
}
} }
// NewJSONSourceFromFile returns an InputSourceContext suitable for // NewJSONSourceFromFile returns an InputSourceContext suitable for

View File

@ -87,9 +87,13 @@ 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) {
if context.IsSet(flagFileName) {
filePath := context.String(flagFileName) filePath := context.String(flagFileName)
return NewTomlSourceFromFile(filePath) return NewTomlSourceFromFile(filePath)
} }
return defaultInputSource()
}
} }
func readCommandToml(filePath string, container interface{}) (err error) { func readCommandToml(filePath string, container interface{}) (err error) {

View File

@ -33,9 +33,13 @@ 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) {
if context.IsSet(flagFileName) {
filePath := context.String(flagFileName) filePath := context.String(flagFileName)
return NewYamlSourceFromFile(filePath) return NewYamlSourceFromFile(filePath)
} }
return defaultInputSource()
}
} }
func readCommandYaml(filePath string, container interface{}) (err error) { func readCommandYaml(filePath string, container interface{}) (err error) {