Skip to content
This repository has been archived by the owner on Nov 27, 2023. It is now read-only.

Commit

Permalink
distinguish stdout and stderr
Browse files Browse the repository at this point in the history
Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
  • Loading branch information
ndeloof committed Jul 2, 2021
1 parent 04d9bc4 commit 96e1e04
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 30 deletions.
10 changes: 6 additions & 4 deletions cmd/compose/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
5 changes: 3 additions & 2 deletions cmd/compose/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 4 additions & 4 deletions kube/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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,
})
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 11 additions & 11 deletions pkg/compose/attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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 {
Expand All @@ -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
}
}()
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/compose/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
}()
Expand Down
6 changes: 3 additions & 3 deletions pkg/compose/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 96e1e04

Please sign in to comment.