Skip to content

Commit

Permalink
Merge pull request #1086 from akramarenkov/fix-altsrc-nil-source-flag
Browse files Browse the repository at this point in the history
Fix bug 'Unable to load file' in altsrc
  • Loading branch information
coilysiren committed Mar 30, 2020
2 parents dd51d24 + 8f64abd commit 39f9fbd
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 5 deletions.
6 changes: 6 additions & 0 deletions altsrc/default_input_source.go
@@ -0,0 +1,6 @@
package altsrc

// defaultInputSource creates a default InputSourceContext.
func defaultInputSource() (InputSourceContext, error) {
return &MapInputSource{file: "", valueMap: map[interface{}]interface{}{}}, nil
}
6 changes: 5 additions & 1 deletion altsrc/json_source_context.go
Expand Up @@ -17,7 +17,11 @@ import (
// by the given flag.
func NewJSONSourceFromFlagFunc(flag string) func(c *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()
}
}

Expand Down
8 changes: 6 additions & 2 deletions altsrc/toml_file_loader.go
Expand Up @@ -87,8 +87,12 @@ func NewTomlSourceFromFile(file string) (InputSourceContext, error) {
// NewTomlSourceFromFlagFunc creates a new TOML InputSourceContext from a provided flag name and source context.
func NewTomlSourceFromFlagFunc(flagFileName string) func(context *cli.Context) (InputSourceContext, error) {
return func(context *cli.Context) (InputSourceContext, error) {
filePath := context.String(flagFileName)
return NewTomlSourceFromFile(filePath)
if context.IsSet(flagFileName) {
filePath := context.String(flagFileName)
return NewTomlSourceFromFile(filePath)
}

return defaultInputSource()
}
}

Expand Down
8 changes: 6 additions & 2 deletions altsrc/yaml_file_loader.go
Expand Up @@ -33,8 +33,12 @@ func NewYamlSourceFromFile(file string) (InputSourceContext, error) {
// NewYamlSourceFromFlagFunc creates a new Yaml InputSourceContext from a provided flag name and source context.
func NewYamlSourceFromFlagFunc(flagFileName string) func(context *cli.Context) (InputSourceContext, error) {
return func(context *cli.Context) (InputSourceContext, error) {
filePath := context.String(flagFileName)
return NewYamlSourceFromFile(filePath)
if context.IsSet(flagFileName) {
filePath := context.String(flagFileName)
return NewYamlSourceFromFile(filePath)
}

return defaultInputSource()
}
}

Expand Down

0 comments on commit 39f9fbd

Please sign in to comment.