From cec23e66f9bd5022845162ae4dd3f2633b5236fa Mon Sep 17 00:00:00 2001 From: Dave Henderson Date: Sat, 28 May 2022 19:24:48 -0400 Subject: General refactoring & cleanup Signed-off-by: Dave Henderson --- data/datasource_stdin.go | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) (limited to 'data/datasource_stdin.go') diff --git a/data/datasource_stdin.go b/data/datasource_stdin.go index 007d935b..b8bd0eff 100644 --- a/data/datasource_stdin.go +++ b/data/datasource_stdin.go @@ -4,17 +4,31 @@ import ( "context" "io" "io/ioutil" + "os" "github.com/pkg/errors" ) -// stdin - for overriding in tests -var stdin io.Reader - 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 +} -- cgit v1.2.3