2017-03-07 10:45:14 +02:00
|
|
|
package task
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2017-06-16 16:24:01 +02:00
|
|
|
"path/filepath"
|
2017-03-07 10:45:14 +02:00
|
|
|
"runtime"
|
|
|
|
|
2018-02-17 18:22:18 +02:00
|
|
|
"github.com/go-task/task/internal/taskfile"
|
|
|
|
|
2017-03-07 10:45:14 +02:00
|
|
|
"github.com/imdario/mergo"
|
|
|
|
"gopkg.in/yaml.v2"
|
|
|
|
)
|
|
|
|
|
2018-03-11 19:39:40 +02:00
|
|
|
// readTaskfile parses Taskfile from the disk
|
|
|
|
func (e *Executor) readTaskfile() error {
|
2017-06-16 16:24:01 +02:00
|
|
|
path := filepath.Join(e.Dir, TaskFilePath)
|
|
|
|
|
2017-06-04 21:02:04 +02:00
|
|
|
var err error
|
2017-12-29 22:27:32 +02:00
|
|
|
e.Taskfile, err = e.readTaskfileData(path)
|
2017-03-07 10:45:14 +02:00
|
|
|
if err != nil {
|
2017-06-04 21:02:04 +02:00
|
|
|
return err
|
2017-03-07 10:45:14 +02:00
|
|
|
}
|
2017-06-04 21:02:04 +02:00
|
|
|
|
2017-06-16 16:24:01 +02:00
|
|
|
osTasks, err := e.readTaskfileData(fmt.Sprintf("%s_%s", path, runtime.GOOS))
|
2017-03-07 10:45:14 +02:00
|
|
|
if err != nil {
|
|
|
|
switch err.(type) {
|
|
|
|
case taskFileNotFound:
|
2017-06-04 21:02:04 +02:00
|
|
|
default:
|
|
|
|
return err
|
2017-03-07 10:45:14 +02:00
|
|
|
}
|
2017-12-29 22:27:32 +02:00
|
|
|
} else {
|
|
|
|
if err := mergo.MapWithOverwrite(&e.Taskfile.Tasks, osTasks.Tasks); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-03-07 10:45:14 +02:00
|
|
|
}
|
2017-12-29 22:27:32 +02:00
|
|
|
for name, task := range e.Taskfile.Tasks {
|
2017-09-16 16:44:13 +02:00
|
|
|
task.Task = name
|
|
|
|
}
|
|
|
|
|
2017-07-08 20:10:01 +02:00
|
|
|
return e.readTaskvars()
|
2017-03-07 10:45:14 +02:00
|
|
|
}
|
|
|
|
|
2018-02-17 18:22:18 +02:00
|
|
|
func (e *Executor) readTaskfileData(path string) (*taskfile.Taskfile, error) {
|
2017-03-07 10:45:14 +02:00
|
|
|
if b, err := ioutil.ReadFile(path + ".yml"); err == nil {
|
2018-02-17 18:22:18 +02:00
|
|
|
var taskfile taskfile.Taskfile
|
2018-04-28 20:39:54 +02:00
|
|
|
return &taskfile, yaml.Unmarshal(b, &taskfile)
|
2017-03-07 10:45:14 +02:00
|
|
|
}
|
|
|
|
return nil, taskFileNotFound{path}
|
|
|
|
}
|
2017-07-04 02:16:10 +02:00
|
|
|
|
2017-07-08 20:10:01 +02:00
|
|
|
func (e *Executor) readTaskvars() error {
|
2017-07-15 18:46:35 +02:00
|
|
|
var (
|
|
|
|
file = filepath.Join(e.Dir, TaskvarsFilePath)
|
|
|
|
osSpecificFile = fmt.Sprintf("%s_%s", file, runtime.GOOS)
|
|
|
|
)
|
2017-07-04 02:16:10 +02:00
|
|
|
|
|
|
|
if b, err := ioutil.ReadFile(file + ".yml"); err == nil {
|
2018-04-28 20:39:54 +02:00
|
|
|
if err := yaml.Unmarshal(b, &e.taskvars); err != nil {
|
2017-07-04 02:16:10 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2017-07-15 18:46:35 +02:00
|
|
|
|
|
|
|
if b, err := ioutil.ReadFile(osSpecificFile + ".yml"); err == nil {
|
2018-02-17 18:22:18 +02:00
|
|
|
osTaskvars := make(taskfile.Vars, 10)
|
2018-04-28 20:39:54 +02:00
|
|
|
if err := yaml.Unmarshal(b, &osTaskvars); err != nil {
|
2017-07-15 18:46:35 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
for k, v := range osTaskvars {
|
|
|
|
e.taskvars[k] = v
|
|
|
|
}
|
|
|
|
}
|
2017-07-04 02:16:10 +02:00
|
|
|
return nil
|
|
|
|
}
|