-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.go
More file actions
81 lines (65 loc) · 1.64 KB
/
utils.go
File metadata and controls
81 lines (65 loc) · 1.64 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
package main
import (
"fmt"
"strings"
)
func parseEnvLine(line string) (string, string, error) {
line = strings.TrimSpace(line)
if line == "" {
return "", "", nil
}
if strings.HasPrefix(line, "#") {
return "", "", nil
}
key, value, found := strings.Cut(line, "=")
if !found {
return "", "", fmt.Errorf("invalid environment line: key=value separator not found: %q", line)
}
key = strings.ToUpper(strings.TrimSpace(key))
if key == "" {
return "", "", fmt.Errorf("key empty for environment line: %q", line)
}
value = strings.TrimSpace(value)
if strings.HasPrefix(value, "\"") && strings.HasSuffix(value, "\"") {
value = strings.TrimPrefix(value, "\"")
value = strings.TrimSuffix(value, "\"")
}
return key, value, nil
}
func copyMap(m map[string]any) map[string]any {
cp := make(map[string]interface{})
for k, v := range m {
vm, ok := v.(map[string]interface{})
if ok {
cp[k] = copyMap(vm)
} else {
cp[k] = v
}
}
return cp
}
// mergeMap deeply merges two maps, with values from the second map taking precedence
func mergeMap(dest, src map[string]any) map[string]any {
if dest == nil {
dest = make(map[string]any)
}
result := copyMap(dest)
for k, v := range src {
if srcMap, ok := v.(map[string]any); ok {
if destMap, exists := result[k]; exists {
if destMapTyped, ok := destMap.(map[string]any); ok {
result[k] = mergeMap(destMapTyped, srcMap)
} else {
// If destination is not a map, replace it
result[k] = copyMap(srcMap)
}
} else {
result[k] = copyMap(srcMap)
}
} else {
// For non-map values, the source value takes precedence
result[k] = v
}
}
return result
}