-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconfig.go
92 lines (79 loc) · 1.87 KB
/
config.go
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package glass
import (
"bytes"
"errors"
"fmt"
"os"
"strings"
"text/template"
"github.com/glasslabs/looking-glass/module"
"gopkg.in/yaml.v3"
)
// ParseSecrets parses secrets from in.
func ParseSecrets(in []byte) (map[string]any, error) {
sec := map[string]any{}
err := yaml.Unmarshal(in, &sec)
return sec, err
}
// Config contains the main configuration.
type Config struct {
UI UIConfig `yaml:"ui"`
Modules []module.Descriptor `yaml:"modules"`
}
// Validate validates the configuration.
func (c Config) Validate() error {
if err := c.UI.Validate(); err != nil {
return err
}
if len(c.Modules) == 0 {
return errors.New("config: at least one module is required")
}
seen := map[string]bool{}
for _, mod := range c.Modules {
if err := mod.Validate(); err != nil {
return err
}
if seen[mod.Name] {
return fmt.Errorf("config: module name %q is a duplicate. module names must be unique", mod.Name)
}
seen[mod.Name] = true
}
return nil
}
func defaultConfig() Config {
return Config{
UI: UIConfig{
Width: 640,
Height: 480,
Fullscreen: true,
},
}
}
// ParseConfig parses configuration from in.
func ParseConfig(in []byte, cfgPath string, secrets map[string]any) (Config, error) {
cfg := defaultConfig()
tmpl, err := template.New("config").
Parse(string(in))
if err != nil {
return cfg, fmt.Errorf("invalid configuration template: %w", err)
}
var buf bytes.Buffer
err = tmpl.Execute(&buf, map[string]any{
"ConfigPath": cfgPath,
"Secrets": secrets,
"Env": getEnvVars(),
})
if err != nil {
return cfg, fmt.Errorf("invalid configuration template: %w", err)
}
err = yaml.Unmarshal(buf.Bytes(), &cfg)
return cfg, err
}
func getEnvVars() map[string]string {
vars := make(map[string]string)
for _, v := range os.Environ() {
parts := strings.Split(v, "=")
vars[parts[0]] = parts[1]
}
return vars
}