diff options
author | Julien Dessaux | 2020-12-24 15:18:24 +0100 |
---|---|---|
committer | Julien Dessaux | 2020-12-24 15:18:24 +0100 |
commit | b4dc5d6841f7ded5995e5f308509b1a3a034cbed (patch) | |
tree | 254466925238c53bd51372a57558ec68fdf78205 /config/config.go | |
parent | Implemented the configuration file format (diff) | |
download | shell-game-launcher-b4dc5d6841f7ded5995e5f308509b1a3a034cbed.tar.gz shell-game-launcher-b4dc5d6841f7ded5995e5f308509b1a3a034cbed.tar.bz2 shell-game-launcher-b4dc5d6841f7ded5995e5f308509b1a3a034cbed.zip |
Began implementing config validation
Diffstat (limited to '')
-rw-r--r-- | config/config.go | 40 |
1 files changed, 39 insertions, 1 deletions
diff --git a/config/config.go b/config/config.go index 2c94dbe..fada1ea 100644 --- a/config/config.go +++ b/config/config.go @@ -3,6 +3,7 @@ package config import ( "os" + "github.com/pkg/errors" "gopkg.in/yaml.v2" ) @@ -10,11 +11,47 @@ type Config struct { // AppConfig is the application level configuration entries App App `yaml:"App"` // Menus is the list of menus. The first one is the default menu for an anonymous user, the second one is the default menu for an authenticated user - Menus []Menu `yaml:"Menus"` + Menus map[string]Menu `yaml:"Menus"` // Games is the list of games. Games map[string]Game `yaml:"Games"` } +func (c *Config) validate() error { + if err := c.App.validate(); err != nil { + return err + } + if len(c.Menus) < 2 { + return errors.New("A valid configuration needs at least two menu entries named anonymous and logged_in") + } + found_anonymous_menu := false + found_logged_in_menu := false + for k, v := range c.Menus { + if err := v.validate(k); err != nil { + return err + } + if k == "anonymous" { + found_anonymous_menu = true + } + if k == "logged_in" { + found_logged_in_menu = true + } + } + if !found_anonymous_menu { + return errors.New("No anonymous menu declared") + } + if !found_logged_in_menu { + return errors.New("No logged_in menu declared") + } + for k, v := range c.Games { + if err := v.validate(k); err != nil { + return err + } + } + // TODO menu existence is tested in global config + // TODO game existence is tested in global config + return nil +} + // LoadFile loads the config from a given file func LoadFile(path string) (config Config, err error) { var f *os.File @@ -27,5 +64,6 @@ func LoadFile(path string) (config Config, err error) { if err = decoder.Decode(&config); err != nil { return } + err = config.validate() return } |