Skip to content

Commit 6cf76a1

Browse files
committed
#62: add new processors
1 parent 2dc982e commit 6cf76a1

File tree

4 files changed

+158
-4
lines changed

4 files changed

+158
-4
lines changed

docs/actions.schema.md

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,3 +274,82 @@ plasma
274274
+ id
275275
uid=1000(plasma) gid=1000(plasma) groups=1000(plasma)
276276
```
277+
278+
279+
## Available Command Template Functions
280+
281+
### `removeLine`
282+
**Description:** A special template directive that removes the entire line from the final output.
283+
284+
**Usage:**
285+
286+
``` yaml
287+
- "{{ if condition }}value{{ else }}{{ removeLine }}{{ end }}"
288+
```
289+
290+
### `isNil`
291+
292+
**Description:** Checks if a value is nil.
293+
294+
**Usage:**
295+
296+
```yaml
297+
- "{{ if not (isNil .param_name) }}--param={{ .param_name }}{{ else }}{{ removeLine }}{{ end }}"
298+
```
299+
300+
### `isSet`
301+
302+
**Description:** Checks if a value has been set (opposite of `isNil`).
303+
304+
```yaml
305+
- "{{ if isSet .param_name }}--param={{ .param_name }}{{else}}{{ removeLine }}{{ end }}"
306+
```
307+
308+
### `isChanged`
309+
310+
**Description:** Checks if an option or argument value has been changed (dirty).
311+
312+
**Usage:**
313+
314+
```yaml
315+
- '{{ if isChanged "param_name"}}--param={{.param_name}}{{else}}{{ removeLine }}{{ end }}'
316+
```
317+
318+
### `removeLineIfNil`
319+
**Description:** Removes the entire command line if the value is nil.
320+
321+
**Usage:**
322+
323+
```yaml
324+
- "{{ removeLineIfNil .param_name }}"
325+
```
326+
327+
### `removeLineIfSet`
328+
**Description:** Removes the entire command line if the value is set (has no nil value).
329+
330+
**Usage:**
331+
332+
```yaml
333+
- "{{ removeLineIfSet .param_name }}"
334+
```
335+
336+
### `removeLineIfChanged`
337+
338+
**Description:** Removes the command line entry if the option/argument value has changed.
339+
340+
**Usage:**
341+
342+
``` yaml
343+
- '{{ removeLineIfChanged "param_name" }}'
344+
```
345+
346+
### `removeLineIfNotChanged`
347+
348+
**Description:** Removes the command line entry if the option/argument value has not changed by the user.
349+
Opposite of `removeLineIfChanged`
350+
351+
**Usage:**
352+
353+
``` yaml
354+
- '{{ removeLineIfNotChanged "param_name" }}'
355+
```

example/actions/arguments/action.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ action:
1818
description: Option to do something
1919
type: boolean
2020
default: false
21+
- name: thirdoption
22+
title: Third option
23+
type: string
2124

2225
runtime:
2326
type: container
@@ -31,3 +34,9 @@ runtime:
3134
- "{{ .arg2 }}"
3235
- "{{ .firstoption|removeLineIfNil }}"
3336
- "{{ if not (isNil .secondoption) }}--secondoption={{ .secondoption }}{{ else }}{{ removeLine }}{{ end }}"
37+
- "{{ if isSet .thirdoption }}--thirdoption={{ .thirdoption }}{{else}}Third option is not set{{ end }}"
38+
- "{{ removeLineIfSet .thirdoption }}"
39+
- '{{ if not (isChanged "thirdoption")}}Third Option is not Changed{{else}}{{ removeLine }}{{ end }}'
40+
- '{{ removeLineIfChanged "thirdoption" }}'
41+
- '{{ if isChanged "thirdoption"}}Third Option is Changed{{else}}{{ removeLine }}{{ end }}'
42+
- '{{ removeLineIfNotChanged "thirdoption" }}'

pkg/action/loader.go

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
"github.com/launchrctl/launchr/internal/launchr"
1313
)
1414

15-
const tokenRmLine = "<TOKEN_REMOVE_THIS_LINE>"
15+
const tokenRmLine = "<TOKEN_REMOVE_THIS_LINE>" //nolint:gosec // G101: Not a credential.
1616

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

@@ -95,19 +95,76 @@ func (err errMissingVar) Error() string {
9595
}
9696

9797
// actionTplFuncs defined template functions available during parsing of an action yaml.
98-
func actionTplFuncs() template.FuncMap {
98+
func actionTplFuncs(input *Input) template.FuncMap {
99+
// Helper function to get value by name from args or opts
100+
getValue := func(name string) any {
101+
args := input.Args()
102+
if arg, ok := args[name]; ok {
103+
return arg
104+
}
105+
106+
opts := input.Opts()
107+
if opt, ok := opts[name]; ok {
108+
return opt
109+
}
110+
111+
return nil
112+
}
113+
114+
// Helper function to check if a parameter is changed
115+
isParamChanged := func(name string) bool {
116+
return input.IsOptChanged(name) || input.IsArgChanged(name)
117+
}
118+
99119
return template.FuncMap{
100120
// Checks if a value is nil. Used in conditions.
101121
"isNil": func(v any) bool {
102122
return v == nil
103123
},
124+
// Checks if a value is not nil. Used in conditions.
125+
"isSet": func(v any) bool {
126+
return v != nil
127+
},
128+
// Checks if a value is changed. Used in conditions.
129+
"isChanged": func(v any) bool {
130+
name, ok := v.(string)
131+
if !ok {
132+
return false
133+
}
134+
135+
return isParamChanged(name)
136+
},
104137
// Removes a line if a given value is nil or pass through.
105138
"removeLineIfNil": func(v any) any {
106139
if v == nil {
107140
return tokenRmLine
108141
}
109142
return v
110143
},
144+
// Removes a line if a given value is not nil or pass through.
145+
"removeLineIfSet": func(v any) any {
146+
if v != nil {
147+
return tokenRmLine
148+
}
149+
150+
return v
151+
},
152+
// Removes a line if a given value is changed or pass through.
153+
"removeLineIfChanged": func(name string) any {
154+
if isParamChanged(name) {
155+
return tokenRmLine
156+
}
157+
158+
return getValue(name)
159+
},
160+
// Removes a line if a given value is not changed or pass through.
161+
"removeLineIfNotChanged": func(name string) any {
162+
if !isParamChanged(name) {
163+
return tokenRmLine
164+
}
165+
166+
return getValue(name)
167+
},
111168
// Removes current line.
112169
"removeLine": func() string {
113170
return tokenRmLine
@@ -126,7 +183,7 @@ func (p inputProcessor) Process(ctx LoadContext, b []byte) ([]byte, error) {
126183
addPredefinedVariables(data, a)
127184

128185
// Parse action without variables to validate
129-
tpl := template.New(a.ID).Funcs(actionTplFuncs())
186+
tpl := template.New(a.ID).Funcs(actionTplFuncs(a.Input()))
130187

131188
_, err := tpl.Parse(string(b))
132189
if err != nil {

pkg/action/loader_test.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,17 @@ func Test_InputProcessor(t *testing.T) {
8686
- "{{ .arg2 }}"
8787
- "{{ if not (isNil .arg1) }}arg1 is not nil{{end}}"
8888
- "{{ if (isNil .optUnd) }}{{ removeLine }}{{ end }}" # Function call without new line`
89-
res, err = proc.Process(ctx, []byte(s))
89+
_, err = proc.Process(ctx, []byte(s))
9090
assert.Equal(t, errMissVars, err)
91+
92+
s = `- "{{ if isSet .arg1 }}arg1 is set"{{end}}
93+
- "{{ removeLineIfSet .arg1 }}" # Function call without new line
94+
- "{{ if isChanged .arg1 }}arg1 is changed{{end}}"
95+
- '{{ removeLineIfNotChanged "arg1" }}'
96+
- '{{ removeLineIfChanged "arg1" }}' # Function call without new line`
97+
res, err = proc.Process(ctx, []byte(s))
98+
assert.NoError(t, err)
99+
assert.Equal(t, "- \"arg1 is set\"\n- \"arg1 is changed\"\n- 'arg1'\n", string(res))
91100
}
92101

93102
func Test_YamlTplCommentsProcessor(t *testing.T) {

0 commit comments

Comments
 (0)