-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
168 lines (131 loc) · 4.26 KB
/
util.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package cmd
import (
"errors"
"fmt"
"io/fs"
"os"
"strings"
"gopkg.in/yaml.v3"
)
// loadPolicyFile tries to intelligently choose a filepath for the
// policy file and then return the unmarshalled struct. If customPath is not
// empty, it will try to use that before the default filenames.
func loadPolicyFile(customPath string) (*PolicyFile, []byte, error) {
var file PolicyFile
var yamlContent []byte
var err error
yamlContent, err = loadYAMLFile(customPath, "policy.yml")
if err != nil {
return nil, nil, fmt.Errorf("./policy.yml' was not found. The file either doesn't exist, you're in the wrong directory, or the '--policyFile' flag needs to be set.")
}
err = yaml.Unmarshal(yamlContent, &file)
if err != nil {
return nil, nil, err
}
return &file, yamlContent, nil
}
// loadRepositoriesFile tries to intelligently choose a filepath for the
// Wardenfile and then return the unmarshalled struct. If customPath is not
// empty, it will try to use that before the default filenames.
func loadRepositoriesFile(customPath string) (*RepositoriesFile, []byte, error) {
var repositoriesFile RepositoriesFile
var yamlContent []byte
var err error
yamlContent, err = loadYAMLFile(customPath, "repositories.yml")
if err != nil {
return nil, nil, fmt.Errorf("./repositories.yml' was not found. The file either doesn't exist, you're in the wrong directory, or the '--repositoriesFile' flag needs to be set.")
}
err = yaml.Unmarshal(yamlContent, &repositoriesFile)
if err != nil {
return nil, nil, fmt.Errorf("The repositories file couldn't be parsed. Something is wrong.")
}
// create all group
compiledFile := RepositoriesFile{
&RepositoryGroup{
Group: "all",
Children: repositoriesFile,
},
}
return &compiledFile, yamlContent, nil
}
// loadYAMLFile attempts to load a YAML file based on one or more possible file
// names. Both .yml and .yaml will be attempted and in that order.
func loadYAMLFile(filepaths ...string) ([]byte, error) {
var yamlContent []byte
var possiblePaths []string
var err error
if len(filepaths) == 0 {
return nil, fmt.Errorf("At least one filepath needs to be provided.")
}
for _, path := range filepaths {
if strings.HasSuffix(path, ".yml") {
path = path[0 : len(path)-3]
} else if strings.HasSuffix(path, ".yaml") {
path = path[0 : len(path)-4]
} else if path == "" {
continue
} else {
return nil, fmt.Errorf("Only YAML files are supported.")
}
possiblePaths = append(possiblePaths, path+"yml")
possiblePaths = append(possiblePaths, path+"yaml")
}
for _, path := range filepaths {
yamlContent, err = os.ReadFile(path)
if errors.Is(err, fs.ErrNotExist) {
continue
} else if errors.Is(err, fs.ErrPermission) {
return nil, fmt.Errorf("Warden doesn't have permission to open %s", path)
} else if err != nil {
return nil, err
}
break
}
if len(yamlContent) == 0 {
return nil, fmt.Errorf("The YAML file was not found.")
}
return yamlContent, nil
}
// saveYAMLFile attempts to save YAML to a file based on one or more possible
// file names. Both .yml and .yaml will be attempted and in that order. If
// 'create' is true, the filename has not not exist to be saved. Returns the
// filepath used.
func saveYAMLFile(content []byte, create bool, filepaths ...string) (string, error) {
var possiblePaths []string
var err error
if len(filepaths) == 0 {
return "", fmt.Errorf("At least one filepath needs to be provided.")
}
for _, path := range filepaths {
if strings.HasSuffix(path, ".yml") {
path = path[0 : len(path)-3]
} else if strings.HasSuffix(path, ".yaml") {
path = path[0 : len(path)-4]
} else if path == "" {
continue
} else {
return "", fmt.Errorf("Only YAML files are supported.")
}
possiblePaths = append(possiblePaths, path+"yml")
possiblePaths = append(possiblePaths, path+"yaml")
}
choosenFilepath := ""
for _, path := range filepaths {
_, err = os.Stat(path)
if errors.Is(err, fs.ErrNotExist) && create {
choosenFilepath = path
break
} else if !errors.Is(err, fs.ErrNotExist) && !create {
choosenFilepath = path
break
}
}
if choosenFilepath == "" {
return "", errors.New("No suitable file to write to.")
}
err = os.WriteFile(choosenFilepath, content, 0664)
if err != nil {
return "", err
}
return choosenFilepath, nil
}