@@ -10,6 +10,10 @@ import (
10
10
"text/template"
11
11
)
12
12
13
+ const tokenRmLine = "<TOKEN_REMOVE_THIS_LINE>"
14
+
15
+ var rgxTokenRmLine = regexp .MustCompile (`.*` + tokenRmLine + `.*\n?` )
16
+
13
17
// Loader is an interface for loading an action file.
14
18
type Loader interface {
15
19
// Content returns the raw file content.
@@ -88,6 +92,27 @@ func (err errMissingVar) Error() string {
88
92
return fmt .Sprintf ("the following variables were used but never defined: %v" , f )
89
93
}
90
94
95
+ // actionTplFuncs defined template functions available during parsing of an action yaml.
96
+ func actionTplFuncs () template.FuncMap {
97
+ return template.FuncMap {
98
+ // Checks if a value is nil. Used in conditions.
99
+ "isNil" : func (v any ) bool {
100
+ return v == nil
101
+ },
102
+ // Removes a line if a given value is nil or pass through.
103
+ "removeLineIfNil" : func (v any ) any {
104
+ if v == nil {
105
+ return tokenRmLine
106
+ }
107
+ return v
108
+ },
109
+ // Removes current line.
110
+ "removeLine" : func () string {
111
+ return tokenRmLine
112
+ },
113
+ }
114
+ }
115
+
91
116
func (p inputProcessor ) Process (ctx LoadContext , b []byte ) ([]byte , error ) {
92
117
if ctx .Action == nil {
93
118
return b , nil
@@ -99,7 +124,8 @@ func (p inputProcessor) Process(ctx LoadContext, b []byte) ([]byte, error) {
99
124
addPredefinedVariables (data , a )
100
125
101
126
// Parse action without variables to validate
102
- tpl := template .New (a .ID )
127
+ tpl := template .New (a .ID ).Funcs (actionTplFuncs ())
128
+
103
129
_ , err := tpl .Parse (string (b ))
104
130
if err != nil {
105
131
// Check if variables have dashes to show the error properly.
@@ -141,6 +167,9 @@ Action definition is correct, but dashes are not allowed in templates, replace "
141
167
}
142
168
}
143
169
170
+ // Remove all lines containing [tokenRmLine].
171
+ res = rgxTokenRmLine .ReplaceAll (res , []byte ("" ))
172
+
144
173
return res , nil
145
174
}
146
175
0 commit comments