2017-03-02 11:46:20 +02:00
|
|
|
package task
|
|
|
|
|
|
|
|
import (
|
2017-03-05 21:49:44 +02:00
|
|
|
"bytes"
|
2017-03-09 00:03:17 +02:00
|
|
|
"errors"
|
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-04-16 21:53:11 +02:00
|
|
|
"github.com/Masterminds/sprig"
|
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-07-15 20:28:59 +02:00
|
|
|
// Vars is a string[string] variables map
|
|
|
|
type Vars map[string]Var
|
|
|
|
|
|
|
|
// Var represents either a static or dynamic variable
|
|
|
|
type Var struct {
|
|
|
|
Static string
|
|
|
|
Sh string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (vs Vars) toStringMap() (m map[string]string) {
|
|
|
|
m = make(map[string]string, len(vs))
|
|
|
|
for k, v := range vs {
|
|
|
|
m[k] = v.Static
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
// ErrCantUnmarshalVar is returned for invalid var YAML
|
|
|
|
ErrCantUnmarshalVar = errors.New("task: can't unmarshal var value")
|
|
|
|
)
|
|
|
|
|
|
|
|
// UnmarshalYAML implements yaml.Unmarshaler interface
|
|
|
|
func (v *Var) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
|
|
|
var str string
|
|
|
|
if err := unmarshal(&str); err == nil {
|
|
|
|
if strings.HasPrefix(str, "$") {
|
|
|
|
v.Sh = strings.TrimPrefix(str, "$")
|
|
|
|
} else {
|
|
|
|
v.Static = str
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var sh struct {
|
|
|
|
Sh string
|
|
|
|
}
|
|
|
|
if err := unmarshal(&sh); err == nil {
|
|
|
|
v.Sh = sh.Sh
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return ErrCantUnmarshalVar
|
|
|
|
}
|
|
|
|
|
2017-07-08 21:00:17 +02:00
|
|
|
var (
|
|
|
|
templateFuncs template.FuncMap
|
|
|
|
)
|
2017-04-16 21:53:11 +02:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// ReplaceVariables writes vars into initial string
|
2017-07-08 19:34:17 +02:00
|
|
|
func (e *Executor) ReplaceVariables(initial string, call Call) (string, error) {
|
2017-07-15 20:38:57 +02:00
|
|
|
if initial == "" {
|
|
|
|
return initial, nil
|
|
|
|
}
|
|
|
|
|
2017-05-01 00:13:21 +02:00
|
|
|
templ, err := template.New("").Funcs(templateFuncs).Parse(initial)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2017-07-08 19:57:12 +02:00
|
|
|
var b bytes.Buffer
|
2017-07-15 20:28:59 +02:00
|
|
|
if err = templ.Execute(&b, call.Vars.toStringMap()); 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
|
|
|
}
|
|
|
|
|
2017-07-08 20:13:27 +02:00
|
|
|
// ReplaceSliceVariables writes vars into initial string slice
|
|
|
|
func (e *Executor) ReplaceSliceVariables(initials []string, call Call) ([]string, error) {
|
|
|
|
result := make([]string, len(initials))
|
|
|
|
for i, s := range initials {
|
|
|
|
var err error
|
|
|
|
result[i], err = e.ReplaceVariables(s, call)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Executor) getVariables(call Call) (Vars, error) {
|
|
|
|
t := e.Tasks[call.Task]
|
|
|
|
|
|
|
|
result := make(Vars, len(t.Vars)+len(e.taskvars)+len(call.Vars))
|
2017-07-08 21:00:17 +02:00
|
|
|
merge := func(vars Vars, runTemplate bool) error {
|
2017-07-08 20:13:27 +02:00
|
|
|
for k, v := range vars {
|
2017-07-08 21:00:17 +02:00
|
|
|
if runTemplate {
|
|
|
|
var err error
|
2017-07-15 20:28:59 +02:00
|
|
|
v.Static, err = e.ReplaceVariables(v.Static, call)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
v.Sh, err = e.ReplaceVariables(v.Sh, call)
|
2017-07-08 21:00:17 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-08 20:13:27 +02:00
|
|
|
v, err := e.handleDynamicVariableContent(v)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-07-08 21:00:17 +02:00
|
|
|
|
2017-07-15 20:28:59 +02:00
|
|
|
result[k] = Var{Static: v}
|
2017-07-08 20:13:27 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-07-08 21:00:17 +02:00
|
|
|
if err := merge(e.taskvars, false); err != nil {
|
2017-07-08 20:13:27 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
2017-07-08 21:00:17 +02:00
|
|
|
if err := merge(t.Vars, true); err != nil {
|
2017-07-08 20:13:27 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
2017-07-08 21:00:17 +02:00
|
|
|
if err := merge(getEnvironmentVariables(), false); err != nil {
|
2017-07-08 20:13:27 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
2017-07-08 21:00:17 +02:00
|
|
|
if err := merge(call.Vars, false); err != nil {
|
2017-07-08 20:13:27 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2017-03-02 11:46:20 +02:00
|
|
|
// GetEnvironmentVariables returns environment variables as map
|
2017-07-08 19:46:49 +02:00
|
|
|
func getEnvironmentVariables() Vars {
|
2017-03-25 16:01:44 +02:00
|
|
|
var (
|
|
|
|
env = os.Environ()
|
2017-07-08 19:46:49 +02:00
|
|
|
m = make(Vars, len(env))
|
2017-03-25 16:01:44 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
for _, e := range env {
|
|
|
|
keyVal := strings.SplitN(e, "=", 2)
|
|
|
|
key, val := keyVal[0], keyVal[1]
|
2017-07-15 20:28:59 +02:00
|
|
|
m[key] = Var{Static: 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-07-08 20:13:27 +02:00
|
|
|
|
2017-07-15 20:28:59 +02:00
|
|
|
func (e *Executor) handleDynamicVariableContent(v Var) (string, error) {
|
|
|
|
if v.Static != "" {
|
|
|
|
return v.Static, nil
|
2017-07-08 20:13:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
e.muDynamicCache.Lock()
|
|
|
|
defer e.muDynamicCache.Unlock()
|
2017-07-15 20:28:59 +02:00
|
|
|
if result, ok := e.dynamicCache[v.Sh]; ok {
|
2017-07-08 20:13:27 +02:00
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var stdout bytes.Buffer
|
|
|
|
opts := &execext.RunCommandOptions{
|
2017-07-15 20:28:59 +02:00
|
|
|
Command: v.Sh,
|
2017-07-08 20:13:27 +02:00
|
|
|
Dir: e.Dir,
|
|
|
|
Stdout: &stdout,
|
|
|
|
Stderr: e.Stderr,
|
|
|
|
}
|
|
|
|
if err := execext.RunCommand(opts); err != nil {
|
|
|
|
return "", &dynamicVarError{cause: err, cmd: opts.Command}
|
|
|
|
}
|
|
|
|
|
|
|
|
result := strings.TrimSuffix(stdout.String(), "\n")
|
|
|
|
if strings.ContainsRune(result, '\n') {
|
|
|
|
return "", ErrMultilineResultCmd
|
|
|
|
}
|
|
|
|
|
|
|
|
result = strings.TrimSpace(result)
|
2017-07-15 20:28:59 +02:00
|
|
|
e.verbosePrintfln(`task: dynamic variable: "%s", result: "%s"`, v.Sh, result)
|
|
|
|
e.dynamicCache[v.Sh] = result
|
2017-07-08 20:13:27 +02:00
|
|
|
return result, nil
|
|
|
|
}
|