1
0
mirror of https://github.com/go-task/task.git synced 2024-12-14 10:52:43 +02:00
task/variable_handling.go

135 lines
3.1 KiB
Go
Raw Normal View History

2017-03-02 11:46:20 +02:00
package task
import (
"bytes"
"encoding/json"
"errors"
"io/ioutil"
2017-03-02 11:46:20 +02:00
"os"
"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/BurntSushi/toml"
2017-03-05 11:08:24 +02:00
"gopkg.in/yaml.v2"
)
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
)
2017-03-09 01:13:52 +02:00
var varCmds = make(map[string]string)
func handleDynamicVariableContent(value string) (string, error) {
2017-03-08 23:34:44 +02:00
if value == "" || value[0] != '$' {
return value, nil
}
2017-03-09 01:13:52 +02:00
if result, ok := varCmds[value]; ok {
return result, nil
}
2017-03-12 22:18:59 +02:00
cmd := execext.NewCommand(value[1:])
cmd.Stdin = os.Stdin
cmd.Stderr = os.Stderr
b, err := cmd.Output()
if err != nil {
return "", err
}
if b[len(b)-1] == '\n' {
b = b[:len(b)-1]
}
if bytes.ContainsRune(b, '\n') {
return "", ErrMultilineResultCmd
}
2017-03-09 01:13:52 +02:00
result := strings.TrimSpace(string(b))
varCmds[value] = result
return result, nil
}
func (t *Task) handleVariables() (map[string]string, error) {
2017-03-02 11:46:20 +02:00
localVariables := make(map[string]string)
for key, value := range t.Vars {
val, err := handleDynamicVariableContent(value)
if err != nil {
return nil, err
}
localVariables[key] = val
2017-03-02 11:46:20 +02:00
}
2017-03-05 21:21:22 +02:00
if fileVariables, err := readTaskvarsFile(); err == nil {
for key, value := range fileVariables {
val, err := handleDynamicVariableContent(value)
if err != nil {
return nil, err
}
localVariables[key] = val
}
} else {
return nil, err
}
2017-03-02 11:46:20 +02:00
for key, value := range getEnvironmentVariables() {
localVariables[key] = value
2017-03-02 11:46:20 +02:00
}
return localVariables, nil
2017-03-02 11:46:20 +02:00
}
var templateFuncs = template.FuncMap{
"OS": func() string { return runtime.GOOS },
"ARCH": func() string { return runtime.GOARCH },
2017-03-12 22:18:59 +02:00
"IsSH": func() bool { return execext.ShExists },
}
// ReplaceVariables writes vars into initial string
func ReplaceVariables(initial string, vars map[string]string) (string, error) {
t, err := template.New("").Funcs(templateFuncs).Parse(initial)
if err != nil {
return "", err
}
b := bytes.NewBuffer(nil)
if err = t.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
}
2017-03-05 21:21:22 +02:00
func readTaskvarsFile() (map[string]string, error) {
var variables map[string]string
2017-03-05 21:21:22 +02:00
if b, err := ioutil.ReadFile(TaskvarsFilePath + ".yml"); err == nil {
if err := yaml.Unmarshal(b, &variables); err != nil {
return nil, err
}
2017-03-05 21:21:22 +02:00
return variables, nil
}
2017-03-05 21:21:22 +02:00
if b, err := ioutil.ReadFile(TaskvarsFilePath + ".json"); err == nil {
if err := json.Unmarshal(b, &variables); err != nil {
return nil, err
}
2017-03-05 21:21:22 +02:00
return variables, nil
}
2017-03-05 21:21:22 +02:00
if b, err := ioutil.ReadFile(TaskvarsFilePath + ".toml"); err == nil {
if err := toml.Unmarshal(b, &variables); err != nil {
return nil, err
}
2017-03-05 21:21:22 +02:00
return variables, nil
}
return variables, nil
}