1
0
mirror of https://github.com/go-task/task.git synced 2025-01-18 04:59:01 +02:00
task/read_taskfile.go
Andrey Nering 81e0f170ef accept setting dir of execution and improve tests
One test is not yet migrated. First we should have specific
Stdin, Stdout and Stderr for executor.
2017-06-24 20:09:05 -03:00

52 lines
1.1 KiB
Go

package task
import (
"encoding/json"
"fmt"
"io/ioutil"
"path/filepath"
"runtime"
"github.com/BurntSushi/toml"
"github.com/imdario/mergo"
"gopkg.in/yaml.v2"
)
// ReadTaskfile parses Taskfile from the disk
func (e *Executor) ReadTaskfile() error {
path := filepath.Join(e.Dir, TaskFilePath)
var err error
e.Tasks, err = e.readTaskfileData(path)
if err != nil {
return err
}
osTasks, err := e.readTaskfileData(fmt.Sprintf("%s_%s", path, runtime.GOOS))
if err != nil {
switch err.(type) {
case taskFileNotFound:
return nil
default:
return err
}
}
if err := mergo.MapWithOverwrite(&e.Tasks, osTasks); err != nil {
return err
}
return nil
}
func (e *Executor) readTaskfileData(path string) (tasks map[string]*Task, err error) {
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}
}