From 32dd20a85ba52ef14ed9dc1b83c978103cf59710 Mon Sep 17 00:00:00 2001 From: Aleksandr Kramarenko Date: Mon, 9 Mar 2020 13:34:05 +0300 Subject: [PATCH] Fix altsrc nil source flag --- altsrc/default_input_source.go | 6 ++++++ altsrc/json_source_context.go | 6 +++++- altsrc/toml_file_loader.go | 8 ++++++-- altsrc/yaml_file_loader.go | 8 ++++++-- 4 files changed, 23 insertions(+), 5 deletions(-) create mode 100644 altsrc/default_input_source.go diff --git a/altsrc/default_input_source.go b/altsrc/default_input_source.go new file mode 100644 index 0000000..7fda719 --- /dev/null +++ b/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 +} diff --git a/altsrc/json_source_context.go b/altsrc/json_source_context.go index 7d70a2a..0215ced 100644 --- a/altsrc/json_source_context.go +++ b/altsrc/json_source_context.go @@ -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)) + } else { + return defaultInputSource() + } } } diff --git a/altsrc/toml_file_loader.go b/altsrc/toml_file_loader.go index c679fdb..5c7914a 100644 --- a/altsrc/toml_file_loader.go +++ b/altsrc/toml_file_loader.go @@ -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) + } else { + return defaultInputSource() + } } } diff --git a/altsrc/yaml_file_loader.go b/altsrc/yaml_file_loader.go index cf2f8a5..7c5ea19 100644 --- a/altsrc/yaml_file_loader.go +++ b/altsrc/yaml_file_loader.go @@ -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) + } else { + return defaultInputSource() + } } }