1
0
mirror of https://github.com/go-task/task.git synced 2025-01-12 04:34:11 +02:00

refactor: onyl read Taskvars file once

This commit is contained in:
Andrey Nering 2017-07-03 21:16:10 -03:00
parent 196d3cb13d
commit b8fe8d465e
3 changed files with 30 additions and 34 deletions

View File

@ -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
}

View File

@ -30,6 +30,7 @@ type Executor struct {
Stdout io.Writer
Stderr io.Writer
taskvars Params
watchingFiles map[string]struct{}
}

View File

@ -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
}