1package script
2
3import (
4	"bytes"
5	"io"
6	"io/ioutil"
7	"os"
8	"strings"
9)
10
11// From creates a stream from a reader.
12func From(name string, r io.Reader) Stream {
13	return Stream{stage: name, r: r}
14}
15
16// Writer creates a stream from a function that writes to a writer.
17func Writer(name string, writer func(io.Writer) error) Stream {
18	b := bytes.NewBuffer(nil)
19	err := writer(b)
20	return Stream{stage: name, r: b, err: err}
21}
22
23// Stdin starts a stream from stdin.
24func Stdin() Stream {
25	stdin := ioutil.NopCloser(os.Stdin) // Prevent closing of stdin.
26	return From("stdin", stdin)
27}
28
29// Echo writes to stdout.
30//
31// Shell command: `echo <s>`
32func Echo(s string) Stream {
33	return From("echo", strings.NewReader(s+"\n"))
34}
35