forked from argoproj/argo-workflows
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scope.go
79 lines (70 loc) · 1.87 KB
/
scope.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
package controller
import (
"strings"
"github.com/argoproj/argo/errors"
wfv1 "github.com/argoproj/argo/pkg/apis/workflow/v1alpha1"
)
// wfScope contains the current scope of variables available when executing a template
type wfScope struct {
tmpl *wfv1.Template
scope map[string]interface{}
}
// replaceMap returns a replacement map of strings intended to be used simple string substitution
func (s *wfScope) replaceMap() map[string]string {
replaceMap := make(map[string]string)
for key, val := range s.scope {
valStr, ok := val.(string)
if ok {
replaceMap[key] = valStr
}
}
return replaceMap
}
func (s *wfScope) addParamToScope(key, val string) {
s.scope[key] = val
}
func (s *wfScope) addArtifactToScope(key string, artifact wfv1.Artifact) {
s.scope[key] = artifact
}
// resolveVar resolves a parameter or artifact
func (s *wfScope) resolveVar(v string) (interface{}, error) {
v = strings.TrimPrefix(v, "{{")
v = strings.TrimSuffix(v, "}}")
parts := strings.Split(v, ".")
prefix := parts[0]
switch prefix {
case "steps", "tasks", "workflow":
val, ok := s.scope[v]
if ok {
return val, nil
}
case "inputs":
art := s.tmpl.Inputs.GetArtifactByName(parts[2])
if art != nil {
return *art, nil
}
}
return nil, errors.Errorf(errors.CodeBadRequest, "Unable to resolve: {{%s}}", v)
}
func (s *wfScope) resolveParameter(v string) (string, error) {
val, err := s.resolveVar(v)
if err != nil {
return "", err
}
valStr, ok := val.(string)
if !ok {
return "", errors.Errorf(errors.CodeBadRequest, "Variable {{%s}} is not a string", v)
}
return valStr, nil
}
func (s *wfScope) resolveArtifact(v string) (*wfv1.Artifact, error) {
val, err := s.resolveVar(v)
if err != nil {
return nil, err
}
valArt, ok := val.(wfv1.Artifact)
if !ok {
return nil, errors.Errorf(errors.CodeBadRequest, "Variable {{%s}} is not an artifact", v)
}
return &valArt, nil
}