-
Notifications
You must be signed in to change notification settings - Fork 4
/
profiles.go
65 lines (58 loc) · 1.52 KB
/
profiles.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
package middlewares
import (
"github.com/go-go-golems/glazed/pkg/cmds/layers"
"github.com/go-go-golems/glazed/pkg/cmds/parameters"
"github.com/pkg/errors"
"gopkg.in/yaml.v3"
"os"
)
func GatherFlagsFromProfiles(
defaultProfileFile string,
profileFile string,
profile string,
options ...parameters.ParseStepOption) Middleware {
return func(next HandlerFunc) HandlerFunc {
return func(layers_ *layers.ParameterLayers, parsedLayers *layers.ParsedLayers) error {
err := next(layers_, parsedLayers)
if err != nil {
return err
}
// if the file does not exist and is not the defaultProfileFile, fail
_, err = os.Stat(profileFile)
if os.IsNotExist(err) {
if profileFile != defaultProfileFile {
return errors.Errorf("profile file %s does not exist", profileFile)
}
return nil
}
// parse profileFile as yaml
f, err := os.Open(profileFile)
if err != nil {
return err
}
defer func(f *os.File) {
_ = f.Close()
}(f)
// profile1:
// layer1:
// parameterName: parameterValue
// layer2:
// parameterName: parameterValue
// etc...
v := map[string]map[string]map[string]interface{}{}
decoder := yaml.NewDecoder(f)
err = decoder.Decode(&v)
if err != nil {
return err
}
if profileMap, ok := v[profile]; ok {
return updateFromMap(layers_, parsedLayers, profileMap, options...)
} else {
if profile != "default" {
return errors.Errorf("profile %s not found in %s", profile, profileFile)
}
return nil
}
}
}
}