-
Notifications
You must be signed in to change notification settings - Fork 8
/
compute.go
49 lines (38 loc) · 1.66 KB
/
compute.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
// 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 startup
import (
"context"
"namespacelabs.dev/foundation/internal/fnerrors"
"namespacelabs.dev/foundation/internal/planning"
"namespacelabs.dev/foundation/internal/support"
"namespacelabs.dev/foundation/schema"
"namespacelabs.dev/foundation/std/pkggraph"
)
func ComputeConfig(ctx context.Context, env pkggraph.Context, serverStartupPlan *schema.StartupPlan, deps []*planning.ParsedNode, info pkggraph.StartupInputs) (*schema.BinaryConfig, error) {
computed := &schema.BinaryConfig{}
// For each already loaded configuration, unify the startup args to produce the final startup configuration.
for _, dep := range deps {
if err := loadStartupPlan(ctx, env, dep, info, computed); err != nil {
return nil, fnerrors.NewWithLocation(dep.Package.Location, "computing startup config: %w", err)
}
}
if err := mergePlan(serverStartupPlan, computed); err != nil {
return nil, err
}
return computed, nil
}
func loadStartupPlan(ctx context.Context, env pkggraph.Context, dep *planning.ParsedNode, info pkggraph.StartupInputs, merged *schema.BinaryConfig) error {
plan, err := dep.ProvisionPlan.Startup.EvalStartup(ctx, env, info, dep.Allocations)
if err != nil {
return fnerrors.AttachLocation(dep.Package.Location, err)
}
return mergePlan(plan, merged)
}
func mergePlan(plan *schema.StartupPlan, merged *schema.BinaryConfig) error {
merged.Args = append(merged.Args, plan.Args...)
var err error
merged.Env, err = support.MergeEnvs(merged.Env, plan.Env)
return err
}