2024-01-25 12:22:10 +00:00
|
|
|
package taskfile
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
|
|
|
// A StdinNode is a node that reads a taskfile from the standard input stream.
|
|
|
|
type StdinNode struct {
|
|
|
|
*BaseNode
|
2024-01-25 12:36:31 +00:00
|
|
|
Dir string
|
2024-01-25 12:22:10 +00:00
|
|
|
}
|
|
|
|
|
2024-01-25 12:36:31 +00:00
|
|
|
func NewStdinNode(dir string) (*StdinNode, error) {
|
2024-01-25 12:22:10 +00:00
|
|
|
base := NewBaseNode()
|
|
|
|
return &StdinNode{
|
|
|
|
BaseNode: base,
|
2024-01-25 12:36:31 +00:00
|
|
|
Dir: dir,
|
2024-01-25 12:22:10 +00: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 12:36:31 +00:00
|
|
|
|
|
|
|
func (node *StdinNode) BaseDir() string {
|
|
|
|
return node.Dir
|
|
|
|
}
|