-
Notifications
You must be signed in to change notification settings - Fork 199
/
common.go
105 lines (91 loc) · 2.35 KB
/
common.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
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 config
import (
"fmt"
"os"
"path"
"path/filepath"
"strings"
"github.com/werf/werf/v2/pkg/util"
)
type rawOrigin interface {
configSection() interface{}
doc() *doc
}
type doc struct {
Content []byte
Line int
RenderFilePath string
}
func checkOverflow(m map[string]interface{}, configSection interface{}, doc *doc) error {
if len(m) > 0 {
var keys []string
for k := range m {
keys = append(keys, k)
}
message := fmt.Sprintf("unknown fields: `%s`!", strings.Join(keys, "`, `"))
if configSection == nil {
return newDetailedConfigError(message, nil, doc)
} else {
return newDetailedConfigError(message, configSection, doc)
}
}
return nil
}
func allRelativePaths(paths []string) bool {
for _, p := range paths {
if !isRelativePath(p) {
return false
}
}
return true
}
func isRelativePath(path string) bool {
return !isAbsolutePath(path) && func() bool {
relPath, err := filepath.Rel("dir", filepath.Join("dir", path))
if err != nil || relPath == ".." || strings.HasPrefix(relPath, ".."+string(os.PathSeparator)) {
return false
}
return true
}()
}
func isAbsolutePath(p string) bool {
return path.IsAbs(p)
}
func oneOrNone(conditions []bool) bool {
if len(conditions) == 0 {
return true
}
exist := false
for _, condition := range conditions {
if condition {
if exist {
return false
} else {
exist = true
}
}
}
return true
}
func InterfaceToStringArray(stringOrStringArray, configSection interface{}, doc *doc) ([]string, error) {
if stringOrStringArray == nil {
return []string{}, nil
} else if val, ok := stringOrStringArray.(string); ok {
return []string{val}, nil
} else if interfaceArray, ok := stringOrStringArray.([]interface{}); ok {
var stringArray []string
for _, interf := range interfaceArray {
if val, ok := interf.(string); ok {
stringArray = append(stringArray, val)
} else {
return nil, newDetailedConfigError(fmt.Sprintf("single string or array of strings expected, got `%v`!", stringOrStringArray), configSection, doc)
}
}
return stringArray, nil
} else {
return nil, newDetailedConfigError(fmt.Sprintf("single string or array of strings expected, got `%v`!", stringOrStringArray), configSection, doc)
}
}
// Stack for setting parents in UnmarshalYAML calls
// Set this to util.NewStack before yaml.Unmarshal
var parentStack *util.Stack