Skip to content

Commit

Permalink
feat: stdin node
Browse files Browse the repository at this point in the history
  • Loading branch information
pd93 committed Feb 22, 2024
1 parent 38a06da commit bb9d582
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 10 deletions.
17 changes: 7 additions & 10 deletions setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,22 +67,19 @@ func (e *Executor) setCurrentDir() error {
return err
}
e.Dir = wd
} else {
var err error
e.Dir, err = filepath.Abs(e.Dir)
if err != nil {
return err
}
}

// Search for a taskfile
root, err := taskfile.ExistsWalk(e.Dir)
if err != nil {
return err
}
e.Dir = filepath.Dir(root)
e.Entrypoint = filepath.Base(root)

return nil
}

func (e *Executor) readTaskfile() error {
uri := filepath.Join(e.Dir, e.Entrypoint)
node, err := taskfile.NewNode(uri, e.Insecure)
node, err := taskfile.NewRootNode(e.Dir, e.Entrypoint, e.Insecure)
if err != nil {
return err
}
Expand Down
25 changes: 25 additions & 0 deletions taskfile/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package taskfile

import (
"context"
"os"
"path/filepath"
"strings"

"github.com/go-task/task/v3/errors"
Expand All @@ -16,6 +18,29 @@ type Node interface {
Remote() bool
}

func NewRootNode(
dir string,
entrypoint string,
insecure bool,
) (Node, error) {
// Check if there is something to read on STDIN
stat, _ := os.Stdin.Stat()
if (stat.Mode()&os.ModeCharDevice) == 0 && stat.Size() > 0 {
return NewStdinNode()
}
// If no entrypoint is specified, search for a taskfile
if entrypoint == "" {
root, err := ExistsWalk(dir)
if err != nil {
return nil, err
}
return NewNode(root, insecure)
}
// Use the specified entrypoint
uri := filepath.Join(dir, entrypoint)
return NewNode(uri, insecure)
}

func NewNode(
uri string,
insecure bool,
Expand Down
40 changes: 40 additions & 0 deletions taskfile/node_stdin.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit bb9d582

Please sign in to comment.