mirror of
https://github.com/go-task/task.git
synced 2026-06-11 09:51:50 +00:00
feat: stdin node
This commit is contained in:
40
taskfile/node_stdin.go
Normal file
40
taskfile/node_stdin.go
Normal file
@@ -0,0 +1,40 @@
|
||||
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
|
||||
}
|
||||
|
||||
func NewStdinNode() (*StdinNode, error) {
|
||||
base := NewBaseNode()
|
||||
return &StdinNode{
|
||||
BaseNode: base,
|
||||
}, 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
|
||||
}
|
||||
Reference in New Issue
Block a user