diff --git a/read_taskfile.go b/read_taskfile.go index cc5da31d..2d26c8c4 100644 --- a/read_taskfile.go +++ b/read_taskfile.go @@ -26,7 +26,6 @@ func (e *Executor) ReadTaskfile() error { if err != nil { switch err.(type) { case taskFileNotFound: - return nil default: return err } @@ -34,6 +33,9 @@ func (e *Executor) ReadTaskfile() error { if err := mergo.MapWithOverwrite(&e.Tasks, osTasks); err != nil { return err } + if err := e.readTaskvarsFile(); err != nil { + return err + } return nil } @@ -49,3 +51,27 @@ func (e *Executor) readTaskfileData(path string) (tasks map[string]*Task, err er } return nil, taskFileNotFound{path} } + +func (e *Executor) readTaskvarsFile() error { + file := filepath.Join(e.Dir, TaskvarsFilePath) + + if b, err := ioutil.ReadFile(file + ".yml"); err == nil { + if err := yaml.Unmarshal(b, &e.taskvars); err != nil { + return err + } + return nil + } + if b, err := ioutil.ReadFile(file + ".json"); err == nil { + if err := json.Unmarshal(b, &e.taskvars); err != nil { + return err + } + return nil + } + if b, err := ioutil.ReadFile(file + ".toml"); err == nil { + if err := toml.Unmarshal(b, &e.taskvars); err != nil { + return err + } + return nil + } + return nil +} diff --git a/task.go b/task.go index acae9e22..e013781e 100644 --- a/task.go +++ b/task.go @@ -30,6 +30,7 @@ type Executor struct { Stdout io.Writer Stderr io.Writer + taskvars Params watchingFiles map[string]struct{} } diff --git a/variable_handling.go b/variable_handling.go index f3c42c26..c6dbac97 100644 --- a/variable_handling.go +++ b/variable_handling.go @@ -2,9 +2,7 @@ package task import ( "bytes" - "encoding/json" "errors" - "io/ioutil" "os" "path/filepath" "runtime" @@ -13,9 +11,7 @@ import ( "github.com/go-task/task/execext" - "github.com/BurntSushi/toml" "github.com/Masterminds/sprig" - "gopkg.in/yaml.v2" ) var ( @@ -63,16 +59,14 @@ func (e *Executor) getVariables(task string, params Params) (map[string]string, } localVariables[key] = val } - if fileVariables, err := e.readTaskvarsFile(); err == nil { - for key, value := range fileVariables { + if e.taskvars != nil { + for key, value := range e.taskvars { val, err := e.handleDynamicVariableContent(value) if err != nil { return nil, err } localVariables[key] = val } - } else { - return nil, err } for key, value := range getEnvironmentVariables() { localVariables[key] = value @@ -163,28 +157,3 @@ func getEnvironmentVariables() map[string]string { } return m } - -func (e *Executor) readTaskvarsFile() (map[string]string, error) { - file := filepath.Join(e.Dir, TaskvarsFilePath) - - var variables map[string]string - if b, err := ioutil.ReadFile(file + ".yml"); err == nil { - if err := yaml.Unmarshal(b, &variables); err != nil { - return nil, err - } - return variables, nil - } - if b, err := ioutil.ReadFile(file + ".json"); err == nil { - if err := json.Unmarshal(b, &variables); err != nil { - return nil, err - } - return variables, nil - } - if b, err := ioutil.ReadFile(file + ".toml"); err == nil { - if err := toml.Unmarshal(b, &variables); err != nil { - return nil, err - } - return variables, nil - } - return variables, nil -}