node_stdin.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package taskfile
  2. import (
  3. "bufio"
  4. "context"
  5. "fmt"
  6. "os"
  7. "strings"
  8. "github.com/go-task/task/v3/internal/execext"
  9. "github.com/go-task/task/v3/internal/filepathext"
  10. )
  11. // A StdinNode is a node that reads a taskfile from the standard input stream.
  12. type StdinNode struct {
  13. *BaseNode
  14. }
  15. func NewStdinNode(dir string) (*StdinNode, error) {
  16. return &StdinNode{
  17. BaseNode: NewBaseNode(dir),
  18. }, nil
  19. }
  20. func (node *StdinNode) Location() string {
  21. return "__stdin__"
  22. }
  23. func (node *StdinNode) Remote() bool {
  24. return false
  25. }
  26. func (node *StdinNode) Read(ctx context.Context) ([]byte, error) {
  27. var stdin []byte
  28. scanner := bufio.NewScanner(os.Stdin)
  29. for scanner.Scan() {
  30. stdin = fmt.Appendln(stdin, scanner.Text())
  31. }
  32. if err := scanner.Err(); err != nil {
  33. return nil, err
  34. }
  35. return stdin, nil
  36. }
  37. func (node *StdinNode) ResolveEntrypoint(entrypoint string) (string, error) {
  38. // If the file is remote, we don't need to resolve the path
  39. if strings.Contains(entrypoint, "://") {
  40. return entrypoint, nil
  41. }
  42. path, err := execext.Expand(entrypoint)
  43. if err != nil {
  44. return "", err
  45. }
  46. if filepathext.IsAbs(path) {
  47. return path, nil
  48. }
  49. return filepathext.SmartJoin(node.Dir(), path), nil
  50. }
  51. func (node *StdinNode) ResolveDir(dir string) (string, error) {
  52. path, err := execext.Expand(dir)
  53. if err != nil {
  54. return "", err
  55. }
  56. if filepathext.IsAbs(path) {
  57. return path, nil
  58. }
  59. return filepathext.SmartJoin(node.Dir(), path), nil
  60. }
  61. func (node *StdinNode) FilenameAndLastDir() (string, string) {
  62. return "", "__stdin__"
  63. }