-
Notifications
You must be signed in to change notification settings - Fork 0
/
glob.go
487 lines (408 loc) · 13.4 KB
/
glob.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
package importer
import (
"fmt"
"net/url"
"path"
"path/filepath"
"sort"
"strings"
"github.com/bmatcuk/doublestar/v4"
"github.com/dominikbraun/graph"
"github.com/google/go-jsonnet"
"github.com/spf13/afero"
"go.uber.org/zap"
)
type (
// GlobImporter can be used to allow import-paths with glob patterns inside.
// Continuous imports are also possible and allow glob pattern in resolved
// file/contents.
// Activate the glob-import via the following prefixa in front of the import
// path definition (see README file):
// - `glob.<?>://`, where <?> can be one of [path, file, dir, stem]
// - `glob.<?>+://`, where <?> can be one of [file, dir, stem]
// - `glob+://`
//
// For `glob.<?>://` all resolved files will stored under its
// path, file(name), dir(name), stem (filename without extension). If multiple
// files would fit for the file, dirs or stem, only the last one will be used.
// Example:
// - Folders/files:
// - a.libsonnet
// - subfolder/a.libsonnet
// - Import path:
// - import 'glob.stem://**/*.libsonnet'
// - Result:
// {
// a: (import 'subfolder/a.libsonnet');
// }
//
GlobImporter struct {
// JPaths stores extra search paths.
JPaths []string
// A FileSystem abstraction; useful for tests
fs afero.Fs
logger *zap.Logger
importGraph graph.Graph[string, string]
importCounter int
// used in the CanHandle() and to store a possible alias.
prefixa map[string]string
aliases map[string]string
// excludePattern is used in the GlobImporter to ignore files matching
// the given pattern in '.gitIgnore' .
excludePattern string
}
// orderedMap takes the glob.<?>:// and glob.<?>+:// results,
// unifies them & keeps the order.
orderedMap struct {
items map[string][]string
keys []string
}
// hierachically sort the resolved files.
hierachically []string
)
func (s hierachically) Len() int {
return len(s)
}
func (s hierachically) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func (s hierachically) Less(i, j int) bool {
s1 := strings.ReplaceAll(s[i], "/", "\x00")
s2 := strings.ReplaceAll(s[j], "/", "\x00")
return s1 < s2
}
// newOrderedMap initialize a new orderedMap.
func newOrderedMap() *orderedMap {
return &orderedMap{
items: make(map[string][]string),
keys: []string{},
}
}
// add is used to either really add a single value to a key or with `extend == true`
// extend the list of values with the new value for the given key.
func (o *orderedMap) add(key, value string, extend bool) {
item, exists := o.items[key]
switch {
case !exists:
o.keys = append(o.keys, key)
o.items[key] = []string{value}
case extend:
o.items[key] = append(item, value)
case !extend:
o.items[key] = []string{value}
}
}
// NewGlobImporter returns a GlobImporter with default prefixa.
func NewGlobImporter(jpaths ...string) *GlobImporter {
return &GlobImporter{
prefixa: map[string]string{
"glob.path": "",
"glob.path+": "",
"glob-str.path": "",
"glob-str.path+": "",
"glob.file": "",
"glob.file+": "",
"glob-str.file": "",
"glob-str.file+": "",
"glob.dir": "",
"glob.dir+": "",
"glob-str.dir": "",
"glob-str.dir+": "",
"glob.stem": "",
"glob.stem+": "",
"glob-str.stem": "",
"glob-str.stem+": "",
"glob+": "",
"glob-str+": "",
},
aliases: make(map[string]string),
logger: zap.New(nil),
JPaths: jpaths,
excludePattern: "",
importGraph: graph.New(graph.StringHash, graph.Tree(), graph.Directed(), graph.PreventCycles()),
importCounter: 0,
fs: afero.NewOsFs(),
}
}
func (g *GlobImporter) setImportGraph(importGraph graph.Graph[string, string], importCounter int) {
g.importGraph = importGraph
g.importCounter = importCounter
}
func (g *GlobImporter) Exclude(pattern string) {
g.excludePattern = pattern
}
// AddAliasPrefix binds a given alias to a given prefix. This prefix must exist
// and only one alias per prefix is possible. An alias must have the suffix
// "://".
func (g *GlobImporter) AddAliasPrefix(alias, prefix string) error {
if _, exists := g.prefixa[prefix]; !exists {
return fmt.Errorf("%w '%s'", ErrUnknownPrefix, prefix)
}
g.prefixa[prefix] = alias
g.aliases[alias] = prefix
return nil
}
// Logger can be used to set the zap.Logger for the GlobImporter.
func (g *GlobImporter) Logger(logger *zap.Logger) {
if logger != nil {
g.logger = logger
}
}
// CanHandle implements the interface method of the Importer and returns true,
// if the path has on of the supported prefixa. Run <Importer>.Prefixa() to get
// the supported prefixa.
func (g GlobImporter) CanHandle(path string) bool {
for k, v := range g.prefixa {
if strings.HasPrefix(path, k) || (strings.HasPrefix(path, v) && len(v) > 0) {
return true
}
}
return false
}
// Prefixa returns the list of supported prefixa for this importer.
func (g GlobImporter) Prefixa() []string {
return append(stringKeysFromMap(g.prefixa), stringValuesFromMap(g.prefixa)...)
}
// Import implements the go-jsonnet iterface method and converts the resolved
// paths into readable paths for the original go-jsonnet FileImporter.
func (g *GlobImporter) Import(importedFrom, importedPath string) (jsonnet.Contents, string, error) {
logger := g.logger.Named("GlobImporter")
logger.Debug("Import()",
zap.String("importedFrom", importedFrom),
zap.String("importedPath", importedPath),
zap.Strings("jpaths", g.JPaths),
)
contents := jsonnet.MakeContents("")
// Hack-ish !!!:
// The resolved glob-imports are still found inside the same file (importedFrom)
// But the "foundAt" value is not allowed to be the same for multiple importer runs,
// causing different contents.
// Related:
// - https://github.com/google/go-jsonnet/issues/349
// - https://github.com/google/go-jsonnet/issues/374
// - https://github.com/google/go-jsonnet/issues/329
// So I have to put for example a simple self-reference './' in front of the "importedFrom" path
// to fake the foundAt value. (tried multiple things, but even flushing the importerCache of
// the VM via running vm.Importer(...) again, couldn't solve this)
p := strings.Repeat("./", g.importCounter)
foundAt := p + "./" + importedFrom
prefix, pattern, err := g.parse(importedPath)
if err != nil {
return contents, foundAt, err
}
// this is the path of the import caller
cwd, _ := filepath.Split(importedFrom)
cwd = filepath.Clean(cwd)
logger.Debug("parsed parameters from importedPath",
zap.String("prefix", prefix),
zap.String("pattern", pattern),
zap.String("cwd", cwd),
)
// g.JPaths will be used first, before the cwd - this will give cwd higher
// priority at the end.
resolvedFiles, err := g.resolveFilesFrom(g.JPaths, cwd, pattern)
if err != nil {
return contents, foundAt, err
}
logger.Debug("glob library returns", zap.Strings("files", resolvedFiles))
files := []string{}
afiles := allowedFiles(resolvedFiles, importedFrom)
basepath, _ := filepath.Split(importedFrom)
if err := g.importGraph.AddVertex(importedPath,
graph.VertexAttribute("shape", "rect"),
graph.VertexAttribute("style", "dashed"),
graph.VertexAttribute("color", "grey"),
graph.VertexAttribute("fontcolor", "grey"),
); err != nil {
logger.Warn(err.Error())
}
for _, f := range afiles {
relf, _ := filepath.Rel(basepath, f)
files = append(files, relf)
if err := g.importGraph.AddVertex(relf,
graph.VertexAttribute("shape", "rect"),
graph.VertexAttribute("color", "grey"),
graph.VertexAttribute("fontcolor", "grey"),
graph.VertexAttribute("style", "dashed"),
); err != nil {
logger.Warn(err.Error())
}
if err := g.importGraph.AddEdge(importedPath, relf,
graph.EdgeAttribute("color", "grey"),
graph.EdgeAttribute("style", "dashed"),
graph.EdgeWeight(g.importCounter),
); err != nil {
logger.Warn(err.Error())
}
}
joinedImports, err := g.handle(files, prefix)
if err != nil {
return contents, foundAt, err
}
contents = jsonnet.MakeContents(joinedImports)
logger.Debug("returns", zap.String("contents", joinedImports), zap.String("foundAt", foundAt))
return contents, foundAt, nil
}
// resolveFilesFrom takes a list of paths together with a glob pattern
// and returns the output of the used doublestar.Glob function.
func (g *GlobImporter) resolveFilesFrom(searchPaths []string, cwd, pattern string) ([]string, error) {
executeGlob := func(dir, pattern string) (matches []string, err error) {
pathPattern := filepath.Join(dir, pattern)
pathPattern = filepath.Clean(pathPattern)
pathPattern = filepath.ToSlash(pathPattern)
base, file := doublestar.SplitPattern(pathPattern)
fs, err := afero.NewIOFS(g.fs).Sub(base)
if err != nil {
return
}
if matches, err = doublestar.Glob(fs, file, doublestar.WithNoFollow(), doublestar.WithFailOnIOErrors()); err != nil {
return
}
for i := range matches {
matches[i] = filepath.FromSlash(path.Join(base, matches[i]))
}
return
}
resolvedFiles := []string{}
for _, p := range searchPaths {
matches, err := executeGlob(p, pattern)
if err != nil {
return []string{}, err
}
resolvedFiles = append(resolvedFiles, matches...)
}
// sort the JPaths results first
sort.Sort(hierachically(resolvedFiles))
// CWD must be last in resolvedFiles
matches, err := executeGlob(cwd, pattern)
if err != nil {
return []string{}, err
}
sort.Sort(hierachically(matches))
resolvedFiles = append(resolvedFiles, matches...)
if len(resolvedFiles) == 0 {
return []string{},
fmt.Errorf("%w for the glob pattern '%s'", ErrEmptyResult, pattern)
}
// handle excludes
if len(g.excludePattern) > 0 {
return g.removeExcludesFrom(resolvedFiles, pattern)
}
return resolvedFiles, nil
}
func (g *GlobImporter) removeExcludesFrom(files []string, pattern string) ([]string, error) {
keep := []string{}
for _, file := range files {
match, err := doublestar.PathMatch(g.excludePattern, file)
if err != nil {
return []string{}, fmt.Errorf("while remove excluded file %s ,error: %w", file, err)
}
if !match {
keep = append(keep, file)
}
}
if len(keep) == 0 {
return []string{},
fmt.Errorf(
"%w, exclude pattern '%s' removed all matches for the glob pattern '%s'",
ErrEmptyResult, g.excludePattern, pattern)
}
return keep, nil
}
func (g *GlobImporter) parse(importedPath string) (string, string, error) {
parsedURL, err := url.Parse(importedPath)
if err != nil {
return "", "",
fmt.Errorf("%w: cannot parse import '%s', error: %w",
ErrMalformedGlobPattern, importedPath, err)
}
prefix := parsedURL.Scheme
pattern := strings.Join([]string{parsedURL.Host, parsedURL.Path}, "/")
query, err := url.ParseQuery(parsedURL.RawQuery)
if err != nil {
return "", "",
fmt.Errorf("%w: cannot parse the query inside the import '%s', error: %w",
ErrMalformedGlobPattern, importedPath, err)
}
if excludePattern, exists := query["exclude"]; exists {
g.excludePattern = excludePattern[0]
}
return prefix, pattern, nil
}
// allowedFiles removes ignoreFile from a given list of files and
// converts the rest via filepath.FromSlash().
// Used to remove self reference of a file to avoid endless loops.
func allowedFiles(files []string, ignoreFile string) []string {
allowedFiles := []string{}
for _, file := range files {
if file == ignoreFile {
continue
}
importPath := filepath.FromSlash(file)
allowedFiles = append(allowedFiles, importPath)
}
return allowedFiles
}
// handle runs the logic behind the different glob prefixa and returns based on
// the prefix the import string.
func (g GlobImporter) handle(files []string, prefix string) (string, error) {
resolvedFiles := newOrderedMap()
// handle import or importstr
importKind := "import"
if strings.HasPrefix(prefix, "glob-str") {
prefix = strings.Replace(prefix, "glob-str", "glob", 1)
importKind += "str"
}
// handle alias prefix
if p, exists := g.aliases[prefix]; exists {
prefix = p
}
switch prefix {
case "glob+":
imports := make([]string, 0, len(files))
for _, f := range files {
i := fmt.Sprintf("(%s '%s')", importKind, f)
imports = append(imports, i)
}
return strings.Join(imports, "+"), nil
case "glob.path", "glob.path+":
imports := make([]string, 0, len(files))
for _, f := range files {
imports = append(imports, fmt.Sprintf("'%s': (%s '%s'),", f, importKind, f))
}
return fmt.Sprintf("{\n%s\n}", strings.Join(imports, "\n")), nil
case "glob.stem", "glob.stem+":
for _, f := range files {
i := fmt.Sprintf("(%s '%s')", importKind, f)
_, filename := filepath.Split(f)
stem, _, _ := strings.Cut(filename, ".")
resolvedFiles.add(stem, i, strings.HasSuffix(prefix, "+"))
}
case "glob.file", "glob.file+":
for _, f := range files {
i := fmt.Sprintf("(%s '%s')", importKind, f)
_, filename := filepath.Split(f)
resolvedFiles.add(filename, i, strings.HasSuffix(prefix, "+"))
}
case "glob.dir", "glob.dir+":
for _, f := range files {
i := fmt.Sprintf("(%s '%s')", importKind, f)
dir, _ := filepath.Split(f)
resolvedFiles.add(dir, i, strings.HasSuffix(prefix, "+"))
}
default:
return "", fmt.Errorf("%w: %s", ErrUnknownPrefix, prefix)
}
return createGlobDotImportsFrom(resolvedFiles), nil
}
// createGlobDotImportsFrom transforms the orderedMap of resolvedFiles
// into the format `{ '<?>': import '...' }`.
func createGlobDotImportsFrom(resolvedFiles *orderedMap) string {
var out strings.Builder
out.WriteString("{\n")
for _, k := range resolvedFiles.keys {
fmt.Fprintf(&out, "'%s': %s,\n", k, strings.Join(resolvedFiles.items[k], "+"))
}
out.WriteString("}")
return out.String()
}