1
0
mirror of https://github.com/go-task/task.git synced 2025-08-08 22:36:57 +02:00

Merge branch 'pull-14-@vars'

This commit is contained in:
Andrey Nering
2017-03-08 18:35:26 -03:00
2 changed files with 49 additions and 3 deletions

View File

@@ -227,6 +227,20 @@ abc
Result: 'abc'
```
#### Dynamic variables
If you prefix a variable with `$`, then the variable is considered a dynamic
variable. The value after the $-symbol will be treated as a command and the
output assigned.
```yml
build:
cmds:
- go build -ldflags="-X main.Version={{.LAST_GIT_COMMIT}}" main.go
vars:
LAST_GIT_COMMIT: $git log -n 1 --format=%h
```
### Go's template engine
Task parse commands as [Go's template engine][gotemplate] before executing

View File

@@ -5,6 +5,7 @@ import (
"encoding/json"
"io/ioutil"
"os"
"os/exec"
"runtime"
"strings"
"text/template"
@@ -18,20 +19,51 @@ var (
TaskvarsFilePath = "Taskvars"
)
func handleDynamicVariableContent(value string) (string, error) {
if value == "" || value[0] != '$' {
return value, nil
}
var cmd *exec.Cmd
if ShExists {
cmd = exec.Command(ShPath, "-c", value[1:])
} else {
cmd = exec.Command("cmd", "/C", value[1:])
}
cmd.Stdin = os.Stdin
cmd.Stderr = os.Stderr
bytes, err := cmd.Output()
if err != nil {
return "", err
}
return strings.TrimSpace(string(bytes)), nil
}
func (t Task) handleVariables() (map[string]string, error) {
localVariables := make(map[string]string)
for key, value := range t.Vars {
localVariables[key] = value
val, err := handleDynamicVariableContent(value)
if err != nil {
return nil, err
}
localVariables[key] = val
}
if fileVariables, err := readTaskvarsFile(); err == nil {
for key, value := range fileVariables {
localVariables[key] = value
val, err := handleDynamicVariableContent(value)
if err != nil {
return nil, err
}
localVariables[key] = val
}
} else {
return nil, err
}
for key, value := range getEnvironmentVariables() {
localVariables[key] = value
val, err := handleDynamicVariableContent(value)
if err != nil {
return nil, err
}
localVariables[key] = val
}
return localVariables, nil
}