1
0
mirror of https://github.com/go-task/task.git synced 2024-12-12 10:45:49 +02:00
task/taskfile/node_file.go

116 lines
2.7 KiB
Go
Raw Normal View History

package taskfile
import (
"context"
"io"
"os"
"path/filepath"
2024-02-13 03:07:00 +02:00
"strings"
2024-02-13 03:07:00 +02:00
"github.com/go-task/task/v3/internal/execext"
"github.com/go-task/task/v3/internal/filepathext"
2024-02-13 03:07:00 +02:00
"github.com/go-task/task/v3/internal/logger"
)
// A FileNode is a node that reads a taskfile from the local filesystem.
type FileNode struct {
*BaseNode
Entrypoint string
}
2024-02-13 03:07:00 +02:00
func NewFileNode(l *logger.Logger, entrypoint, dir string, opts ...NodeOption) (*FileNode, error) {
var err error
base := NewBaseNode(opts...)
2024-02-13 03:07:00 +02:00
entrypoint, dir, err = resolveFileNodeEntrypointAndDir(l, entrypoint, dir)
if err != nil {
return nil, err
}
2024-02-13 03:07:00 +02:00
base.dir = dir
return &FileNode{
BaseNode: base,
2024-02-13 03:07:00 +02:00
Entrypoint: entrypoint,
}, nil
}
func (node *FileNode) Location() string {
2024-02-13 03:07:00 +02:00
return 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)
}
2024-02-13 03:07:00 +02:00
// resolveFileNodeEntrypointAndDir resolves checks the values of entrypoint and dir and
// populates them with default values if necessary.
func resolveFileNodeEntrypointAndDir(l *logger.Logger, entrypoint, dir string) (string, string, error) {
var err error
if entrypoint != "" {
entrypoint, err = Exists(l, entrypoint)
if err != nil {
return "", "", err
}
if dir == "" {
dir = filepath.Dir(entrypoint)
}
return entrypoint, dir, nil
}
if dir == "" {
dir, err = os.Getwd()
if err != nil {
return "", "", err
}
}
entrypoint, err = ExistsWalk(l, dir)
if err != nil {
return "", "", err
}
dir = filepath.Dir(entrypoint)
return entrypoint, dir, nil
}
2024-02-13 21:29:28 +02:00
func (node *FileNode) ResolveEntrypoint(entrypoint string) (string, error) {
2024-02-13 03:07:00 +02:00
// If the file is remote, we don't need to resolve the path
if strings.Contains(entrypoint, "://") {
return entrypoint, nil
2024-02-13 03:07:00 +02:00
}
path, err := execext.Expand(entrypoint)
2024-02-13 03:07:00 +02:00
if err != nil {
return "", err
}
if filepathext.IsAbs(path) {
return path, nil
}
// NOTE: Uses the directory of the entrypoint (Taskfile), not the current working directory
// This means that files are included relative to one another
entrypointDir := filepath.Dir(node.Entrypoint)
return filepathext.SmartJoin(entrypointDir, path), nil
}
2024-02-13 21:29:28 +02:00
func (node *FileNode) ResolveDir(dir string) (string, error) {
path, err := execext.Expand(dir)
2024-02-13 03:07:00 +02:00
if err != nil {
return "", err
}
if filepathext.IsAbs(path) {
return path, nil
}
// NOTE: Uses the directory of the entrypoint (Taskfile), not the current working directory
// This means that files are included relative to one another
entrypointDir := filepath.Dir(node.Entrypoint)
return filepathext.SmartJoin(entrypointDir, path), nil
}