mirror of
https://github.com/go-task/task.git
synced 2025-06-08 23:56:21 +02:00
This shouldn't have any behavior changes for now. This is a code refactor that should allow us to do further improvements on how variables are handled, specially regarding respecting the declaration order in Taskfiles, which should make it easier for the users. Initial work on #218
46 lines
863 B
Go
46 lines
863 B
Go
package read
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
|
|
"github.com/go-task/task/v2/internal/taskfile"
|
|
|
|
"gopkg.in/yaml.v3"
|
|
)
|
|
|
|
// Taskvars reads a Taskvars for a given directory
|
|
func Taskvars(dir string) (*taskfile.Vars, error) {
|
|
vars := &taskfile.Vars{}
|
|
|
|
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
|
|
}
|
|
vars.Merge(osVars)
|
|
}
|
|
|
|
return vars, nil
|
|
}
|
|
|
|
func readTaskvars(file string) (*taskfile.Vars, error) {
|
|
f, err := os.Open(file)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var vars taskfile.Vars
|
|
return &vars, yaml.NewDecoder(f).Decode(&vars)
|
|
}
|