mirror of
https://github.com/go-task/task.git
synced 2025-11-25 22:32:55 +02:00
refactor: taskfile/ast package (#1450)
* refactor: ast package * feat: read -> taskfile * refactor: taskfile.Taskfile -> taskfile.Read * refactor: move merge function back into taskfile package * refactor: rename taskfile.go to read.go
This commit is contained in:
54
taskfile/node_file.go
Normal file
54
taskfile/node_file.go
Normal file
@@ -0,0 +1,54 @@
|
||||
package taskfile
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/go-task/task/v3/internal/filepathext"
|
||||
)
|
||||
|
||||
// A FileNode is a node that reads a taskfile from the local filesystem.
|
||||
type FileNode struct {
|
||||
*BaseNode
|
||||
Dir string
|
||||
Entrypoint string
|
||||
}
|
||||
|
||||
func NewFileNode(uri string, opts ...NodeOption) (*FileNode, error) {
|
||||
base := NewBaseNode(opts...)
|
||||
if uri == "" {
|
||||
d, err := os.Getwd()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
uri = d
|
||||
}
|
||||
path, err := Exists(uri)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &FileNode{
|
||||
BaseNode: base,
|
||||
Dir: filepath.Dir(path),
|
||||
Entrypoint: filepath.Base(path),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (node *FileNode) Location() string {
|
||||
return filepathext.SmartJoin(node.Dir, node.Entrypoint)
|
||||
}
|
||||
|
||||
func (node *FileNode) Remote() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (node *FileNode) Read(ctx context.Context) ([]byte, error) {
|
||||
f, err := os.Open(node.Location())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer f.Close()
|
||||
return io.ReadAll(f)
|
||||
}
|
||||
Reference in New Issue
Block a user