aboutsummaryrefslogtreecommitdiff
path: root/pkg/config/config.go
blob: b363a0c00bf1840eedafe60c95cf234bc3e7c596 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package config

import (
	"fmt"
	"os"

	"gopkg.in/yaml.v3"
)

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 map[string]Menu `yaml:"Menus"`
	// Games is the list of games.
	Games map[string]Game `yaml:"Games"`
}

func (c *Config) validate() error {
	// App
	if err := c.App.validate(); err != nil {
		return err
	}
	// Menus
	if len(c.Menus) < 2 {
		return fmt.Errorf("A valid configuration needs at least two menu entries named anonymous and logged_in")
	}
	for k, v := range c.Menus {
		if err := v.validate(k); err != nil {
			return err
		}
	}
	// Games
	for k, v := range c.Games {
		if err := v.validate(k); err != nil {
			return err
		}
	}
	return nil
}

// 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, fmt.Errorf("Failed to open configuration file %s : %w", path, errOpen)
	}
	defer f.Close()
	decoder := yaml.NewDecoder(f)
	if err := decoder.Decode(&c); err != nil {
		return nil, fmt.Errorf("Failed to decode configuration file : %w", err)
	}
	if err := c.validate(); err != nil {
		return nil, fmt.Errorf("Failed to validate configuration : %w", err)
	}
	// If all looks good we validate menu consistency
	for _, v := range c.Menus {
		if err := v.validateConsistency(c); err != nil {
			return nil, fmt.Errorf("Failed menu consistency checks : %w", err)
		}
	}
	return c, nil
}