From 24548e87decbdfea38bbf692cecad6d4eefc3ec0 Mon Sep 17 00:00:00 2001 From: Flu0r1ne Date: Sun, 22 Aug 2021 23:27:53 -0500 Subject: Refactoring & Enhanced logging --- config.go | 42 +++++++++++++++++++++++++++++------------- 1 file changed, 29 insertions(+), 13 deletions(-) (limited to 'config.go') diff --git a/config.go b/config.go index 6efa90d..bc0fa6a 100644 --- a/config.go +++ b/config.go @@ -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 } -- cgit v1.2.3