Skip to content

Commit

Permalink
Merge pull request #41 from evilmarty/funcs
Browse files Browse the repository at this point in the history
Add helper functions to script templates
  • Loading branch information
evilmarty authored Jul 16, 2024
2 parents 081e386 + a40ea91 commit 4c9750e
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 1 deletion.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,14 @@ The expression to reference an input value. ie. '{{ .Input.my_input }}'

The expression to reference an environment variable. ie. '{{ .Env.HOME }}'

### input "input_name"

A function to retrieve the input by its name. ie. '{{input "my_input"}}'

### env "variable_name"

A function to retrieve the environment variable by its name. ie. '{{env "HOME"}}'

## Example config with single command

```yaml
Expand Down
2 changes: 1 addition & 1 deletion command_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func (cs CommandSet) RenderScript(data TemplateData) (string, error) {
continue
}
if tmpl == nil {
tmpl = template.New(command.Name)
tmpl = template.New(command.Name).Funcs(data.Funcs())
} else {
tmpl = tmpl.New(command.Name)
}
Expand Down
24 changes: 24 additions & 0 deletions command_set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,30 @@ func TestCommandSetRenderScript(t *testing.T) {
assert.NoError(t, err, "CommandSet.RenderScript() returned an unexpected error")
assert.Equal(t, expected, actual, "CommandSet.RenderScript() returned unexpected result")
})
t.Run("helper functions", func(t *testing.T) {
data := TemplateData{
Input: map[string]any{
"A": "a",
"B": "b",
},
Env: map[string]string{
"C": "c",
"D": "d",
},
}
cs := CommandSet{
Commands: []ConfigCommand{
{
Name: "foobar",
Run: "echo {{input \"A\"}} {{env \"C\"}}",
},
},
}
expected := "echo a c"
actual, err := cs.RenderScript(data)
assert.NoError(t, err, "CommandSet.RenderScript() returned an unexpected error")
assert.Equal(t, expected, actual, "CommandSet.RenderScript() returned unexpected result")
})
}

func TestCommandSetRenderScriptToTemp(t *testing.T) {
Expand Down
23 changes: 23 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,29 @@ type TemplateData struct {
Env map[string]string
}

func (td TemplateData) getInput(name string) any {
if v, ok := td.Input[name]; ok {
return v
} else {
return nil
}
}

func (td TemplateData) getEnv(name string) any {
if v, ok := td.Env[name]; ok {
return v
} else {
return nil
}
}

func (td *TemplateData) Funcs() template.FuncMap {
return template.FuncMap{
"input": td.getInput,
"env": td.getEnv,
}
}

func NewTemplateData(input map[string]any, env []string) TemplateData {
safeInputs := make(map[string]any)
for name, value := range input {
Expand Down

0 comments on commit 4c9750e

Please sign in to comment.