-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce variable map self-interpolation and command variable interp…
…olation. A variable in the variables section can now reference another variable and will get populated before an actual task’s content will get populated. Additionally to self-referencing variables `$()` will run a command and capture the output into a variable.
- Loading branch information
Jens Petersohn
committed
Sep 2, 2020
1 parent
2a554e0
commit 5976aaa
Showing
7 changed files
with
224 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
// Package interpolation provides methods to interpolate user defined variables and robo tasks. | ||
package interpolation | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"github.com/tj/robo/task" | ||
"gopkg.in/yaml.v2" | ||
"os" | ||
"os/exec" | ||
"regexp" | ||
"strings" | ||
"text/template" | ||
) | ||
|
||
var commandPattern = regexp.MustCompile("\\$\\((.+)\\)") | ||
|
||
// Vars interpolates a given map of interfaces (strings or submaps) with itself | ||
// returning it mit populated template values. | ||
func Vars(vars *map[string]interface{}) error { | ||
b, err := yaml.Marshal(*vars) | ||
if err != nil { | ||
return err | ||
} | ||
s := string(b) | ||
|
||
err = interpolate("variables", *vars, &s) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = interpolateVariableCommands(&s) | ||
if err != nil { | ||
return fmt.Errorf("failed replacing variable placeholder with command result") | ||
} | ||
|
||
err = yaml.Unmarshal([]byte(s), vars) | ||
if err != nil { | ||
return err | ||
} | ||
return err | ||
} | ||
|
||
func interpolateVariableCommands(s *string) error { | ||
// find all commands | ||
matches := commandPattern.FindAllStringSubmatch(*s, -1) | ||
for _, match := range matches { | ||
if len(match) != 2 { | ||
continue | ||
} | ||
cmdOut, err := captureCommandOutput(match[1]) | ||
if err != nil { | ||
return fmt.Errorf("error while executing command. Error: %s", err) | ||
} | ||
*s = strings.ReplaceAll(*s, match[0], cmdOut) | ||
} | ||
return nil | ||
} | ||
|
||
// captureCommandOutput executes a command and captures the output which usually gets prompted to stdout. | ||
func captureCommandOutput(args string) (string, error) { | ||
var cmd *exec.Cmd | ||
// try to use the user's default shell. If it is not set via env var fall back to `sh`. | ||
if defaultShell, ok := os.LookupEnv("SHELL"); ok { | ||
cmd = exec.Command(defaultShell, "-c", args) | ||
} else { | ||
cmd = exec.Command("sh", "-c", args) | ||
} | ||
var b bytes.Buffer | ||
cmd.Stdin = os.Stdin | ||
cmd.Stdout = &b | ||
cmd.Stderr = os.Stderr | ||
err := cmd.Run() | ||
return strings.TrimSuffix(string(b.Bytes()), "\n"), err | ||
} | ||
|
||
// Tasks interpolates a given task with a set of data replacing placeholders | ||
// in the command, summary, script, exec and envs property. | ||
func Tasks(tasks map[string]*task.Task, data map[string]interface{}) error { | ||
for _, task := range tasks { | ||
// interpolate the tasks main fields | ||
err := interpolate( | ||
"task", | ||
data, | ||
&task.Command, | ||
&task.Summary, | ||
&task.Script, | ||
&task.Exec, | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// interpolate a tasks environment data | ||
for i, item := range task.Env { | ||
if err := interpolate("env-var", data, &item); err != nil { | ||
return err | ||
} | ||
task.Env[i] = item | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
// interpolate populates a given slice of templates with actual values provided | ||
// in the data parameter. | ||
func interpolate(name string, data interface{}, temps ...*string) error { | ||
for _, temp := range temps { | ||
t, err := template.New(name).Parse(*temp) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var b bytes.Buffer | ||
err = t.Execute(&b, data) | ||
if err != nil { | ||
return err | ||
} | ||
*temp = string(b.Bytes()) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package interpolation | ||
|
||
import ( | ||
"github.com/bmizerany/assert" | ||
"github.com/tj/robo/task" | ||
"testing" | ||
) | ||
|
||
func TestVars_whenValueReferencesOtherKey_shouldReplaceAccordingly(t *testing.T) { | ||
vars := map[string]interface{}{ | ||
"foo": "Hello", | ||
"bar": "{{ .foo }} World!", | ||
} | ||
err := Vars(&vars) | ||
|
||
assert.Equal(t, nil, err) | ||
assert.Equal(t, "Hello", vars["foo"]) | ||
assert.Equal(t, "Hello World!", vars["bar"]) | ||
} | ||
|
||
func TestVars_whenValueIsCommand_shouldReplaceWithCommandResult(t *testing.T) { | ||
vars := map[string]interface{}{ | ||
"foo": "$(echo Hello)", | ||
"bar": | ||
map[string]interface{}{ | ||
"sub": "$(echo World!)", | ||
}, | ||
} | ||
|
||
err := Vars(&vars) | ||
|
||
assert.Equal(t, nil, err) | ||
assert.Equal(t, "Hello", vars["foo"]) | ||
assert.Equal(t, "World!", vars["bar"].(map[interface{}]interface{})["sub"]) | ||
} | ||
|
||
func TestTasks(t *testing.T) { | ||
tk := task.Task{ | ||
Summary: "This task handles {{ .foo }} World!", | ||
Command: "echo {{ .foo }} World!", | ||
Script: "/path/to/{{ .foo }}.sh", | ||
Exec: "{{ .foo }} World!", | ||
Env: []string{"bar={{ .foo }} World!"}, | ||
} | ||
|
||
vars := map[string]interface{}{"foo": "Hello"} | ||
|
||
err := Tasks(map[string]*task.Task{"tk": &tk}, vars) | ||
assert.Equal(t, nil, err) | ||
assert.Equal(t, "This task handles Hello World!", tk.Summary) | ||
assert.Equal(t, "echo Hello World!", tk.Command) | ||
assert.Equal(t, "/path/to/Hello.sh", tk.Script) | ||
assert.Equal(t, "Hello World!", tk.Exec) | ||
assert.Equal(t, "bar=Hello World!", tk.Env[0]) | ||
} |