Skip to content

Commit

Permalink
#62 Add new processor to skip arguments in declaration
Browse files Browse the repository at this point in the history
  • Loading branch information
lexbritvin committed Feb 21, 2025
1 parent f5567a3 commit 8b305f7
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
4 changes: 2 additions & 2 deletions example/actions/arguments/action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ runtime:
command:
- sh
- /action/main.sh
- "{{ .firstoption }}"
- "{{ .secondoption }}"
- "{{ .firstoption | removeLineIfNil }}"
- "{{ if not (isNil .secondoption) }}{{ .secondoption }}{{ end }}"
20 changes: 19 additions & 1 deletion pkg/action/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import (
"text/template"
)

const tokenRmLine = "<TOKEN_REMOVE_THIS_LINE>"

var rgxTokenRmLine = regexp.MustCompile(`.*` + tokenRmLine + `.*\n?`)

// Loader is an interface for loading an action file.
type Loader interface {
// Content returns the raw file content.
Expand Down Expand Up @@ -99,7 +103,18 @@ func (p inputProcessor) Process(ctx LoadContext, b []byte) ([]byte, error) {
addPredefinedVariables(data, a)

// Parse action without variables to validate
tpl := template.New(a.ID)
tpl := template.New(a.ID).Funcs(template.FuncMap{
"isNil": func(v any) bool {
return v == nil
},
"removeLineIfNil": func(v any) any {
if v == nil {
return tokenRmLine
}
return v
},
})

_, err := tpl.Parse(string(b))
if err != nil {
// Check if variables have dashes to show the error properly.
Expand Down Expand Up @@ -141,6 +156,9 @@ Action definition is correct, but dashes are not allowed in templates, replace "
}
}

// Remove all lines containing [tokenRmLine].
res = rgxTokenRmLine.ReplaceAll(res, []byte(""))

return res, nil
}

Expand Down
9 changes: 9 additions & 0 deletions pkg/action/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,15 @@ func Test_InputProcessor(t *testing.T) {
res, err = proc.Process(ctx, []byte(s))
assert.Equal(t, err, errMissingVar{vars: map[string]struct{}{"optUnd": {}, "arg2": {}}})
assert.Equal(t, "", string(res))

// Remove line if a variable not exists or is nil.
s = `- "{{ .arg1 | removeLineIfNil }}"
- "{{ .optUnd | removeLineIfNil }}" # With new line
- "{{ if not (isNil .arg1) }}arg1 is not nil{{end}}"
- "{{ .optUnd | removeLineIfNil }}" # Without new line`
res, err = proc.Process(ctx, []byte(s))
assert.NoError(t, err)
assert.Equal(t, "- \"arg1\"\n- \"arg1 is not nil\"\n", string(res))
}

func Test_YamlTplCommentsProcessor(t *testing.T) {
Expand Down

0 comments on commit 8b305f7

Please sign in to comment.