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"
|
|
|
|
"io/ioutil"
|
2017-03-02 11:46:20 +02:00
|
|
|
"os"
|
2017-03-07 21:07:03 +02:00
|
|
|
"os/exec"
|
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
|
|
|
|
|
|
|
"github.com/BurntSushi/toml"
|
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-02 11:46:20 +02:00
|
|
|
)
|
|
|
|
|
2017-03-07 21:07:03 +02:00
|
|
|
func handleDynamicVariableContent(value string) (string, error) {
|
2017-03-08 23:34:44 +02:00
|
|
|
if value == "" || value[0] != '$' {
|
2017-03-07 21:07:03 +02:00
|
|
|
return value, nil
|
|
|
|
}
|
|
|
|
var cmd *exec.Cmd
|
|
|
|
if ShExists {
|
2017-03-08 09:20:07 +02:00
|
|
|
cmd = exec.Command(ShPath, "-c", value[1:])
|
2017-03-07 21:07:03 +02:00
|
|
|
} else {
|
2017-03-08 09:20:07 +02:00
|
|
|
cmd = exec.Command("cmd", "/C", value[1:])
|
2017-03-07 21:07:03 +02:00
|
|
|
}
|
|
|
|
cmd.Stdin = os.Stdin
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
bytes, err := cmd.Output()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return strings.TrimSpace(string(bytes)), nil
|
|
|
|
}
|
|
|
|
|
2017-03-02 12:28:34 +02:00
|
|
|
func (t Task) handleVariables() (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-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-02 12:28:34 +02:00
|
|
|
return localVariables, nil
|
2017-03-02 11:46:20 +02:00
|
|
|
}
|
|
|
|
|
2017-03-05 21:49:44 +02:00
|
|
|
var templateFuncs = template.FuncMap{
|
|
|
|
"OS": func() string { return runtime.GOOS },
|
|
|
|
"ARCH": func() string { return runtime.GOARCH },
|
|
|
|
"IsSH": func() bool { return 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
|
|
|
}
|
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-05 21:17:50 +02:00
|
|
|
type getKeyValFunc func(item string) (key, val string)
|
|
|
|
getEnvironment := func(data []string, getKeyVal getKeyValFunc) map[string]string {
|
2017-03-02 11:46:20 +02:00
|
|
|
items := make(map[string]string)
|
|
|
|
for _, item := range data {
|
2017-03-05 21:17:50 +02:00
|
|
|
key, val := getKeyVal(item)
|
2017-03-02 11:46:20 +02:00
|
|
|
items[key] = val
|
|
|
|
}
|
|
|
|
return items
|
|
|
|
}
|
2017-03-05 21:17:50 +02:00
|
|
|
return getEnvironment(os.Environ(), func(item string) (key, val string) {
|
2017-03-02 11:46:20 +02:00
|
|
|
splits := strings.Split(item, "=")
|
|
|
|
key = splits[0]
|
|
|
|
val = splits[1]
|
|
|
|
return
|
|
|
|
})
|
|
|
|
}
|
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
|
|
|
|
}
|