Skip to content

Commit 55b2a28

Browse files
committed
chore(utils): add parse dryccfile
1 parent 85cc428 commit 55b2a28

3 files changed

Lines changed: 210 additions & 7 deletions

File tree

http.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func (c *Client) Do(req *http.Request) (*http.Response, error) {
5555
setControllerVersion(c, res.Header)
5656

5757
// Return results along with api compatibility error
58-
return res, checkAPICompatibility(apiVersion, APIVersion)
58+
return res, CheckAPICompatibility(apiVersion, APIVersion)
5959
}
6060

6161
// NewRequest wraps [NewRequestWithContext] using [context.Background].
@@ -143,7 +143,7 @@ your drycc version is correct.`
143143
c.ControllerAPIVersion = apiVersion
144144
setControllerVersion(c, res.Header)
145145

146-
return checkAPICompatibility(apiVersion, APIVersion)
146+
return CheckAPICompatibility(apiVersion, APIVersion)
147147
}
148148

149149
// Healthcheck can be called to see if the controller is healthy
@@ -177,7 +177,7 @@ func (c *Client) Healthcheck() error {
177177
c.ControllerAPIVersion = apiVersion
178178
setControllerVersion(c, res.Header)
179179

180-
return checkAPICompatibility(apiVersion, APIVersion)
180+
return CheckAPICompatibility(apiVersion, APIVersion)
181181
}
182182

183183
func addUserAgent(headers *http.Header, userAgent string) {

utils.go

Lines changed: 75 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,81 @@
11
package drycc
22

3-
import "strings"
3+
import (
4+
"fmt"
5+
"os"
6+
"path"
7+
"regexp"
8+
"strings"
49

5-
func checkAPICompatibility(serverAPIVersion, clientAPIVersion string) error {
10+
yaml "gopkg.in/yaml.v3"
11+
)
12+
13+
func ParseEnv(fileame string) (map[string]interface{}, error) {
14+
contents, err := os.ReadFile(fileame)
15+
if err != nil {
16+
return nil, err
17+
}
18+
configMap := make(map[string]interface{})
19+
regex := regexp.MustCompile(`^([A-z0-9_\-\.]+)=([\s\S]*)$`)
20+
for _, config := range strings.Split(string(contents), "\n") {
21+
// Skip config that starts with an comment
22+
if len(config) == 0 || config[0] == '#' {
23+
continue
24+
}
25+
26+
if regex.MatchString(config) {
27+
captures := regex.FindStringSubmatch(config)
28+
configMap[captures[1]] = captures[2]
29+
} else {
30+
return nil, fmt.Errorf("'%s' does not match the pattern 'key=var', ex: MODE=test", config)
31+
}
32+
}
33+
34+
return configMap, nil
35+
}
36+
37+
func ParseDryccfile(dryccpath string) (map[string]interface{}, error) {
38+
config := make(map[string]interface{})
39+
if entries, err := os.ReadDir(path.Join(dryccpath, "config")); err == nil {
40+
for _, entry := range entries {
41+
if !entry.IsDir() {
42+
if env, err := ParseEnv(path.Join(dryccpath, "config", entry.Name())); err == nil {
43+
config[entry.Name()] = env
44+
} else {
45+
return nil, err
46+
}
47+
}
48+
}
49+
}
50+
pipeline := make(map[string]interface{})
51+
if entries, err := os.ReadDir(dryccpath); err == nil {
52+
for _, entry := range entries {
53+
if !entry.IsDir() && (strings.HasSuffix(entry.Name(), ".yaml") || strings.HasSuffix(entry.Name(), ".yml")) {
54+
data := make(map[string]interface{})
55+
if bytes, err := os.ReadFile(path.Join(dryccpath, entry.Name())); err == nil {
56+
if err = yaml.Unmarshal([]byte(bytes), data); err == nil {
57+
pipeline[entry.Name()] = data
58+
} else {
59+
return nil, err
60+
}
61+
} else {
62+
return nil, err
63+
}
64+
}
65+
}
66+
}
67+
68+
dryccfile := make(map[string]interface{})
69+
if len(config) > 0 {
70+
dryccfile["config"] = config
71+
}
72+
if len(pipeline) > 0 {
73+
dryccfile["pipeline"] = pipeline
74+
}
75+
return dryccfile, nil
76+
}
77+
78+
func CheckAPICompatibility(serverAPIVersion, clientAPIVersion string) error {
679
sVersion := strings.Split(serverAPIVersion, ".")
780
aVersion := strings.Split(clientAPIVersion, ".")
881

utils_test.go

Lines changed: 132 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
package drycc
22

3-
import "testing"
3+
import (
4+
"fmt"
5+
"os"
6+
"path/filepath"
7+
"testing"
8+
)
49

510
type versionComparison struct {
611
Client string
@@ -17,10 +22,135 @@ func TestCheckAPIVersions(t *testing.T) {
1722
}
1823

1924
for _, check := range comparisons {
20-
err := checkAPICompatibility(check.Client, check.Server)
25+
err := CheckAPICompatibility(check.Client, check.Server)
2126

2227
if err != check.Error {
2328
t.Errorf("%v: Expected %v, Got %v", check, check.Error, err)
2429
}
2530
}
2631
}
32+
33+
func TestParseEnv(t *testing.T) {
34+
expects := map[string]string{
35+
"F1": "111",
36+
"F2": "222",
37+
"F3": "333",
38+
}
39+
40+
f, err := os.CreateTemp("", "env")
41+
if err != nil {
42+
t.Fatal(err)
43+
}
44+
defer os.RemoveAll(f.Name())
45+
for key, value := range expects {
46+
f.WriteString(fmt.Sprintf("%s=%s\n", key, value))
47+
}
48+
f.Seek(0, 0)
49+
50+
config, err := ParseEnv(f.Name())
51+
if err != nil {
52+
t.Fatal(err)
53+
}
54+
if len(config) != 3 {
55+
t.Errorf("Expected %d, Got %d", 3, len(config))
56+
}
57+
for key, value := range expects {
58+
if v, ok := config[key]; !ok || config[key] != value {
59+
t.Errorf("Expected %s, Got %s", v, value)
60+
}
61+
}
62+
}
63+
64+
func TestParseDryccfile(t *testing.T) {
65+
webPipeline := `
66+
kind: pipeline
67+
ptype: web
68+
build:
69+
docker: Dockerfile
70+
arg:
71+
CODENAME: bookworm
72+
env:
73+
VERSION: 1.2.1
74+
run:
75+
command:
76+
- ./deployment-tasks.sh
77+
image: worker
78+
timeout: 100
79+
config:
80+
- jvmconfig
81+
deploy:
82+
command:
83+
- bash
84+
- -ec
85+
args:
86+
- bundle exec puma -C config/puma.rb
87+
`
88+
taskPipeline := `
89+
kind: pipeline
90+
ptype: task
91+
build:
92+
docker: Dockerfile
93+
arg:
94+
CODENAME: bookworm
95+
env:
96+
VERSION: 1.2.1
97+
run:
98+
command:
99+
- ./deployment-tasks.sh
100+
image: worker
101+
timeout: 100
102+
config:
103+
- jvmconfig
104+
deploy:
105+
command:
106+
- bash
107+
- -ec
108+
args:
109+
- bundle exec puma -C config/puma.rb
110+
`
111+
112+
tmp, err := os.MkdirTemp("", "drycc")
113+
if err != nil {
114+
t.Fatal(err)
115+
}
116+
defer os.RemoveAll(tmp)
117+
118+
configDir := filepath.Join(tmp, "config")
119+
os.MkdirAll(configDir, 0777)
120+
os.WriteFile(filepath.Join(tmp, "web.yml"), []byte(webPipeline), 0777)
121+
os.WriteFile(filepath.Join(tmp, "task.yaml"), []byte(taskPipeline), 0777)
122+
os.WriteFile(filepath.Join(configDir, "global"), []byte("DEBUG=true\n"), 0777)
123+
os.WriteFile(filepath.Join(configDir, "web"), []byte("PORT=8000\nJVM_OPTIONS=-Xms16G\n"), 0777)
124+
125+
dryccfile, err := ParseDryccfile(tmp)
126+
if err != nil {
127+
t.Fatal(err)
128+
}
129+
if config, ok := dryccfile["config"]; ok {
130+
webConfig := config.(map[string]interface{})["web"].(map[string]interface{})
131+
globalConfig := config.(map[string]interface{})["global"].(map[string]interface{})
132+
133+
if globalConfig["DEBUG"] != "true" {
134+
t.Errorf("Expected %s, Got %s", "true", globalConfig["DEBUG"])
135+
}
136+
if webConfig["PORT"].(string) != "8000" {
137+
t.Errorf("Expected %s, Got %s", "8000", webConfig["PORT"])
138+
}
139+
if webConfig["JVM_OPTIONS"].(string) != "-Xms16G" {
140+
t.Errorf("Expected %s, Got %s", "-Xms16G", webConfig["JVM_OPTIONS"])
141+
}
142+
} else {
143+
t.Error("Config not found")
144+
}
145+
146+
if pipeline, ok := dryccfile["pipeline"]; ok {
147+
if _, ok := pipeline.(map[string]interface{})["web.yml"]; !ok {
148+
t.Error("web.yml not found")
149+
}
150+
if _, ok := pipeline.(map[string]interface{})["task.yaml"]; !ok {
151+
t.Error("task.yaml not found")
152+
}
153+
} else {
154+
t.Error("Pipeline not found")
155+
}
156+
}

0 commit comments

Comments
 (0)