diff options
author | Patrick Steinhardt <psteinhardt@gitlab.com> | 2022-06-16 15:58:45 +0300 |
---|---|---|
committer | Patrick Steinhardt <psteinhardt@gitlab.com> | 2022-06-23 09:13:31 +0300 |
commit | 1bb94d32c13c5804ec337dde26540b2bc964f960 (patch) | |
tree | 9650f28ecb9c07b9f65290b9dcf624849d6b133c | |
parent | b4a7f8cc4892da19562b1928b7977f1447a4c177 (diff) |
command: Replace `StdinSentinel` with an option
The `StdinSentinel` value can be passed to `WithStdin()` in order to
tell the command package to set up the command for `Write()`ing to it.
Replace this with a new option `WithSetupStdin()` so that we can avoid
the use of sentinel values.
-rw-r--r-- | internal/command/command.go | 11 | ||||
-rw-r--r-- | internal/command/command_test.go | 2 | ||||
-rw-r--r-- | internal/command/option.go | 13 | ||||
-rw-r--r-- | internal/git/catfile/object_info_reader.go | 2 | ||||
-rw-r--r-- | internal/git/catfile/object_reader.go | 2 | ||||
-rw-r--r-- | internal/git/command_options.go | 8 | ||||
-rw-r--r-- | internal/git/updateref/updateref.go | 2 |
7 files changed, 24 insertions, 16 deletions
diff --git a/internal/command/command.go b/internal/command/command.go index 2ebd3d12f..5782e75dc 100644 --- a/internal/command/command.go +++ b/internal/command/command.go @@ -109,13 +109,6 @@ var ( // envInjector is responsible for injecting environment variables required for tracing into // the child process. envInjector = tracing.NewEnvInjector() - - // SetupStdin instructs New() to configure the stdin pipe of the command it is creating. - // This allows you call Write() on the command as if it is an ordinary io.Writer, sending - // data directly to the stdin of the process. - // - // You should not call Read() on this value - it is strictly for configuration! - SetupStdin io.Reader = stdinSentinel{} ) const ( @@ -210,9 +203,9 @@ func New(ctx context.Context, cmd *exec.Cmd, opts ...Option) (*Command, error) { // Three possible values for stdin: // * nil - Go implicitly uses /dev/null - // * SetupStdin - configure with cmd.StdinPipe(), allowing Write() to work + // * stdinSentinel - configure with cmd.StdinPipe(), allowing Write() to work // * Another io.Reader - becomes cmd.Stdin. Write() will not work - if cfg.stdin == SetupStdin { + if _, ok := cfg.stdin.(stdinSentinel); ok { pipe, err := cmd.StdinPipe() if err != nil { return nil, fmt.Errorf("GitCommand: stdin: %v", err) diff --git a/internal/command/command_test.go b/internal/command/command_test.go index 7bf69ba9c..dbe7d04dd 100644 --- a/internal/command/command_test.go +++ b/internal/command/command_test.go @@ -212,7 +212,7 @@ func TestNew_setupStdin(t *testing.T) { stdin := "Test value" var buf bytes.Buffer - cmd, err := New(ctx, exec.Command("cat"), WithStdin(SetupStdin), WithStdout(&buf)) + cmd, err := New(ctx, exec.Command("cat"), WithSetupStdin(), WithStdout(&buf)) require.NoError(t, err) _, err = fmt.Fprintf(cmd, "%s", stdin) diff --git a/internal/command/option.go b/internal/command/option.go index f31602254..7e4c0c64f 100644 --- a/internal/command/option.go +++ b/internal/command/option.go @@ -13,15 +13,22 @@ type config struct { // created. type Option func(cfg *config) -// WithStdin sets up the command to read from the given reader. If stdin is specified as SetupStdin, -// you will be able to write to the stdin of the subprocess by calling Write() on the returned -// Command. +// WithStdin sets up the command to read from the given reader. func WithStdin(stdin io.Reader) Option { return func(cfg *config) { cfg.stdin = stdin } } +// WithSetupStdin instructs New() to configure the stdin pipe of the command it is creating. This +// allows you call Write() on the command as if it is an ordinary io.Writer, sending data directly +// to the stdin of the process. +func WithSetupStdin() Option { + return func(cfg *config) { + cfg.stdin = stdinSentinel{} + } +} + // WithStdout sets up the command to write standard output to the given writer. func WithStdout(stdout io.Writer) Option { return func(cfg *config) { diff --git a/internal/git/catfile/object_info_reader.go b/internal/git/catfile/object_info_reader.go index f391bd128..84857ce70 100644 --- a/internal/git/catfile/object_info_reader.go +++ b/internal/git/catfile/object_info_reader.go @@ -146,7 +146,7 @@ func newObjectInfoReader( git.Flag{Name: "--buffer"}, }, }, - git.WithStdin(command.SetupStdin), + git.WithSetupStdin(), ) if err != nil { return nil, err diff --git a/internal/git/catfile/object_reader.go b/internal/git/catfile/object_reader.go index 4810a8207..516050301 100644 --- a/internal/git/catfile/object_reader.go +++ b/internal/git/catfile/object_reader.go @@ -67,7 +67,7 @@ func newObjectReader( git.Flag{Name: "--buffer"}, }, }, - git.WithStdin(command.SetupStdin), + git.WithSetupStdin(), ) if err != nil { return nil, err diff --git a/internal/git/command_options.go b/internal/git/command_options.go index ffa7e402c..f2ed3bb04 100644 --- a/internal/git/command_options.go +++ b/internal/git/command_options.go @@ -185,6 +185,14 @@ func WithStdin(r io.Reader) CmdOpt { } } +// WithSetupStdin sets up the command so that it can be `Write()`en to. +func WithSetupStdin() CmdOpt { + return func(_ context.Context, _ config.Cfg, _ CommandFactory, c *cmdCfg) error { + c.commandOpts = append(c.commandOpts, command.WithSetupStdin()) + return nil + } +} + // WithStdout sets the command's stdout. func WithStdout(w io.Writer) CmdOpt { return func(_ context.Context, _ config.Cfg, _ CommandFactory, c *cmdCfg) error { diff --git a/internal/git/updateref/updateref.go b/internal/git/updateref/updateref.go index a00f78c3e..cc97513cc 100644 --- a/internal/git/updateref/updateref.go +++ b/internal/git/updateref/updateref.go @@ -63,7 +63,7 @@ func New(ctx context.Context, repo git.RepositoryExecutor, opts ...UpdaterOpt) ( Flags: []git.Option{git.Flag{Name: "-z"}, git.Flag{Name: "--stdin"}}, }, txOption, - git.WithStdin(command.SetupStdin), + git.WithSetupStdin(), git.WithStderr(&stderr), ) if err != nil { |