1
0
mirror of https://github.com/go-task/task.git synced 2025-03-03 14:52:13 +02:00
task/internal/taskfile/read/taskfile.go
Andrey Nering cc6f7b6088 Manually implement merging of Taskfiles and remove dependency on github.com/imdario/mergo
I was carreful enough to check the behavior keeps the same
2018-07-22 17:54:44 -03:00

48 lines
900 B
Go

package read
import (
"fmt"
"os"
"path/filepath"
"runtime"
"github.com/go-task/task/internal/taskfile"
"gopkg.in/yaml.v2"
)
// Taskfile reads a Taskfile for a given directory
func Taskfile(dir string) (*taskfile.Taskfile, error) {
path := filepath.Join(dir, "Taskfile.yml")
t, err := readTaskfile(path)
if err != nil {
return nil, err
}
path = filepath.Join(dir, fmt.Sprintf("Taskfile_%s.yml", runtime.GOOS))
if _, err = os.Stat(path); err == nil {
osTaskfile, err := readTaskfile(path)
if err != nil {
return nil, err
}
if err = taskfile.Merge(t, osTaskfile); err != nil {
return nil, err
}
}
for name, task := range t.Tasks {
task.Task = name
}
return t, nil
}
func readTaskfile(file string) (*taskfile.Taskfile, error) {
f, err := os.Open(file)
if err != nil {
return nil, err
}
var t taskfile.Taskfile
return &t, yaml.NewDecoder(f).Decode(&t)
}