Skip to content

Commit 43e270c

Browse files
committed
#62 Add new processor to skip arguments in declaration
1 parent 7c5ff0d commit 43e270c

File tree

3 files changed

+41
-3
lines changed

3 files changed

+41
-3
lines changed

example/actions/arguments/action.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,5 @@ runtime:
2929
- /action/main.sh
3030
- "{{ .arg1 }}"
3131
- "{{ .arg2 }}"
32-
- "{{ .firstoption }}"
33-
- "{{ .secondoption }}"
32+
- "{{ .firstoption | removeLineIfNil }}"
33+
- "{{ if not (isNil .secondoption) }}--secondoption={{ .secondoption }}{{ else }}{{ removeLine }}{{ end }}"

pkg/action/loader.go

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ import (
1010
"text/template"
1111
)
1212

13+
const tokenRmLine = "<TOKEN_REMOVE_THIS_LINE>"
14+
15+
var rgxTokenRmLine = regexp.MustCompile(`.*` + tokenRmLine + `.*\n?`)
16+
1317
// Loader is an interface for loading an action file.
1418
type Loader interface {
1519
// Content returns the raw file content.
@@ -88,6 +92,27 @@ func (err errMissingVar) Error() string {
8892
return fmt.Sprintf("the following variables were used but never defined: %v", f)
8993
}
9094

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+
91116
func (p inputProcessor) Process(ctx LoadContext, b []byte) ([]byte, error) {
92117
if ctx.Action == nil {
93118
return b, nil
@@ -99,7 +124,8 @@ func (p inputProcessor) Process(ctx LoadContext, b []byte) ([]byte, error) {
99124
addPredefinedVariables(data, a)
100125

101126
// Parse action without variables to validate
102-
tpl := template.New(a.ID)
127+
tpl := template.New(a.ID).Funcs(actionTplFuncs())
128+
103129
_, err := tpl.Parse(string(b))
104130
if err != nil {
105131
// 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 "
141167
}
142168
}
143169

170+
// Remove all lines containing [tokenRmLine].
171+
res = rgxTokenRmLine.ReplaceAll(res, []byte(""))
172+
144173
return res, nil
145174
}
146175

pkg/action/loader_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,15 @@ func Test_InputProcessor(t *testing.T) {
6767
res, err = proc.Process(ctx, []byte(s))
6868
assert.Equal(t, err, errMissingVar{vars: map[string]struct{}{"optUnd": {}, "arg2": {}}})
6969
assert.Equal(t, "", string(res))
70+
71+
// Remove line if a variable not exists or is nil.
72+
s = `- "{{ .arg1 | removeLineIfNil }}"
73+
- "{{ .optUnd | removeLineIfNil }}" # Piping with new line
74+
- "{{ if not (isNil .arg1) }}arg1 is not nil{{end}}"
75+
- "{{ if isNil .optUnd }}{{ removeLine }}{{ end }}" # Function call without new line`
76+
res, err = proc.Process(ctx, []byte(s))
77+
assert.NoError(t, err)
78+
assert.Equal(t, "- \"arg1\"\n- \"arg1 is not nil\"\n", string(res))
7079
}
7180

7281
func Test_YamlTplCommentsProcessor(t *testing.T) {

0 commit comments

Comments
 (0)