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