forked from runabol/tork
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheval.go
145 lines (136 loc) · 2.91 KB
/
eval.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package eval
import (
"bytes"
"fmt"
"regexp"
"github.com/antonmedv/expr"
"github.com/pkg/errors"
"github.com/runabol/tork/task"
)
var exprMatcher = regexp.MustCompile(`{{\s*(.+?)\s*}}`)
func EvaluateTask(t *task.Task, c map[string]any) error {
// evaluate name
name, err := EvaluateTemplate(t.Name, c)
if err != nil {
return err
}
t.Name = name
// evaluate var
var_, err := EvaluateTemplate(t.Var, c)
if err != nil {
return err
}
t.Var = var_
// evaluate image
img, err := EvaluateTemplate(t.Image, c)
if err != nil {
return err
}
t.Image = img
// evaluate queue
q, err := EvaluateTemplate(t.Queue, c)
if err != nil {
return err
}
t.Queue = q
// evaluate the env vars
env := t.Env
for k, v := range env {
result, err := EvaluateTemplate(v, c)
if err != nil {
return err
}
env[k] = result
}
t.Env = env
// evaluate if expr
ifExpr, err := EvaluateTemplate(t.If, c)
if err != nil {
return err
}
t.If = ifExpr
// evaluate pre-tasks
pres := make([]*task.Task, len(t.Pre))
for i, pre := range t.Pre {
if err := EvaluateTask(pre, c); err != nil {
return err
}
pres[i] = pre
}
t.Pre = pres
// evaluate post-tasks
posts := make([]*task.Task, len(t.Post))
for i, post := range t.Post {
if err := EvaluateTask(post, c); err != nil {
return err
}
posts[i] = post
}
t.Post = posts
// evaluate parallel tasks
if t.Parallel != nil {
parallel := make([]*task.Task, len(t.Parallel.Tasks))
for i, par := range t.Parallel.Tasks {
if err := EvaluateTask(par, c); err != nil {
return err
}
parallel[i] = par
}
t.Parallel.Tasks = parallel
}
return nil
}
func EvaluateTemplate(ex string, c map[string]any) (string, error) {
if ex == "" {
return "", nil
}
loc := 0
var buf bytes.Buffer
for _, match := range exprMatcher.FindAllStringSubmatchIndex(ex, -1) {
startTag := match[0]
endTag := match[1]
startExpr := match[2]
endExpr := match[3]
buf.WriteString(ex[loc:startTag])
ev, err := EvaluateExpr(ex[startExpr:endExpr], c)
if err != nil {
return "", err
}
buf.WriteString(fmt.Sprintf("%v", ev))
loc = endTag
}
buf.WriteString(ex[loc:])
return buf.String(), nil
}
func ValidExpr(ex string) bool {
ex = sanitizeExpr(ex)
_, err := expr.Compile(ex)
return err == nil
}
func sanitizeExpr(ex string) string {
if matches := exprMatcher.FindStringSubmatch(ex); matches != nil {
return matches[1]
}
return ex
}
func EvaluateExpr(ex string, c map[string]any) (any, error) {
ex = sanitizeExpr(ex)
env := map[string]any{
"randomInt": randomInt,
"coinflip": coinflip,
"range": range_,
"parseJSON": parseJSON,
}
for k, v := range c {
env[k] = v
}
program, err := expr.Compile(ex, expr.Env(env))
if err != nil {
return "", errors.Wrapf(err, "error compiling expression: %s", ex)
}
output, err := expr.Run(program, env)
if err != nil {
return "", errors.Wrapf(err, "error evaluating expression: %s", ex)
}
return output, nil
}