-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
64 lines (57 loc) · 1.46 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
// Copyright 2022 Cosmos Nicolaou. All rights reserved.
// Use of this source code is governed by the Apache-2.0
// license that can be found in the LICENSE file.
package transforms
import (
"fmt"
"os"
"gopkg.in/yaml.v3"
)
// Config represents the loaded transformer configuration.
type Config struct {
Configs []yaml.Node `yaml:"configs"`
Transforms []string
}
// ConfigureAll configures all of the transformers currently registered.
func (c Config) ConfigureAll() error {
for i, name := range c.Transforms {
tfr, ok := installed[name]
if !ok {
return fmt.Errorf("transformer %v not installed", name)
}
if err := tfr.Configure(c.Configs[i]); err != nil {
return err
}
}
return nil
}
// LoadConfigFile loads the transform configuration from the
// specified YAML file.
func LoadConfigFile(filename string) (Config, error) {
data, err := os.ReadFile(filename)
if err != nil {
return Config{}, err
}
return ParseConfig(data)
}
// ParseConfig parses the supplied YAML data to create an instance
// of Config.
func ParseConfig(data []byte) (Config, error) {
cfg := Config{}
if err := yaml.Unmarshal(data, &cfg); err != nil {
return Config{}, err
}
cfg.Transforms = make([]string, len(cfg.Configs))
for i, c := range cfg.Configs {
var tmp map[string]yaml.Node
if err := c.Decode(&tmp); err != nil {
return Config{}, err
}
for name, config := range tmp {
cfg.Configs[i] = config
cfg.Transforms[i] = name
break
}
}
return cfg, nil
}