/
config.go
92 lines (82 loc) · 2.02 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 santaconfig
import (
"context"
"os"
"path/filepath"
"strings"
"sync"
"github.com/BurntSushi/toml"
"github.com/groob/moroz/santa"
"github.com/pkg/errors"
)
func NewFileRepo(path string) *FileRepo {
repo := FileRepo{
configIndex: make(map[string]santa.Config),
configPath: path,
}
return &repo
}
type FileRepo struct {
mtx sync.RWMutex
configIndex map[string]santa.Config
configPath string
}
func (f *FileRepo) updateIndex(configs []santa.Config) {
f.configIndex = make(map[string]santa.Config, len(configs))
for _, conf := range configs {
f.configIndex[conf.MachineID] = conf
}
}
func (f *FileRepo) AllConfigs(ctx context.Context) ([]santa.Config, error) {
f.mtx.Lock()
defer f.mtx.Unlock()
configs, err := loadConfigs(f.configPath)
if err != nil {
return nil, err
}
f.updateIndex(configs)
return configs, nil
}
func (f *FileRepo) Config(ctx context.Context, machineID string) (santa.Config, error) {
f.mtx.Lock()
defer f.mtx.Unlock()
var conf santa.Config
configs, err := loadConfigs(f.configPath)
if err != nil {
return conf, errors.Wrapf(err, "loading config for machineID %q", machineID)
}
f.updateIndex(configs)
conf, ok := f.configIndex[machineID]
if !ok {
return conf, errors.Errorf("configuration %q not found", machineID)
}
return conf, nil
}
func loadConfigs(path string) ([]santa.Config, error) {
var configs []santa.Config
err := filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if filepath.Ext(info.Name()) != ".toml" {
return nil
}
file, err := os.ReadFile(path)
if err != nil {
return err
}
if !info.IsDir() {
var conf santa.Config
err := toml.Unmarshal(file, &conf)
if err != nil {
return errors.Wrapf(err, "failed to decode %v, skipping \n", info.Name())
}
name := info.Name()
conf.MachineID = strings.TrimSuffix(name, filepath.Ext(name))
configs = append(configs, conf)
return nil
}
return nil
})
return configs, errors.Wrapf(err, "loading configs from path")
}