From 96e1e041d606e4c79f1ddc35681d79d6baea01a4 Mon Sep 17 00:00:00 2001 From: Nicolas De Loof Date: Fri, 2 Jul 2021 07:55:08 +0200 Subject: [PATCH] distinguish stdout and stderr Signed-off-by: Nicolas De Loof --- cmd/compose/exec.go | 10 ++++++---- cmd/compose/run.go | 5 +++-- kube/client/client.go | 8 ++++---- pkg/api/api.go | 5 +++-- pkg/compose/attach.go | 22 +++++++++++----------- pkg/compose/exec.go | 8 ++++---- pkg/compose/run.go | 6 +++--- 7 files changed, 34 insertions(+), 30 deletions(-) diff --git a/cmd/compose/exec.go b/cmd/compose/exec.go index 7350a9a07..ce6220f6e 100644 --- a/cmd/compose/exec.go +++ b/cmd/compose/exec.go @@ -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 { diff --git a/cmd/compose/run.go b/cmd/compose/run.go index 61b50ee6a..aff9934cb 100644 --- a/cmd/compose/run.go +++ b/cmd/compose/run.go @@ -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, diff --git a/kube/client/client.go b/kube/client/client.go index 5b24a7262..7f4200cb0 100644 --- a/kube/client/client.go +++ b/kube/client/client.go @@ -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, }) } diff --git a/pkg/api/api.go b/pkg/api/api.go index ebed72b57..9ad5959f4 100644 --- a/pkg/api/api.go +++ b/pkg/api/api.go @@ -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 diff --git a/pkg/compose/attach.go b/pkg/compose/attach.go index d13ce79a1..a06770545 100644 --- a/pkg/compose/attach.go +++ b/pkg/compose/attach.go @@ -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 } }() } diff --git a/pkg/compose/exec.go b/pkg/compose/exec.go index 5cb79e18d..1dcca02f2 100644 --- a/pkg/compose/exec.go +++ b/pkg/compose/exec.go @@ -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 } }() diff --git a/pkg/compose/run.go b/pkg/compose/run.go index 251951388..c59234593 100644 --- a/pkg/compose/run.go +++ b/pkg/compose/run.go @@ -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 }