2018-07-22 21:05:47 +02:00
|
|
|
package read
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
|
|
|
|
2020-08-19 10:59:58 +02:00
|
|
|
"github.com/go-task/task/v3/taskfile"
|
2018-07-22 21:05:47 +02:00
|
|
|
|
2020-03-28 16:27:49 +02:00
|
|
|
"gopkg.in/yaml.v3"
|
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
|
|
|
|
|
|
|
path := filepath.Join(dir, "Taskvars.yml")
|
|
|
|
if _, err := os.Stat(path); err == nil {
|
|
|
|
vars, err = readTaskvars(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
path = filepath.Join(dir, fmt.Sprintf("Taskvars_%s.yml", runtime.GOOS))
|
|
|
|
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
|
|
|
}
|