diff options
author | Flu0r1ne <flur01ne@flu0r1ne.net> | 2021-08-22 23:27:53 -0500 |
---|---|---|
committer | Flu0r1ne <flur01ne@flu0r1ne.net> | 2021-08-22 23:27:53 -0500 |
commit | 24548e87decbdfea38bbf692cecad6d4eefc3ec0 (patch) | |
tree | 4f5d1432d1e22a1f19ceabbc92dcdd86bed98017 /config.go | |
parent | 5d33040ab80b5cce7883b2e5965aa17db2e6515a (diff) | |
download | deb-planr-24548e87decbdfea38bbf692cecad6d4eefc3ec0.tar.xz deb-planr-24548e87decbdfea38bbf692cecad6d4eefc3ec0.zip |
Refactoring & Enhanced logging
Diffstat (limited to 'config.go')
-rw-r--r-- | config.go | 42 |
1 files changed, 29 insertions, 13 deletions
@@ -1,7 +1,6 @@ package planr import ( - // "fmt" "log" "github.com/BurntSushi/toml" ) @@ -41,7 +40,7 @@ type InheritableConfig interface { // A parser function takes a blob of TOML and decodes it into // configuration relevant to an adapter -type TomlParser func (toml.Primitive) InheritableConfig +type TomlParser func (toml.Primitive) (InheritableConfig, error) // The name under which an adapter registers corresponds // to a table under the super-table adapters. All corresponding @@ -134,7 +133,10 @@ func (child *Defaults) Inherit(p interface{}) { // according to methods registered with the runner // Once parsed, they are stored alongside the registered name to determine // which adapter will receive the configuration -func (defaults *Defaults) decodeAdapters(adapters []AdapterConfig, asDefault bool) { +func (defaults *Defaults) decodeAdapters( + adapters []AdapterConfig, + asDefault bool, +) error { defaults.configs_ = &adapters defaults.adapters_ = make(map[string]InheritableConfig) @@ -144,40 +146,54 @@ func (defaults *Defaults) decodeAdapters(adapters []AdapterConfig, asDefault boo if exists { var parsed InheritableConfig + var err error if asDefault { - parsed = config.ParseDefaultConfig(primitive) + parsed, err = config.ParseDefaultConfig(primitive) } else { - parsed = config.ParseConfig(primitive) + parsed, err = config.ParseConfig(primitive) + } + + if err != nil { + return err } defaults.adapters_[config.Name] = parsed } } } + + return nil } // Decode defaults.toml -func DecodeDefaults(path string, adapterCfg []AdapterConfig) Defaults { +func DecodeDefaults(path string, adapterCfg []AdapterConfig) (Defaults, error) { defaults := Defaults { } + + if _, err := toml.DecodeFile(path, &defaults); err != nil { - log.Fatal(err) + return defaults, err + } + + if err := defaults.decodeAdapters(adapterCfg, true); err != nil { + return defaults, err } - defaults.decodeAdapters(adapterCfg, true) - return defaults + return defaults, nil } // Decode an individual unit -func DecodeConfig(path string, adapterCfg []AdapterConfig) TestCaseConfig { +func DecodeConfig(path string, adapterCfg []AdapterConfig) (TestCaseConfig, error) { config := TestCaseConfig { } if _, err := toml.DecodeFile(path, &config); err != nil { - log.Fatal(err) + return config, nil } - config.decodeAdapters(adapterCfg, false) + if err := config.decodeAdapters(adapterCfg, false); err != nil { + return config, err + } - return config + return config, nil } |