2017-03-02 11:46:20 +02:00
|
|
|
package task
|
|
|
|
|
|
|
|
import (
|
2017-03-05 21:49:44 +02:00
|
|
|
"bytes"
|
2017-03-02 12:28:34 +02:00
|
|
|
"encoding/json"
|
2017-03-09 00:03:17 +02:00
|
|
|
"errors"
|
2017-03-02 12:28:34 +02:00
|
|
|
"io/ioutil"
|
2017-03-02 11:46:20 +02:00
|
|
|
"os"
|
2017-04-16 22:16:56 +02:00
|
|
|
"path/filepath"
|
2017-03-05 21:49:44 +02:00
|
|
|
"runtime"
|
2017-03-02 11:46:20 +02:00
|
|
|
"strings"
|
2017-03-05 21:49:44 +02:00
|
|
|
"text/template"
|
2017-03-02 12:28:34 +02:00
|
|
|
|
2017-03-12 22:18:59 +02:00
|
|
|
"github.com/go-task/task/execext"
|
|
|
|
|
2017-03-02 12:28:34 +02:00
|
|
|
"github.com/BurntSushi/toml"
|
2017-04-16 21:53:11 +02:00
|
|
|
"github.com/Masterminds/sprig"
|
2017-03-05 11:08:24 +02:00
|
|
|
"gopkg.in/yaml.v2"
|
2017-03-02 12:28:34 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2017-03-05 21:21:22 +02:00
|
|
|
// TaskvarsFilePath file containing additional variables
|
|
|
|
TaskvarsFilePath = "Taskvars"
|
2017-03-09 00:03:17 +02:00
|
|
|
// 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-07 21:07:03 +02:00
|
|
|
func handleDynamicVariableContent(value string) (string, error) {
|
2017-04-30 23:50:44 +02:00
|
|
|
if !strings.HasPrefix(value, "$") {
|
2017-03-07 21:07:03 +02:00
|
|
|
return value, nil
|
|
|
|
}
|
2017-04-22 20:46:29 +02:00
|
|
|
|
|
|
|
buff := bytes.NewBuffer(nil)
|
|
|
|
|
|
|
|
opts := &execext.RunCommandOptions{
|
|
|
|
Command: strings.TrimPrefix(value, "$"),
|
|
|
|
Stdout: buff,
|
|
|
|
Stderr: os.Stderr,
|
2017-03-07 21:07:03 +02:00
|
|
|
}
|
2017-04-22 20:46:29 +02:00
|
|
|
if err := execext.RunCommand(opts); err != nil {
|
|
|
|
return "", err
|
2017-03-09 00:03:17 +02:00
|
|
|
}
|
2017-04-22 20:46:29 +02:00
|
|
|
|
|
|
|
result := buff.String()
|
|
|
|
result = strings.TrimSuffix(result, "\n")
|
|
|
|
if strings.ContainsRune(result, '\n') {
|
2017-03-09 00:03:17 +02:00
|
|
|
return "", ErrMultilineResultCmd
|
|
|
|
}
|
2017-04-22 20:46:29 +02:00
|
|
|
|
|
|
|
result = strings.TrimSpace(result)
|
2017-03-09 01:13:52 +02:00
|
|
|
return result, nil
|
2017-03-07 21:07:03 +02:00
|
|
|
}
|
|
|
|
|
2017-05-01 00:13:21 +02:00
|
|
|
func (t *Task) getVariables() (map[string]string, error) {
|
2017-03-02 11:46:20 +02:00
|
|
|
localVariables := make(map[string]string)
|
2017-03-05 20:56:07 +02:00
|
|
|
for key, value := range t.Vars {
|
2017-03-07 21:07:03 +02:00
|
|
|
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 {
|
2017-03-02 12:28:34 +02:00
|
|
|
for key, value := range fileVariables {
|
2017-03-07 21:07:03 +02:00
|
|
|
val, err := handleDynamicVariableContent(value)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
localVariables[key] = val
|
2017-03-02 12:28:34 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-03-02 11:46:20 +02:00
|
|
|
for key, value := range getEnvironmentVariables() {
|
2017-03-09 01:03:21 +02:00
|
|
|
localVariables[key] = value
|
2017-03-02 11:46:20 +02:00
|
|
|
}
|
2017-03-02 12:28:34 +02:00
|
|
|
return localVariables, nil
|
2017-03-02 11:46:20 +02:00
|
|
|
}
|
|
|
|
|
2017-04-16 21:53:11 +02:00
|
|
|
var templateFuncs template.FuncMap
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
taskFuncs := template.FuncMap{
|
|
|
|
"OS": func() string { return runtime.GOOS },
|
|
|
|
"ARCH": func() string { return runtime.GOARCH },
|
2017-04-22 20:46:29 +02:00
|
|
|
// historical reasons
|
|
|
|
"IsSH": func() bool { return true },
|
2017-04-16 22:16:56 +02:00
|
|
|
"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 ""
|
|
|
|
},
|
2017-04-16 21:53:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
templateFuncs = sprig.TxtFuncMap()
|
|
|
|
for k, v := range taskFuncs {
|
|
|
|
templateFuncs[k] = v
|
|
|
|
}
|
2017-03-05 21:49:44 +02:00
|
|
|
}
|
|
|
|
|
2017-05-01 00:32:28 +02:00
|
|
|
// ReplaceSliceVariables writes vars into initial string slice
|
|
|
|
func (t *Task) ReplaceSliceVariables(initials []string) ([]string, error) {
|
|
|
|
result := make([]string, len(initials))
|
|
|
|
for i, s := range initials {
|
|
|
|
var err error
|
|
|
|
result[i], err = t.ReplaceVariables(s)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2017-03-05 21:49:44 +02:00
|
|
|
// ReplaceVariables writes vars into initial string
|
2017-05-01 00:13:21 +02:00
|
|
|
func (t *Task) ReplaceVariables(initial string) (string, error) {
|
|
|
|
vars, err := t.getVariables()
|
2017-03-05 21:49:44 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-03-05 21:49:44 +02:00
|
|
|
b := bytes.NewBuffer(nil)
|
2017-05-01 00:13:21 +02:00
|
|
|
if err = templ.Execute(b, vars); err != nil {
|
2017-03-05 21:49:44 +02:00
|
|
|
return "", err
|
2017-03-02 11:46:20 +02:00
|
|
|
}
|
2017-03-05 21:49:44 +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 {
|
2017-03-25 16:01:44 +02:00
|
|
|
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
|
|
|
}
|
2017-03-25 16:01:44 +02:00
|
|
|
return m
|
2017-03-02 11:46:20 +02:00
|
|
|
}
|
2017-03-02 12:28:34 +02:00
|
|
|
|
2017-03-05 21:21:22 +02:00
|
|
|
func readTaskvarsFile() (map[string]string, error) {
|
2017-03-02 12:28:34 +02:00
|
|
|
var variables map[string]string
|
2017-03-05 21:21:22 +02:00
|
|
|
if b, err := ioutil.ReadFile(TaskvarsFilePath + ".yml"); err == nil {
|
2017-03-02 12:28:34 +02:00
|
|
|
if err := yaml.Unmarshal(b, &variables); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-03-05 21:21:22 +02:00
|
|
|
return variables, nil
|
2017-03-02 12:28:34 +02:00
|
|
|
}
|
2017-03-05 21:21:22 +02:00
|
|
|
if b, err := ioutil.ReadFile(TaskvarsFilePath + ".json"); err == nil {
|
2017-03-02 12:28:34 +02:00
|
|
|
if err := json.Unmarshal(b, &variables); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-03-05 21:21:22 +02:00
|
|
|
return variables, nil
|
2017-03-02 12:28:34 +02:00
|
|
|
}
|
2017-03-05 21:21:22 +02:00
|
|
|
if b, err := ioutil.ReadFile(TaskvarsFilePath + ".toml"); err == nil {
|
2017-03-02 12:28:34 +02:00
|
|
|
if err := toml.Unmarshal(b, &variables); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-03-05 21:21:22 +02:00
|
|
|
return variables, nil
|
2017-03-02 12:28:34 +02:00
|
|
|
}
|
|
|
|
return variables, nil
|
|
|
|
}
|