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 eaa2258 commit 8613d8e
Show file tree
Hide file tree
Showing 3 changed files with 41 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={{ .secondoption }}{{ else }}{{ removeLine }}{{ end }}"
31 changes: 30 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 @@ -88,6 +92,27 @@ func (err errMissingVar) Error() string {
return fmt.Sprintf("the following variables were used but never defined: %v", f)
}

// actionTplFuncs defined template functions available during parsing of an action yaml.
func actionTplFuncs() template.FuncMap {
return template.FuncMap{
// Checks if a value is nil. Used in conditions.
"isNil": func(v any) bool {
return v == nil
},
// Removes a line if a given value is nil or pass through.
"removeLineIfNil": func(v any) any {
if v == nil {
return tokenRmLine
}
return v
},
// Removes current line.
"removeLine": func() string {
return tokenRmLine
},
}
}

func (p inputProcessor) Process(ctx LoadContext, b []byte) ([]byte, error) {
if ctx.Action == nil {
return b, nil
Expand All @@ -99,7 +124,8 @@ 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(actionTplFuncs())

_, err := tpl.Parse(string(b))
if err != nil {
// Check if variables have dashes to show the error properly.
Expand Down Expand Up @@ -141,6 +167,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 }}" # Piping with new line
- "{{ if not (isNil .arg1) }}arg1 is not nil{{end}}"
- "{{ if isNil .optUnd }}{{ removeLine }}{{ end }}" # Function call 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 8613d8e

Please sign in to comment.