-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworkspace.go
297 lines (243 loc) · 7.38 KB
/
workspace.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
package main
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/goccy/go-yaml"
"github.com/rs/zerolog/log"
"github.com/yoanm/go-github-tf/core"
)
func readWorkspace(rootPath, configDir, templateDir, yamlAnchorDir string) (*core.Config, error) {
var err error
config := core.NewConfig()
if _, err = os.Stat(rootPath); os.IsNotExist(err) {
return nil, inputDirectoryDoesntExistError(rootPath)
}
configureYamlAnchorDirectory(rootPath, yamlAnchorDir)
decoderOpts := []yaml.DecodeOption{yaml.UseOrderedMap()}
confErr := readConfigDirectory(config, filepath.Join(rootPath, configDir), decoderOpts)
tplErr := readTemplateDirectory(config, filepath.Join(rootPath, templateDir), decoderOpts)
switch {
case confErr != nil && tplErr != nil:
return nil, workspaceLoadingError([]error{confErr, tplErr})
case confErr != nil:
return nil, workspaceLoadingError([]error{confErr})
case tplErr != nil:
return nil, workspaceLoadingError([]error{tplErr})
}
return config, nil
}
func configureYamlAnchorDirectory(path string, yamlAnchorDir string) {
anchorDir := filepath.Join(path, yamlAnchorDir)
fs, err := os.Stat(anchorDir)
exists := !os.IsNotExist(err)
isDir := exists && err == nil && fs.IsDir()
if !exists || !isDir {
return
}
core.YamlAnchorDirectory = &anchorDir
}
func readConfigDirectory(config *core.Config, rootPath string, decoderOpts []yaml.DecodeOption) error {
if _, err := os.Stat(rootPath); os.IsNotExist(err) {
// Nothing to do
return nil
}
var (
files []os.DirEntry
readErr error
)
if files, readErr = os.ReadDir(rootPath); readErr != nil {
return configDirectoryLoadingError([]error{readErr})
}
filenames := make([]string, 0, len(files))
for _, file := range files {
filenames = append(filenames, file.Name())
}
return loadConfigDirectoryFiles(config, rootPath, decoderOpts, filenames)
}
func loadConfigDirectoryFiles(
config *core.Config,
rootPath string,
decoderOpts []yaml.DecodeOption,
filenames []string,
) error {
visited := map[string]string{}
errList := map[string]error{}
for _, filename := range filenames {
loadConfigDirectoryFile(config, filename, filepath.Join(rootPath, filename), decoderOpts, errList, visited)
}
uniqRepoList := map[string]string{}
for fName, repoName := range visited {
if firstFName, ok := uniqRepoList[repoName]; ok {
errList[fName] = alreadyImportedRepositoryError(repoName, []string{fName, firstFName})
} else {
uniqRepoList[repoName] = fName
}
}
if len(errList) > 0 {
return configDirectoryLoadingError(core.MapToSortedList(errList))
}
return nil
}
func loadConfigDirectoryFile(
config *core.Config,
filename string,
path string,
decoderOpts []yaml.DecodeOption,
errList map[string]error,
visited map[string]string,
) {
switch {
case filename == "repos.yaml" || filename == "repos.yml":
loadReposConfigFile(config, filename, path, decoderOpts, errList, visited)
case filename == "repos":
loadReposConfigDirectory(config, path, decoderOpts, errList, visited)
default:
log.Debug().Msgf("%s is not a known file or directory => ignored", path)
}
}
func loadReposConfigDirectory(
config *core.Config,
path string,
decoderOpts []yaml.DecodeOption,
errList map[string]error,
visited map[string]string,
) {
subVisited, loadErrList := readRepositoryDirectory(config, path, decoderOpts)
for k, v := range loadErrList {
errList[k] = v
}
for k, v := range subVisited {
visited[k] = v
}
}
func loadReposConfigFile(
config *core.Config,
filename string,
path string,
decoderOpts []yaml.DecodeOption,
errList map[string]error,
visited map[string]string,
) {
repoConfigs, loadErr := core.LoadRepositoriesFromFile(path, decoderOpts...)
if loadErr != nil {
errList[filename] = loadErr
} else {
log.Debug().Msgf("Loaded '%s' as repositories config", path)
for k, v := range repoConfigs {
config.AppendRepo(v)
visited[fmt.Sprintf("%s[%d]", path, k)] = *v.Name
}
}
}
func readRepositoryDirectory(
config *core.Config,
rootPath string,
decoderOpts []yaml.DecodeOption,
) (map[string]string, map[string]error) {
dirName := filepath.Base(rootPath)
files, readErr := os.ReadDir(rootPath)
if readErr != nil {
return nil, map[string]error{dirName: readErr}
}
errList := map[string]error{}
visited := map[string]string{}
log.Debug().Msgf("Reading repository directory: %s", rootPath)
for _, file := range files {
filePath := filepath.Join(rootPath, file.Name())
ext := filepath.Ext(file.Name())
if ext == ".yml" || ext == ".yaml" {
repoConfig, loadErr := core.LoadRepositoryFromFile(filePath, decoderOpts...)
if loadErr != nil {
errList[filePath] = loadErr
} else {
log.Debug().Msgf("Loaded '%s' as repository config", filePath)
config.AppendRepo(repoConfig)
visited[filePath] = *repoConfig.Name
}
} else {
log.Debug().Msgf("%s is not a YAML template => ignored", filePath)
}
}
return visited, errList
}
func readTemplateDirectory(
config *core.Config,
rootPath string,
decoderOpts []yaml.DecodeOption,
) error {
if _, err := os.Stat(rootPath); os.IsNotExist(err) {
// Nothing to do
return nil
}
files, readErr := os.ReadDir(rootPath)
if readErr == nil {
log.Debug().Msgf("Reading template directory: %s", rootPath)
errList := map[string]error{}
for _, file := range files {
readTemplateDirectoryFile(config, filepath.Join(rootPath, file.Name()), decoderOpts, errList)
}
if len(errList) > 0 {
return templateLoadingError(core.MapToSortedList(errList))
}
} else {
return templateLoadingError([]error{readErr})
}
return nil
}
func readTemplateDirectoryFile(
config *core.Config,
path string,
decoderOpts []yaml.DecodeOption,
errList map[string]error,
) {
ext := filepath.Ext(path)
if ext == ".yml" || ext == ".yaml" {
loadErr := loadTemplateFromFile(config, path, decoderOpts)
if loadErr != nil {
errList[path] = loadErr
}
} else {
log.Debug().Msgf("%s is not a YAML template => ignored", path)
}
}
func loadTemplateFromFile(config *core.Config, filePath string, decoderOpts []yaml.DecodeOption) error {
filename := filepath.Base(filePath)
ext := filepath.Ext(filename)
tplName := filename[:strings.LastIndex(filename, ext)]
switch {
case strings.HasSuffix(tplName, ".repo"):
tplName = strings.TrimSuffix(tplName, ".repo")
tpl, err := core.LoadRepositoryTemplateFromFile(filePath, decoderOpts...)
if err != nil {
//nolint:wrapcheck // Expected to return error as is
return err
}
config.Templates.Repos[tplName] = tpl
log.Debug().Msgf("Loaded '%s' as repository template", filePath)
case strings.HasSuffix(tplName, ".branch-protection"):
tplName = strings.TrimSuffix(tplName, ".branch-protection")
tpl, err := core.LoadBranchProtectionTemplateFromFile(filePath, decoderOpts...)
if err != nil {
//nolint:wrapcheck // Expected to return error as is
return err
} else {
config.Templates.BranchProtections[tplName] = tpl
log.Debug().Msgf("Loaded '%s' as branch protection template", filePath)
}
case strings.HasSuffix(tplName, ".branch"):
tplName = strings.TrimSuffix(tplName, ".branch")
tpl, err := core.LoadBranchTemplateFromFile(filePath, decoderOpts...)
if err != nil {
//nolint:wrapcheck // Expected to return error as is
return err
} else {
config.Templates.Branches[tplName] = tpl
log.Debug().Msgf("Loaded '%s' as branch protection template", filePath)
}
default:
log.Debug().Msgf("%s is not a known template type => ignored", filePath)
}
return nil
}