-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathutils.go
More file actions
101 lines (89 loc) · 2.58 KB
/
utils.go
File metadata and controls
101 lines (89 loc) · 2.58 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
package drycc
import (
"fmt"
"os"
"path"
"regexp"
"strings"
yaml "gopkg.in/yaml.v3"
)
// ParseEnv parses environment variables from a file.
func ParseEnv(fileame string) (map[string]any, error) {
contents, err := os.ReadFile(fileame)
if err != nil {
return nil, err
}
configMap := make(map[string]any)
regex := regexp.MustCompile(`^([A-z0-9_\-\.]+)=([\s\S]*)$`)
for _, config := range strings.Split(string(contents), "\n") {
// Skip config that starts with an comment
if len(config) == 0 || config[0] == '#' {
continue
}
if regex.MatchString(config) {
captures := regex.FindStringSubmatch(config)
configMap[captures[1]] = captures[2]
} else {
return nil, fmt.Errorf("'%s' does not match the pattern 'key=var', ex: MODE=test", config)
}
}
return configMap, nil
}
// ParseDryccfile parses a Drycc configuration file.
func ParseDryccfile(dryccpath string) (map[string]any, error) {
config := make(map[string]any)
if entries, err := os.ReadDir(path.Join(dryccpath, "config")); err == nil {
for _, entry := range entries {
if !entry.IsDir() {
if env, err := ParseEnv(path.Join(dryccpath, "config", entry.Name())); err == nil {
config[entry.Name()] = env
} else {
return nil, err
}
}
}
}
pipeline := make(map[string]any)
if entries, err := os.ReadDir(dryccpath); err == nil {
for _, entry := range entries {
if !entry.IsDir() && (strings.HasSuffix(entry.Name(), ".yaml") || strings.HasSuffix(entry.Name(), ".yml")) {
data := make(map[string]any)
if bytes, err := os.ReadFile(path.Join(dryccpath, entry.Name())); err == nil {
if err = yaml.Unmarshal([]byte(bytes), data); err == nil {
pipeline[entry.Name()] = data
} else {
return nil, err
}
} else {
return nil, err
}
}
}
}
dryccfile := make(map[string]any)
if len(config) > 0 {
dryccfile["config"] = config
}
if len(pipeline) > 0 {
dryccfile["pipeline"] = pipeline
}
return dryccfile, nil
}
// CheckAPICompatibility checks if the server and client API versions are compatible.
func CheckAPICompatibility(serverAPIVersion, clientAPIVersion string) error {
sVersion := strings.Split(serverAPIVersion, ".")
aVersion := strings.Split(clientAPIVersion, ".")
// If API Versions are invalid, return a mismatch error.
if len(sVersion) < 2 || len(aVersion) < 2 {
return ErrAPIMismatch
}
// If major versions are different, return a mismatch error.
if sVersion[0] != aVersion[0] {
return ErrAPIMismatch
}
// If server is older than client, return mismatch error.
if sVersion[1] < aVersion[1] {
return ErrAPIMismatch
}
return nil
}