aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJulien Dessaux2021-01-13 15:57:06 +0100
committerJulien Dessaux2021-01-13 15:57:06 +0100
commit80cbc184c3ebc6424057a9b8cddcc0e86cd29d43 (patch)
tree917471a7955ce3540330c5015b6406dadf86b4a5
parentFinished implementing config tests (diff)
downloadshell-game-launcher-80cbc184c3ebc6424057a9b8cddcc0e86cd29d43.tar.gz
shell-game-launcher-80cbc184c3ebc6424057a9b8cddcc0e86cd29d43.tar.bz2
shell-game-launcher-80cbc184c3ebc6424057a9b8cddcc0e86cd29d43.zip
Have the configuration loadFile function return a pointer
-rw-r--r--config/config.go28
-rw-r--r--config/config_test.go8
-rw-r--r--main.go14
3 files changed, 18 insertions, 32 deletions
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)
}
}
diff --git a/main.go b/main.go
deleted file mode 100644
index 5ab16fd..0000000
--- a/main.go
+++ /dev/null
@@ -1,14 +0,0 @@
-package main
-
-import (
- "log"
- "shell-game-launcher/config"
-)
-
-func main() {
- config, err := config.LoadFile("example/complete.yaml")
- if err != nil {
- log.Fatal(err)
- }
- log.Printf("%+v", config)
-}