-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwith.go
75 lines (59 loc) · 1.62 KB
/
with.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
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2024-Present Harry Randazzo
package vai
import (
"context"
"fmt"
"runtime"
"github.com/charmbracelet/log"
"github.com/d5/tengo/v2"
)
// WithEntry is a single entry in a With map
type WithEntry any
// With is a map of string keys and WithEntry values used to pass parameters to called tasks and within steps
//
// Each key will be mapped to an equivalent environment variable
// when the command is run. eg. `with: {foo: bar}` will be passed
// as `foo=bar` to the command.
type With map[string]WithEntry
// PerformLookups evaluates the expressions in the local With map
func PerformLookups(ctx context.Context, outer, local With, previousOutputs CommandOutputs) (With, error) {
if len(local) == 0 {
return local, nil
}
logger := log.FromContext(ctx)
logger.Debug("templating", "input", outer, "local", local)
r := make(With, len(local))
for k, v := range local {
val, ok := v.(string)
if !ok {
r[k] = v
continue
}
env := map[string]interface{}{
"os": runtime.GOOS,
"arch": runtime.GOARCH,
"platform": fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH),
"input": outer[k],
}
steps := map[string]tengo.Object{}
for k, v := range previousOutputs {
obj, err := tengo.FromInterface(v)
if err != nil {
return nil, err
}
steps[k] = obj
}
env["steps"] = steps
out, err := tengo.Eval(ctx, val, env)
if err != nil {
return nil, err
}
if out == nil {
return nil, fmt.Errorf("expression evaluated to <nil>:\n\t%s", val)
}
r[k] = out
}
logger.Debug("templated", "result", r)
return r, nil
}