generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 7
/
discover.go
62 lines (56 loc) · 1.39 KB
/
discover.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
package buildengine
import (
"context"
"io/fs"
"os"
"path/filepath"
"sort"
"github.com/TBD54566975/ftl/internal/log"
)
// DiscoverModules recursively loads all modules under the given directories
// (or if none provided, the current working directory is used).
func DiscoverModules(ctx context.Context, moduleDirs []string) ([]Module, error) {
out := []Module{}
logger := log.FromContext(ctx)
modules, err := discoverModules(moduleDirs...)
if err != nil {
logger.Tracef("error discovering modules: %v", err)
return nil, err
}
out = append(out, modules...)
return out, nil
}
// discoverModules recursively loads all modules under the given directories.
//
// If no directories are provided, the current working directory is used.
func discoverModules(dirs ...string) ([]Module, error) {
if len(dirs) == 0 {
cwd, err := os.Getwd()
if err != nil {
return nil, err
}
dirs = []string{cwd}
}
out := []Module{}
for _, dir := range dirs {
err := WalkDir(dir, func(path string, d fs.DirEntry) error {
if filepath.Base(path) != "ftl.toml" {
return nil
}
moduleDir := filepath.Dir(path)
module, err := LoadModule(moduleDir)
if err != nil {
return err
}
out = append(out, module)
return ErrSkip
})
if err != nil {
return nil, err
}
}
sort.Slice(out, func(i, j int) bool {
return out[i].Config.Module < out[j].Config.Module
})
return out, nil
}