blob: b8bd0eff0c2be388a544a8194061039353544d80 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
package data
import (
"context"
"io"
"io/ioutil"
"os"
"github.com/pkg/errors"
)
func readStdin(ctx context.Context, source *Source, args ...string) ([]byte, error) {
stdin := stdinFromContext(ctx)
b, err := ioutil.ReadAll(stdin)
if err != nil {
return nil, errors.Wrapf(err, "Can't read %s", stdin)
}
return b, nil
}
type stdinCtxKey struct{}
func ContextWithStdin(ctx context.Context, r io.Reader) context.Context {
return context.WithValue(ctx, stdinCtxKey{}, r)
}
func stdinFromContext(ctx context.Context) io.Reader {
if r, ok := ctx.Value(stdinCtxKey{}).(io.Reader); ok {
return r
}
return os.Stdin
}
|