2023-12-29 22:32:03 +02:00
|
|
|
package taskfile
|
2023-09-02 22:24:01 +02:00
|
|
|
|
|
|
|
import (
|
2023-09-12 23:42:54 +02:00
|
|
|
"context"
|
|
|
|
"io"
|
2023-09-02 22:24:01 +02:00
|
|
|
"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 {
|
2023-09-12 23:42:54 +02:00
|
|
|
*BaseNode
|
2023-09-02 22:24:01 +02:00
|
|
|
Dir string
|
|
|
|
Entrypoint string
|
|
|
|
}
|
|
|
|
|
2023-09-12 23:42:54 +02:00
|
|
|
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
|
|
|
|
}
|
2023-09-14 23:57:46 +02:00
|
|
|
path, err := Exists(uri)
|
2023-09-02 22:24:01 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &FileNode{
|
2023-09-12 23:42:54 +02:00
|
|
|
BaseNode: base,
|
2023-09-02 22:24:01 +02:00
|
|
|
Dir: filepath.Dir(path),
|
|
|
|
Entrypoint: filepath.Base(path),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (node *FileNode) Location() string {
|
|
|
|
return filepathext.SmartJoin(node.Dir, node.Entrypoint)
|
|
|
|
}
|
|
|
|
|
2023-09-12 23:42:54 +02:00
|
|
|
func (node *FileNode) Remote() bool {
|
|
|
|
return false
|
|
|
|
}
|
2023-09-02 22:24:01 +02:00
|
|
|
|
2023-09-12 23:42:54 +02:00
|
|
|
func (node *FileNode) Read(ctx context.Context) ([]byte, error) {
|
|
|
|
f, err := os.Open(node.Location())
|
2023-09-02 22:24:01 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
2023-09-12 23:42:54 +02:00
|
|
|
return io.ReadAll(f)
|
2023-09-02 22:24:01 +02:00
|
|
|
}
|