2017-03-07 10:45:14 +02:00
|
|
|
package task
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"runtime"
|
|
|
|
|
|
|
|
"github.com/BurntSushi/toml"
|
|
|
|
"github.com/imdario/mergo"
|
|
|
|
"gopkg.in/yaml.v2"
|
|
|
|
)
|
|
|
|
|
2017-06-04 21:02:04 +02:00
|
|
|
// ReadTaskfile parses Taskfile from the disk
|
|
|
|
func (e *Executor) ReadTaskfile() error {
|
|
|
|
var err error
|
|
|
|
e.Tasks, err = e.readTaskfileData(TaskFilePath)
|
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
|
|
|
|
|
|
|
osTasks, err := e.readTaskfileData(fmt.Sprintf("%s_%s", TaskFilePath, 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
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
return err
|
2017-03-07 10:45:14 +02:00
|
|
|
}
|
|
|
|
}
|
2017-06-04 21:02:04 +02:00
|
|
|
if err := mergo.MapWithOverwrite(&e.Tasks, osTasks); err != nil {
|
|
|
|
return err
|
2017-03-07 10:45:14 +02:00
|
|
|
}
|
2017-06-04 21:02:04 +02:00
|
|
|
return nil
|
2017-03-07 10:45:14 +02:00
|
|
|
}
|
|
|
|
|
2017-06-04 21:02:04 +02:00
|
|
|
func (e *Executor) readTaskfileData(path string) (tasks map[string]*Task, err error) {
|
2017-03-07 10:45:14 +02:00
|
|
|
if b, err := ioutil.ReadFile(path + ".yml"); err == nil {
|
|
|
|
return tasks, yaml.Unmarshal(b, &tasks)
|
|
|
|
}
|
|
|
|
if b, err := ioutil.ReadFile(path + ".json"); err == nil {
|
|
|
|
return tasks, json.Unmarshal(b, &tasks)
|
|
|
|
}
|
|
|
|
if b, err := ioutil.ReadFile(path + ".toml"); err == nil {
|
|
|
|
return tasks, toml.Unmarshal(b, &tasks)
|
|
|
|
}
|
|
|
|
return nil, taskFileNotFound{path}
|
|
|
|
}
|