-
Notifications
You must be signed in to change notification settings - Fork 9
/
phase2.go
98 lines (77 loc) · 2.59 KB
/
phase2.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// 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 cuefrontend
import (
"context"
"strings"
"cuelang.org/go/cue"
"namespacelabs.dev/foundation/internal/fnerrors"
"namespacelabs.dev/foundation/internal/frontend/cuefrontend/args"
"namespacelabs.dev/foundation/internal/frontend/fncue"
"namespacelabs.dev/foundation/internal/uniquestrings"
"namespacelabs.dev/foundation/schema"
"namespacelabs.dev/foundation/std/pkggraph"
)
type phase2plan struct {
owner schema.PackageName
partial *fncue.Partial
Value *fncue.CueV
Left []fncue.KeyAndPath // injected values left to be filled.
}
type cueStartupPlan struct {
Args *args.ArgsListOrMap `json:"args"`
Env *args.EnvMap `json:"env"`
}
var _ pkggraph.PreStartup = phase2plan{}
func (s phase2plan) EvalStartup(ctx context.Context, env pkggraph.Context, info pkggraph.StartupInputs, allocs []pkggraph.ValueWithPath) (*schema.StartupPlan, error) {
plan := &schema.StartupPlan{}
res, err := fncue.SerializedEval(s.partial, func() (*fncue.CueV, error) {
res, _, err := s.evalStartupStage(ctx, env, info)
if err != nil {
return nil, err
}
for _, alloc := range allocs {
res = res.FillPath(cue.ParsePath(alloc.Need.CuePath), alloc.Value)
}
return res, nil
})
if err != nil {
return nil, err
}
if v := lookupTransition(res, "startup"); v.Exists() {
if err := v.Val.Validate(cue.Concrete(true)); err != nil {
return nil, err
}
var raw cueStartupPlan
if err := v.Val.Decode(&raw); err != nil {
return nil, err
}
envVar, err := raw.Env.Parsed(ctx, nil, s.owner)
if err != nil {
return nil, err
}
plan.Env = envVar
plan.Args = raw.Args.Parsed()
}
return plan, nil
}
func (s phase2plan) evalStartupStage(ctx context.Context, wenv pkggraph.Context, info pkggraph.StartupInputs) (*fncue.CueV, []fncue.KeyAndPath, error) {
inputs := newFuncs().
WithFetcher(fncue.ServerDepIKw, FetchServer(wenv, info.Stack)).
WithFetcher(fncue.VCSIKw, func(ctx context.Context, v cue.Value) (interface{}, error) {
return nil, fnerrors.BadInputError("inputs.#VCS is no longer supported, please update your foundation dependency")
})
vv, left, err := applyInputs(ctx, inputs, s.Value, s.Left)
if err != nil {
return nil, nil, err
}
if len(left) > 0 {
var keys uniquestrings.List
for _, kv := range left {
keys.Add(kv.Key)
}
return nil, nil, fnerrors.InternalError("inputs not provisioned: %s", strings.Join(keys.Strings(), ", "))
}
return vv, left, err
}