2024-01-25 14:22:10 +02:00
|
|
|
package taskfile
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2024-02-13 03:07:00 +02:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/go-task/task/v3/internal/execext"
|
|
|
|
"github.com/go-task/task/v3/internal/filepathext"
|
2024-01-25 14:22:10 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// A StdinNode is a node that reads a taskfile from the standard input stream.
|
|
|
|
type StdinNode struct {
|
|
|
|
*BaseNode
|
|
|
|
}
|
|
|
|
|
2024-01-25 14:36:31 +02:00
|
|
|
func NewStdinNode(dir string) (*StdinNode, error) {
|
2024-01-25 14:22:10 +02:00
|
|
|
return &StdinNode{
|
2024-03-04 20:00:28 +02:00
|
|
|
BaseNode: NewBaseNode(dir),
|
2024-01-25 14:22:10 +02:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (node *StdinNode) Location() string {
|
|
|
|
return "__stdin__"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (node *StdinNode) Remote() bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (node *StdinNode) Read(ctx context.Context) ([]byte, error) {
|
|
|
|
var stdin []byte
|
|
|
|
scanner := bufio.NewScanner(os.Stdin)
|
|
|
|
for scanner.Scan() {
|
|
|
|
stdin = fmt.Appendln(stdin, scanner.Text())
|
|
|
|
}
|
|
|
|
if err := scanner.Err(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return stdin, nil
|
|
|
|
}
|
2024-01-25 14:36:31 +02:00
|
|
|
|
2024-02-13 21:29:28 +02:00
|
|
|
func (node *StdinNode) 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
|
2024-02-13 21:28:42 +02:00
|
|
|
if strings.Contains(entrypoint, "://") {
|
|
|
|
return entrypoint, nil
|
2024-02-13 03:07:00 +02:00
|
|
|
}
|
|
|
|
|
2024-02-13 21:28:42 +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
|
|
|
|
}
|
|
|
|
|
|
|
|
return filepathext.SmartJoin(node.Dir(), path), nil
|
|
|
|
}
|
|
|
|
|
2024-02-13 21:29:28 +02:00
|
|
|
func (node *StdinNode) ResolveDir(dir string) (string, error) {
|
2024-02-13 21:28:42 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
return filepathext.SmartJoin(node.Dir(), path), nil
|
2024-01-25 14:36:31 +02:00
|
|
|
}
|
2024-06-28 18:07:43 +02:00
|
|
|
|
|
|
|
func (node *StdinNode) FilenameAndLastDir() (string, string) {
|
|
|
|
return "", "__stdin__"
|
|
|
|
}
|