distinguish stdout and stderr

Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
This commit is contained in:
Nicolas De Loof 2021-07-02 07:55:08 +02:00
parent 04d9bc49ed
commit 96e1e041d6
No known key found for this signature in database
GPG Key ID: 9858809D6F8F6E7E
7 changed files with 34 additions and 30 deletions

View File

@ -92,8 +92,9 @@ func runExec(ctx context.Context, backend api.Service, opts execOpts) error {
Detach: opts.detach,
WorkingDir: opts.workingDir,
Writer: os.Stdout,
Reader: os.Stdin,
Stdin: os.Stdin,
Stdout: os.Stdout,
Stderr: os.Stderr,
}
if execOpts.Tty {
@ -107,8 +108,9 @@ func runExec(ctx context.Context, backend api.Service, opts execOpts) error {
}
}()
execOpts.Writer = con
execOpts.Reader = con
execOpts.Stdin = con
execOpts.Stdout = con
execOpts.Stderr = con
}
exitCode, err := backend.Exec(ctx, project, execOpts)
if exitCode != 0 {

View File

@ -189,8 +189,9 @@ func runRun(ctx context.Context, backend api.Service, project *types.Project, op
Command: opts.Command,
Detach: opts.Detach,
AutoRemove: opts.Remove,
Writer: os.Stdout,
Reader: os.Stdin,
Stdin: os.Stdin,
Stdout: os.Stdout,
Stderr: os.Stderr,
Tty: !opts.noTty,
WorkingDir: opts.workdir,
User: opts.user,

View File

@ -125,7 +125,7 @@ func (kc KubeClient) Exec(ctx context.Context, projectName string, opts api.RunO
TTY: opts.Tty,
}
if opts.Reader == nil {
if opts.Stdin == nil {
option.Stdin = false
}
@ -141,9 +141,9 @@ func (kc KubeClient) Exec(ctx context.Context, projectName string, opts api.RunO
return err
}
return exec.Stream(remotecommand.StreamOptions{
Stdin: opts.Reader,
Stdout: opts.Writer,
Stderr: opts.Writer,
Stdin: opts.Stdin,
Stdout: opts.Stdout,
Stderr: opts.Stdout,
Tty: opts.Tty,
})
}

View File

@ -208,8 +208,9 @@ type RunOptions struct {
Entrypoint []string
Detach bool
AutoRemove bool
Writer io.WriteCloser
Reader io.ReadCloser
Stdin io.ReadCloser
Stdout io.WriteCloser
Stderr io.WriteCloser
Tty bool
WorkingDir string
User string

View File

@ -78,21 +78,21 @@ func (s *composeService) attachContainer(ctx context.Context, container moby.Con
Line: line,
})
})
_, _, err = s.attachContainerStreams(ctx, container.ID, service.Tty, nil, w)
_, _, err = s.attachContainerStreams(ctx, container.ID, service.Tty, nil, w, w)
return err
}
func (s *composeService) attachContainerStreams(ctx context.Context, container string, tty bool, r io.ReadCloser, w io.Writer) (func(), chan bool, error) {
func (s *composeService) attachContainerStreams(ctx context.Context, container string, tty bool, stdin io.ReadCloser, stdout, stderr io.Writer) (func(), chan bool, error) {
detached := make(chan bool)
var (
in *streams.In
restore = func() { /* noop */ }
)
if r != nil {
in = streams.NewIn(r)
if stdin != nil {
in = streams.NewIn(stdin)
}
stdin, stdout, err := s.getContainerStreams(ctx, container)
streamIn, streamOut, err := s.getContainerStreams(ctx, container)
if err != nil {
return restore, detached, err
}
@ -102,10 +102,10 @@ func (s *composeService) attachContainerStreams(ctx context.Context, container s
if in != nil {
in.Close() //nolint:errcheck
}
stdout.Close() //nolint:errcheck
streamOut.Close() //nolint:errcheck
}()
if in != nil && stdin != nil {
if in != nil && streamIn != nil {
if in.IsTerminal() {
state, err := term.SetRawTerminal(in.FD())
if err != nil {
@ -116,19 +116,19 @@ func (s *composeService) attachContainerStreams(ctx context.Context, container s
}
}
go func() {
_, err := io.Copy(stdin, r)
_, err := io.Copy(streamIn, stdin)
if _, ok := err.(term.EscapeError); ok {
close(detached)
}
}()
}
if w != nil {
if stdout != nil {
go func() {
if tty {
io.Copy(w, stdout) // nolint:errcheck
io.Copy(stdout, streamOut) // nolint:errcheck
} else {
stdcopy.StdCopy(w, w, stdout) // nolint:errcheck
stdcopy.StdCopy(stdout, stderr, streamOut) // nolint:errcheck
}
}()
}

View File

@ -96,12 +96,12 @@ func (s *composeService) interactiveExec(ctx context.Context, opts api.RunOption
stdout := ContainerStdout{HijackedResponse: resp}
stdin := ContainerStdin{HijackedResponse: resp}
r, err := s.getEscapeKeyProxy(opts.Reader)
r, err := s.getEscapeKeyProxy(opts.Stdin)
if err != nil {
return err
}
in := streams.NewIn(opts.Reader)
in := streams.NewIn(opts.Stdin)
if in.IsTerminal() {
state, err := term.SetRawTerminal(in.FD())
if err != nil {
@ -112,10 +112,10 @@ func (s *composeService) interactiveExec(ctx context.Context, opts api.RunOption
go func() {
if opts.Tty {
_, err := io.Copy(opts.Writer, stdout)
_, err := io.Copy(opts.Stdout, stdout)
outputDone <- err
} else {
_, err := stdcopy.StdCopy(opts.Writer, opts.Writer, stdout)
_, err := stdcopy.StdCopy(opts.Stdout, opts.Stderr, stdout)
outputDone <- err
}
}()

View File

@ -74,15 +74,15 @@ func (s *composeService) RunOneOffContainer(ctx context.Context, project *types.
if err != nil {
return 0, err
}
fmt.Fprintln(opts.Writer, containerID)
fmt.Fprintln(opts.Stdout, containerID)
return 0, nil
}
r, err := s.getEscapeKeyProxy(opts.Reader)
r, err := s.getEscapeKeyProxy(opts.Stdin)
if err != nil {
return 0, err
}
restore, detachC, err := s.attachContainerStreams(ctx, containerID, service.Tty, r, opts.Writer)
restore, detachC, err := s.attachContainerStreams(ctx, containerID, service.Tty, r, opts.Stdout, opts.Stderr)
if err != nil {
return 0, err
}