-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconfig.go
More file actions
105 lines (91 loc) · 2.19 KB
/
config.go
File metadata and controls
105 lines (91 loc) · 2.19 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
package utils
import (
"fmt"
"os"
"strings"
"go.yaml.in/yaml/v4"
)
type Config struct {
values map[string]any
Path string
// This is the map of secrets that are available during the execution
// of the action graph. The values contain the context name and
// the secret value. Example: 'secrets.input1'
Secrets map[string]string
// this is the map of all 'inputs.xyz' variables
Inputs map[string]any
Env map[string]string
}
func (c *Config) LoadFile(filePath string) error {
c.Path = filePath
data, err := os.ReadFile(filePath)
if err != nil {
return err
}
var parsedData map[string]any
err = yaml.Unmarshal(data, &parsedData)
if err != nil {
return err
}
linearData := flatten(parsedData, "")
for k, v := range linearData {
// if the high level key is missing a dot and the value contains
// an equal sign, the user might have accidentally used this syntax:
// env:
// MY_VAR=foo
// instead of:
// env:
// MY_VAR: foo
vs := fmt.Sprintf("%v", v)
if !strings.Contains(k, ".") && strings.Contains(vs, "=") {
return fmt.Errorf("incorrect syntax, use ':' instead of '=' in key '%s'", k)
}
c.values[k] = v
}
return nil
}
func (c *Config) Get(key string) string {
v := c.values[key]
if v == nil {
return ""
} else {
return fmt.Sprintf("%v", v)
}
}
func (c *Config) GetAll(keyPrefix string) map[string]any {
values := map[string]any{}
for k, v := range c.values {
if k == keyPrefix || strings.HasPrefix(k, keyPrefix+".") {
k = strings.TrimPrefix(k, keyPrefix+".")
values[k] = v
}
}
return values
}
func flatten(data map[string]any, prefix string) map[string]any {
flatMap := make(map[string]any)
for k, v := range data {
key := k
if prefix != "" {
key = prefix + "." + k
}
switch v := v.(type) {
case map[any]any:
// Convert map[any]any to map[string]any
stringMap := make(map[string]any)
for key, value := range v {
stringMap[fmt.Sprintf("%v", key)] = value
}
for subKey, subValue := range flatten(stringMap, key) {
flatMap[subKey] = subValue
}
case map[string]any:
for subKey, subValue := range flatten(v, key) {
flatMap[subKey] = subValue
}
default:
flatMap[key] = v
}
}
return flatMap
}