Skip to content

Commit

Permalink
add stdin as exec option (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rasek91 authored Apr 13, 2023
1 parent ebc233c commit 7a7e0ba
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
9 changes: 9 additions & 0 deletions env.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,10 +191,19 @@ type runnable interface {
type ExecOption func(o *ExecOptions)

type ExecOptions struct {
Stdin io.Reader
Stdout io.Writer
Stderr io.Writer
}

// WithExecOptionStdin sets stdin reader to be used when exec is performed.
// By default, it is nil.
func WithExecOptionStdin(stdin io.Reader) ExecOption {
return func(o *ExecOptions) {
o.Stdin = stdin
}
}

// WithExecOptionStdout sets stdout writer to be used when exec is performed.
// By default, it is streaming to the env logger.
func WithExecOptionStdout(stdout io.Writer) ExecOption {
Expand Down
6 changes: 6 additions & 0 deletions env_docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -677,7 +677,13 @@ func (d *dockerRunnable) Exec(command Command, opts ...ExecOption) error {
args := []string{"exec", d.containerName()}
args = append(args, command.Cmd)
args = append(args, command.Args...)
if o.Stdin != nil {
args = append(args[:1], append([]string{"-i"}, args[1:]...)...)
}
cmd := d.env.exec("docker", args...)
if o.Stdin != nil {
cmd.Stdin = o.Stdin
}
cmd.Stdout = o.Stdout
cmd.Stderr = o.Stderr
return cmd.Run()
Expand Down

0 comments on commit 7a7e0ba

Please sign in to comment.