-
Notifications
You must be signed in to change notification settings - Fork 9
/
list.go
72 lines (56 loc) · 1.72 KB
/
list.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
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package parsing
import (
"context"
"fmt"
"io/fs"
"path/filepath"
"namespacelabs.dev/foundation/internal/console"
"namespacelabs.dev/foundation/internal/fnfs"
"namespacelabs.dev/foundation/internal/workspace/dirs"
"namespacelabs.dev/foundation/std/cfg"
)
type SchemaList struct {
Root *Root
Locations []fnfs.Location
Types []PackageType
}
// Returns a list of all of the schema definitions found under root.
func ListSchemas(ctx context.Context, env cfg.Context, root *Root) (SchemaList, error) {
sl := SchemaList{Root: root}
pl := NewPackageLoader(env)
visited := map[string]struct{}{} // Map of directory name to presence.
if err := fnfs.WalkDir(root.ReadOnlyFS(), ".", func(path string, d fs.DirEntry) error {
if d.IsDir() {
if dirs.IsExcludedAsSource(path) || dirs.IsHidden(d.Name()) {
// Schemas are not allowed to exist in hidden directories.
return fs.SkipDir
}
return nil
}
// Is there a least a .cue file in the directory?
if filepath.Ext(d.Name()) == ".cue" {
dir := filepath.Dir(path)
if _, ok := visited[dir]; ok {
return nil
}
pkg := root.RelPackage(dir)
ptype, err := pl.frontend.GuessPackageType(ctx, pkg.AsPackageName())
if err != nil {
fmt.Fprintf(console.Stderr(ctx), "failed to parse %s: %v\n", dir, err)
return nil
}
if ptype != PackageType_None {
sl.Locations = append(sl.Locations, pkg)
sl.Types = append(sl.Types, ptype)
}
visited[dir] = struct{}{}
}
return nil
}); err != nil {
return SchemaList{}, err
}
return sl, nil
}