1
0
mirror of https://github.com/go-task/task.git synced 2025-04-19 12:12:27 +02:00

fix: resolve directory correctly when using --dir

This commit is contained in:
Pete Davison 2024-01-25 12:36:31 +00:00
parent bb9d582255
commit c7ba42b81a
5 changed files with 19 additions and 5 deletions

View File

@ -16,6 +16,7 @@ type Node interface {
Location() string Location() string
Optional() bool Optional() bool
Remote() bool Remote() bool
BaseDir() string
} }
func NewRootNode( func NewRootNode(
@ -26,7 +27,7 @@ func NewRootNode(
// Check if there is something to read on STDIN // Check if there is something to read on STDIN
stat, _ := os.Stdin.Stat() stat, _ := os.Stdin.Stat()
if (stat.Mode()&os.ModeCharDevice) == 0 && stat.Size() > 0 { if (stat.Mode()&os.ModeCharDevice) == 0 && stat.Size() > 0 {
return NewStdinNode() return NewStdinNode(dir)
} }
// If no entrypoint is specified, search for a taskfile // If no entrypoint is specified, search for a taskfile
if entrypoint == "" { if entrypoint == "" {

View File

@ -52,3 +52,7 @@ func (node *FileNode) Read(ctx context.Context) ([]byte, error) {
defer f.Close() defer f.Close()
return io.ReadAll(f) return io.ReadAll(f)
} }
func (node *FileNode) BaseDir() string {
return node.Dir
}

View File

@ -65,3 +65,7 @@ func (node *HTTPNode) Read(ctx context.Context) ([]byte, error) {
return b, nil return b, nil
} }
func (node *HTTPNode) BaseDir() string {
return ""
}

View File

@ -10,12 +10,14 @@ import (
// A StdinNode is a node that reads a taskfile from the standard input stream. // A StdinNode is a node that reads a taskfile from the standard input stream.
type StdinNode struct { type StdinNode struct {
*BaseNode *BaseNode
Dir string
} }
func NewStdinNode() (*StdinNode, error) { func NewStdinNode(dir string) (*StdinNode, error) {
base := NewBaseNode() base := NewBaseNode()
return &StdinNode{ return &StdinNode{
BaseNode: base, BaseNode: base,
Dir: dir,
}, nil }, nil
} }
@ -38,3 +40,7 @@ func (node *StdinNode) Read(ctx context.Context) ([]byte, error) {
} }
return stdin, nil return stdin, nil
} }
func (node *StdinNode) BaseDir() string {
return node.Dir
}

View File

@ -48,12 +48,11 @@ func Read(
return nil, &errors.TaskfileVersionCheckError{URI: node.Location()} return nil, &errors.TaskfileVersionCheckError{URI: node.Location()}
} }
// Annotate any included Taskfile reference with a base directory for resolving relative paths if dir := node.BaseDir(); dir != "" {
if node, isFileNode := node.(*FileNode); isFileNode {
_ = tf.Includes.Range(func(namespace string, include ast.Include) error { _ = tf.Includes.Range(func(namespace string, include ast.Include) error {
// Set the base directory for resolving relative paths, but only if not already set // Set the base directory for resolving relative paths, but only if not already set
if include.BaseDir == "" { if include.BaseDir == "" {
include.BaseDir = node.Dir include.BaseDir = dir
tf.Includes.Set(namespace, include) tf.Includes.Set(namespace, include)
} }
return nil return nil