2018-07-22 21:05:47 +02:00
|
|
|
package read
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"runtime"
|
|
|
|
|
2020-03-28 16:27:49 +02:00
|
|
|
"gopkg.in/yaml.v3"
|
2021-01-07 16:48:33 +02:00
|
|
|
|
2022-08-06 23:19:07 +02:00
|
|
|
"github.com/go-task/task/v3/internal/filepathext"
|
2021-01-07 16:48:33 +02:00
|
|
|
"github.com/go-task/task/v3/taskfile"
|
2018-07-22 21:05:47 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Taskvars reads a Taskvars for a given directory
|
2020-03-29 21:54:59 +02:00
|
|
|
func Taskvars(dir string) (*taskfile.Vars, error) {
|
|
|
|
vars := &taskfile.Vars{}
|
2018-07-22 21:05:47 +02:00
|
|
|
|
2022-08-06 23:19:07 +02:00
|
|
|
path := filepathext.SmartJoin(dir, "Taskvars.yml")
|
2018-07-22 21:05:47 +02:00
|
|
|
if _, err := os.Stat(path); err == nil {
|
|
|
|
vars, err = readTaskvars(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-06 23:19:07 +02:00
|
|
|
path = filepathext.SmartJoin(dir, fmt.Sprintf("Taskvars_%s.yml", runtime.GOOS))
|
2018-07-22 21:05:47 +02:00
|
|
|
if _, err := os.Stat(path); err == nil {
|
|
|
|
osVars, err := readTaskvars(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-03-29 21:54:59 +02:00
|
|
|
vars.Merge(osVars)
|
2018-07-22 21:05:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return vars, nil
|
|
|
|
}
|
|
|
|
|
2020-03-29 21:54:59 +02:00
|
|
|
func readTaskvars(file string) (*taskfile.Vars, error) {
|
2018-07-22 21:05:47 +02:00
|
|
|
f, err := os.Open(file)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var vars taskfile.Vars
|
2020-03-29 21:54:59 +02:00
|
|
|
return &vars, yaml.NewDecoder(f).Decode(&vars)
|
2018-07-22 21:05:47 +02:00
|
|
|
}
|