generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 7
/
local_client.go
61 lines (53 loc) · 1.77 KB
/
local_client.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
package admin
import (
"context"
"fmt"
"path/filepath"
"github.com/TBD54566975/ftl/backend/schema"
"github.com/TBD54566975/ftl/buildengine"
"github.com/TBD54566975/ftl/common/configuration"
"github.com/TBD54566975/ftl/common/projectconfig"
"github.com/alecthomas/types/optional"
)
// localClient reads and writes to local projectconfig files without making any network
// calls. It allows us to interface with local ftl-project.toml files without needing to
// start a controller.
type localClient struct {
*AdminService
}
type diskSchemaRetriever struct {
// Omit to use the project root as the deploy root.
deployRoot optional.Option[string]
}
func newLocalClient(ctx context.Context) *localClient {
cm := configuration.ConfigFromContext(ctx)
sm := configuration.SecretsFromContext(ctx)
return &localClient{NewAdminService(cm, sm, &diskSchemaRetriever{})}
}
func (s *diskSchemaRetriever) GetActiveSchema(ctx context.Context) (*schema.Schema, error) {
path, ok := projectconfig.DefaultConfigPath().Get()
if !ok {
return nil, fmt.Errorf("no project config path available")
}
projConfig, err := projectconfig.Load(ctx, path)
if err != nil {
return nil, fmt.Errorf("could not load project config: %w", err)
}
modules, err := buildengine.DiscoverModules(ctx, projConfig.AbsModuleDirs())
if err != nil {
return nil, fmt.Errorf("could not discover modules: %w", err)
}
sch := &schema.Schema{}
for _, m := range modules {
schemaPath := m.Config.Abs().Schema
if r, ok := s.deployRoot.Get(); ok {
schemaPath = filepath.Join(r, m.Config.Module, m.Config.DeployDir, m.Config.Schema)
}
module, err := schema.ModuleFromProtoFile(schemaPath)
if err != nil {
return nil, fmt.Errorf("could not load module schema: %w", err)
}
sch.Upsert(module)
}
return sch, nil
}