refactor load logic
@@ -89,6 +89,10 @@ func (l *Loader) Save(conf *Config, path string) error {
filepath.Ext(path),
)
+ if configFormat == nil {
+ return fmt.Errorf("parser not found for format")
+ }
+
data, err := configFormat.Serialize(conf)
if err != nil {
return err
@@ -125,24 +129,22 @@ func (l *Loader) LoadConfig(confPath string) (*Config, error) {
// If more than one path is specified, their contents are merged
// together into on configuration object.
func (l *Loader) Load(paths []string) (*Config, error) {
- var baseConf *Config
+ // special case for default configs
+ // try to load local config and only if not found try to load global config
+ if IsDefaultPath(paths) {
+ conf, err := l.LoadConfig(DefaultPath)
+ if err == nil {
+ return l.postprocess(conf)
+ }
- if IsGlobalPath(paths) {
- var defaultConfigError error
- var globalConfigError error
- //try to load default flow.json
- baseConf, defaultConfigError = l.LoadConfig(paths[1])
- if defaultConfigError != nil {
- //try to load global flow.json
- baseConf, globalConfigError = l.LoadConfig(paths[0])
- if globalConfigError != nil {
+ conf, err = l.LoadConfig(GlobalPath())
+ if err != nil {
return nil, ErrDoesNotExist
}
}
- } else {
+ var baseConf *Config
for _, confPath := range paths {
-
conf, err := l.LoadConfig(confPath)
if err != nil {
return nil, err
@@ -155,19 +157,13 @@ func (l *Loader) Load(paths []string) (*Config, error) {
l.composeConfig(baseConf, conf)
}
- }
// if no config was loaded - neither local nor global return an error.
if baseConf == nil {
return nil, ErrDoesNotExist
}
- baseConf, err := l.postprocess(baseConf)
- if err != nil {
- return nil, err
- }
-
- return baseConf, nil
+ return l.postprocess(baseConf)
}
// preprocess does all manipulations to the raw configuration format happens here.
