From 80cbc184c3ebc6424057a9b8cddcc0e86cd29d43 Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Wed, 13 Jan 2021 15:57:06 +0100 Subject: Have the configuration loadFile function return a pointer --- config/config.go | 28 ++++++++++++++-------------- config/config_test.go | 8 ++++---- 2 files changed, 18 insertions(+), 18 deletions(-) (limited to 'config') diff --git a/config/config.go b/config/config.go index 9a161ed..0867105 100644 --- a/config/config.go +++ b/config/config.go @@ -39,26 +39,26 @@ func (c *Config) validate() error { return nil } -// LoadFile loads the config from a given file -func LoadFile(path string) (config Config, err error) { - var f *os.File - f, err = os.Open(path) - if err != nil { - return config, errors.Wrapf(err, "Failed to open configuration file %s", path) +// LoadFile loads the c from a given file +func LoadFile(path string) (*Config, error) { + var c *Config + f, errOpen := os.Open(path) + if errOpen != nil { + return nil, errors.Wrapf(errOpen, "Failed to open curation file %s", path) } defer f.Close() decoder := yaml.NewDecoder(f) - if err = decoder.Decode(&config); err != nil { - return config, errors.Wrap(err, "Failed to decode configuration file") + if err := decoder.Decode(&c); err != nil { + return nil, errors.Wrap(err, "Failed to decode curation file") } - if err = config.validate(); err != nil { - return config, errors.Wrap(err, "Failed to validate configuration") + if err := c.validate(); err != nil { + return nil, errors.Wrap(err, "Failed to validate curation") } // If all looks good we validate menu consistency - for _, v := range config.Menus { - if err = v.validateConsistency(&config); err != nil { - return config, errors.Wrap(err, "Failed menu consistency checks") + for _, v := range c.Menus { + if err := v.validateConsistency(c); err != nil { + return nil, errors.Wrap(err, "Failed menu consistency checks") } } - return + return c, nil } diff --git a/config/config_test.go b/config/config_test.go index 2bc5489..74c7996 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -50,8 +50,8 @@ func TestLoadFile(t *testing.T) { }, }, } - if config, err := LoadFile("test_data/minimal.yaml"); err != nil || !reflect.DeepEqual(want, config) { - t.Fatalf("minimal example failed:\nerror %v\nwant:%+v\ngot: %+v", err, want, config) + if config, err := LoadFile("test_data/minimal.yaml"); err != nil || !reflect.DeepEqual(want, *config) { + t.Fatalf("minimal example failed:\nerror %v\nwant:%+v\ngot: %+v", err, want, *config) } t.Cleanup(func() { os.RemoveAll("var/") }) @@ -215,7 +215,7 @@ func TestLoadFile(t *testing.T) { }, }, } - if config, err := LoadFile("../example/complete.yaml"); err != nil || !reflect.DeepEqual(want, config) { - t.Fatalf("complete example failed:\nerror %v\nwant:%+v\ngot: %+v", err, want, config) + if config, err := LoadFile("../example/complete.yaml"); err != nil || !reflect.DeepEqual(want, *config) { + t.Fatalf("complete example failed:\nerror %v\nwant:%+v\ngot: %+v", err, want, *config) } } -- cgit v1.2.3