mirror of
https://github.com/go-task/task.git
synced 2026-06-25 13:46:13 +00:00
feat: node refactor (#1316)
* refactor: node reader interface * refactor: rewrite Taskfile() as anon recursive func * chore: NewNodeFromIncludedTaskfile * chore: changelog
This commit is contained in:
70
taskfile/read/node_file.go
Normal file
70
taskfile/read/node_file.go
Normal file
@@ -0,0 +1,70 @@
|
||||
package read
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
|
||||
"github.com/go-task/task/v3/errors"
|
||||
"github.com/go-task/task/v3/internal/filepathext"
|
||||
"github.com/go-task/task/v3/taskfile"
|
||||
)
|
||||
|
||||
// A FileNode is a node that reads a taskfile from the local filesystem.
|
||||
type FileNode struct {
|
||||
BaseNode
|
||||
Dir string
|
||||
Entrypoint string
|
||||
}
|
||||
|
||||
func NewFileNode(parent Node, path string, optional bool) (*FileNode, error) {
|
||||
path, err := exists(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &FileNode{
|
||||
BaseNode: BaseNode{
|
||||
parent: parent,
|
||||
optional: optional,
|
||||
},
|
||||
Dir: filepath.Dir(path),
|
||||
Entrypoint: filepath.Base(path),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (node *FileNode) Location() string {
|
||||
return filepathext.SmartJoin(node.Dir, node.Entrypoint)
|
||||
}
|
||||
|
||||
func (node *FileNode) Read() (*taskfile.Taskfile, error) {
|
||||
if node.Dir == "" {
|
||||
d, err := os.Getwd()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
node.Dir = d
|
||||
}
|
||||
|
||||
path, err := existsWalk(node.Location())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
node.Dir = filepath.Dir(path)
|
||||
node.Entrypoint = filepath.Base(path)
|
||||
|
||||
f, err := os.Open(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
var t taskfile.Taskfile
|
||||
if err := yaml.NewDecoder(f).Decode(&t); err != nil {
|
||||
return nil, &errors.TaskfileInvalidError{URI: filepathext.TryAbsToRel(path), Err: err}
|
||||
}
|
||||
|
||||
t.Location = path
|
||||
return &t, nil
|
||||
}
|
||||
Reference in New Issue
Block a user