8000 Add helper functions to script templates by evilmarty · Pull Request #41 · evilmarty/ilc · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add helper functions to script templates #41

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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