-
Notifications
You must be signed in to change notification settings - Fork 9
/
root.go
47 lines (36 loc) · 1.16 KB
/
root.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
// 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 module
import (
"context"
"path/filepath"
"namespacelabs.dev/foundation/internal/fnerrors"
"namespacelabs.dev/foundation/internal/parsing"
"namespacelabs.dev/foundation/internal/parsing/devhost"
)
func FindRoot(ctx context.Context, dir string) (*parsing.Root, error) {
return FindRootWithArgs(ctx, dir, parsing.ModuleAtArgs{})
}
func FindRootWithArgs(ctx context.Context, dir string, args parsing.ModuleAtArgs) (*parsing.Root, error) {
abs, err := filepath.Abs(dir)
if err != nil {
return nil, err
}
return findWorkspaceRoot(ctx, abs, args)
}
func findWorkspaceRoot(ctx context.Context, dir string, args parsing.ModuleAtArgs) (*parsing.Root, error) {
path, err := parsing.FindModuleRoot(dir)
if err != nil {
return nil, fnerrors.New("workspace: %w", err)
}
data, err := parsing.ModuleAt(ctx, path, args)
if err != nil {
return nil, err
}
r := parsing.NewRoot(data, data)
if err := devhost.Prepare(ctx, r); err != nil {
return r, err
}
return r, nil
}