-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: add contextual stdio for better testing
- Loading branch information
Showing
5 changed files
with
58 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package internal | ||
|
||
import ( | ||
"context" | ||
"io" | ||
"os" | ||
) | ||
|
||
type ( | ||
stdoutKey struct{} | ||
stderrKey struct{} | ||
stdinKey struct{} | ||
) | ||
|
||
func WithStdout(ctx context.Context, w io.Writer) context.Context { | ||
return context.WithValue(ctx, stdoutKey{}, w) | ||
} | ||
|
||
func Stdout(ctx context.Context) io.Writer { | ||
w, ok := ctx.Value(stdoutKey{}).(io.Writer) | ||
if !ok { | ||
return os.Stdout | ||
} | ||
return w | ||
} | ||
|
||
func WithStderr(ctx context.Context, w io.Writer) context.Context { | ||
return context.WithValue(ctx, stderrKey{}, w) | ||
} | ||
|
||
func Stderr(ctx context.Context) io.Writer { | ||
w, ok := ctx.Value(stderrKey{}).(io.Writer) | ||
if !ok { | ||
return os.Stderr | ||
} | ||
return w | ||
} | ||
|
||
func WithStdin(ctx context.Context, r io.Reader) context.Context { | ||
return context.WithValue(ctx, stdinKey{}, r) | ||
} | ||
|
||
func Stdin(ctx context.Context) io.Reader { | ||
r, ok := ctx.Value(stdinKey{}).(io.Reader) | ||
if !ok { | ||
return os.Stdin | ||
} | ||
return r | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters