-
Notifications
You must be signed in to change notification settings - Fork 4
/
config.go
55 lines (47 loc) · 1.31 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
package pkg
import (
"fmt"
"os"
"path/filepath"
"github.com/imdario/mergo"
"gopkg.in/yaml.v3"
)
type Config map[string]any
func LoadLocalConfig(path string) (Config, error) {
localConfigBytes, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("error reading config file %s: %w", path, err)
}
var localConfig map[string]interface{}
err = yaml.Unmarshal(localConfigBytes, &localConfig)
if err != nil {
return nil, err
}
return localConfig, nil
}
func (c Config) WithTemplateDefaults(templateName string) (Config, error) {
configForTemplate, err := loadDefaultConfig(templateName)
if err != nil {
return nil, err
}
err = mergo.Merge(&configForTemplate, &c, mergo.WithOverride)
if err != nil {
return nil, err
}
return configForTemplate, nil
}
func loadDefaultConfig(templateName string) (Config, error) {
var config map[string]interface{}
configBytes, err := templateFS.ReadFile(filepath.Join("templates", templateName+".config.yaml"))
if err != nil {
if !os.IsNotExist(err) {
return nil, fmt.Errorf("error reading embedded config file for template %s: %w", templateName, err)
}
} else {
err = yaml.Unmarshal(configBytes, &config)
if err != nil {
return nil, fmt.Errorf("error parsing embedded config file for template %s: %w", templateName, err)
}
}
return config, nil
}