Skip to content

Commit

Permalink
Apply variable interpolation to a task's examples and its usage. (#57)
Browse files Browse the repository at this point in the history
Co-authored-by: Jens Petersohn <[email protected]>
  • Loading branch information
jenpet and Jens Petersohn authored Jul 7, 2022
1 parent a1ab412 commit 09b63c8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
20 changes: 20 additions & 0 deletions interpolation/interpolation.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ func Tasks(tasks map[string]*task.Task, data map[string]interface{}) error {
&task.Summary,
&task.Script,
&task.Exec,
&task.Usage,
)
if err != nil {
return err
Expand All @@ -101,6 +102,11 @@ func Tasks(tasks map[string]*task.Task, data map[string]interface{}) error {
task.Env[i] = item
}

// interpolate a task's list of examples
if err := Examples(task.Examples, data); err != nil {
return err
}

// interpolate a task's before and after steps
if err := Optionals("before", task.Before, data); err != nil {
return err
Expand All @@ -112,6 +118,20 @@ func Tasks(tasks map[string]*task.Task, data map[string]interface{}) error {
return nil
}

// Examples interpolates the list of examples (description and command attribute) for a task
func Examples(examples []*task.Example, data map[string]interface{}) error {
for i, example := range examples {
if err := interpolate("example-description", data, &example.Description); err != nil {
return err
}
if err := interpolate("example-command", data, &example.Command); err != nil {
return err
}
examples[i] = example
}
return nil
}

// Optionals interpolates a list of runnables (i.e. optional steps for a task or the overall robo configuration).
func Optionals(id string, rs []*task.Runnable, data map[string]interface{}) error {
for i, step := range rs {
Expand Down
7 changes: 7 additions & 0 deletions interpolation/interpolation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ func TestTasks(t *testing.T) {
Script: "/path/to/{{ .foo }}.sh",
Exec: "{{ .foo }} World!",
Env: []string{"bar={{ .foo }} World!"},
Usage: "robo {{.bar}}",
Examples: []*task.Example{
{Description: "{{ .foo }} Example!", Command: "robo {{.bar}}"},
},
}

vars := map[string]interface{}{"foo": "Hello", "bar": "Bye"}
Expand All @@ -68,4 +72,7 @@ func TestTasks(t *testing.T) {
assert.Equal(t, "Bye", tk.After[1].Script)
assert.Equal(t, "Bye", tk.After[2].Exec)
assert.Equal(t, "bar=Hello World!", tk.Env[0])
assert.Equal(t, "robo Bye", tk.Usage)
assert.Equal(t, "Hello Example!", tk.Examples[0].Description)
assert.Equal(t, "robo Bye", tk.Examples[0].Command)
}

0 comments on commit 09b63c8

Please sign in to comment.