Browse Source

Merge pull request #1878 from ndeloof/exec_streams

Nicolas De loof 4 years ago
parent
commit
4049feb74d
7 changed files with 34 additions and 30 deletions
  1. 6 4
      cmd/compose/exec.go
  2. 3 2
      cmd/compose/run.go
  3. 4 4
      kube/client/client.go
  4. 3 2
      pkg/api/api.go
  5. 11 11
      pkg/compose/attach.go
  6. 4 4
      pkg/compose/exec.go
  7. 3 3
      pkg/compose/run.go

+ 6 - 4
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 {

+ 3 - 2
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,

+ 4 - 4
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,
 	})
 }

+ 3 - 2
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

+ 11 - 11
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
 			}
 		}()
 	}

+ 4 - 4
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
 		}
 	}()

+ 3 - 3
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
 	}