1
0
mirror of https://github.com/go-task/task.git synced 2025-01-12 04:34:11 +02:00
task/variable_handling.go

160 lines
3.5 KiB
Go
Raw Normal View History

2017-03-02 11:46:20 +02:00
package task
import (
"bytes"
"errors"
2017-03-02 11:46:20 +02:00
"os"
"path/filepath"
"runtime"
2017-03-02 11:46:20 +02:00
"strings"
"text/template"
2017-03-12 22:18:59 +02:00
"github.com/go-task/task/execext"
"github.com/Masterminds/sprig"
)
var (
2017-03-05 21:21:22 +02:00
// TaskvarsFilePath file containing additional variables
TaskvarsFilePath = "Taskvars"
// ErrMultilineResultCmd is returned when a command returns multiline result
ErrMultilineResultCmd = errors.New("Got multiline result from command")
2017-03-02 11:46:20 +02:00
)
func (e *Executor) handleDynamicVariableContent(value string) (string, error) {
2017-04-30 23:50:44 +02:00
if !strings.HasPrefix(value, "$") {
return value, nil
}
buff := bytes.NewBuffer(nil)
opts := &execext.RunCommandOptions{
Command: strings.TrimPrefix(value, "$"),
Dir: e.Dir,
Stdout: buff,
Stderr: e.Stderr,
}
if err := execext.RunCommand(opts); err != nil {
return "", err
}
result := buff.String()
result = strings.TrimSuffix(result, "\n")
if strings.ContainsRune(result, '\n') {
return "", ErrMultilineResultCmd
}
result = strings.TrimSpace(result)
2017-03-09 01:13:52 +02:00
return result, nil
}
2017-07-06 01:07:24 +02:00
func (e *Executor) getVariables(task string, vars Vars) (map[string]string, error) {
t := e.Tasks[task]
2017-03-02 11:46:20 +02:00
localVariables := make(map[string]string)
for key, value := range t.Vars {
val, err := e.handleDynamicVariableContent(value)
if err != nil {
return nil, err
}
localVariables[key] = val
2017-03-02 11:46:20 +02:00
}
2017-07-04 02:16:10 +02:00
if e.taskvars != nil {
for key, value := range e.taskvars {
val, err := e.handleDynamicVariableContent(value)
if err != nil {
return nil, err
}
localVariables[key] = val
}
}
2017-03-02 11:46:20 +02:00
for key, value := range getEnvironmentVariables() {
localVariables[key] = value
2017-03-02 11:46:20 +02:00
}
2017-07-06 01:07:24 +02:00
if vars != nil {
for k, v := range vars {
2017-07-02 20:30:50 +02:00
val, err := e.handleDynamicVariableContent(v)
if err != nil {
return nil, err
}
localVariables[k] = val
}
}
return localVariables, nil
2017-03-02 11:46:20 +02:00
}
var templateFuncs template.FuncMap
func init() {
taskFuncs := template.FuncMap{
"OS": func() string { return runtime.GOOS },
"ARCH": func() string { return runtime.GOARCH },
// historical reasons
"IsSH": func() bool { return true },
"FromSlash": func(path string) string {
return filepath.FromSlash(path)
},
"ToSlash": func(path string) string {
return filepath.ToSlash(path)
},
2017-05-27 15:52:22 +02:00
"ExeExt": func() string {
if runtime.GOOS == "windows" {
return ".exe"
}
return ""
},
}
templateFuncs = sprig.TxtFuncMap()
for k, v := range taskFuncs {
templateFuncs[k] = v
}
}
// ReplaceSliceVariables writes vars into initial string slice
2017-07-06 01:07:24 +02:00
func (e *Executor) ReplaceSliceVariables(initials []string, task string, vars Vars) ([]string, error) {
result := make([]string, len(initials))
for i, s := range initials {
var err error
2017-07-06 01:07:24 +02:00
result[i], err = e.ReplaceVariables(s, task, vars)
if err != nil {
return nil, err
}
}
return result, nil
}
// ReplaceVariables writes vars into initial string
2017-07-06 01:07:24 +02:00
func (e *Executor) ReplaceVariables(initial, task string, vars Vars) (string, error) {
vars, err := e.getVariables(task, vars)
if err != nil {
return "", err
}
2017-05-01 00:13:21 +02:00
templ, err := template.New("").Funcs(templateFuncs).Parse(initial)
if err != nil {
return "", err
}
b := bytes.NewBuffer(nil)
2017-05-01 00:13:21 +02:00
if err = templ.Execute(b, vars); err != nil {
return "", err
2017-03-02 11:46:20 +02:00
}
return b.String(), nil
2017-03-02 11:46:20 +02:00
}
// GetEnvironmentVariables returns environment variables as map
func getEnvironmentVariables() map[string]string {
var (
env = os.Environ()
m = make(map[string]string, len(env))
)
for _, e := range env {
keyVal := strings.SplitN(e, "=", 2)
key, val := keyVal[0], keyVal[1]
m[key] = val
2017-03-02 11:46:20 +02:00
}
return m
2017-03-02 11:46:20 +02:00
}