Skip to content
This repository was archived by the owner on Jul 18, 2025. It is now read-only.

Commit 4049feb

Browse files
authored
Merge pull request #1878 from ndeloof/exec_streams
2 parents 5ea2b20 + 96e1e04 commit 4049feb

File tree

7 files changed

+34
-30
lines changed

7 files changed

+34
-30
lines changed

cmd/compose/exec.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,9 @@ func runExec(ctx context.Context, backend api.Service, opts execOpts) error {
9292
Detach: opts.detach,
9393
WorkingDir: opts.workingDir,
9494

95-
Writer: os.Stdout,
96-
Reader: os.Stdin,
95+
Stdin: os.Stdin,
96+
Stdout: os.Stdout,
97+
Stderr: os.Stderr,
9798
}
9899

99100
if execOpts.Tty {
@@ -107,8 +108,9 @@ func runExec(ctx context.Context, backend api.Service, opts execOpts) error {
107108
}
108109
}()
109110

110-
execOpts.Writer = con
111-
execOpts.Reader = con
111+
execOpts.Stdin = con
112+
execOpts.Stdout = con
113+
execOpts.Stderr = con
112114
}
113115
exitCode, err := backend.Exec(ctx, project, execOpts)
114116
if exitCode != 0 {

cmd/compose/run.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,9 @@ func runRun(ctx context.Context, backend api.Service, project *types.Project, op
189189
Command: opts.Command,
190190
Detach: opts.Detach,
191191
AutoRemove: opts.Remove,
192-
Writer: os.Stdout,
193-
Reader: os.Stdin,
192+
Stdin: os.Stdin,
193+
Stdout: os.Stdout,
194+
Stderr: os.Stderr,
194195
Tty: !opts.noTty,
195196
WorkingDir: opts.workdir,
196197
User: opts.user,

kube/client/client.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ func (kc KubeClient) Exec(ctx context.Context, projectName string, opts api.RunO
125125
TTY: opts.Tty,
126126
}
127127

128-
if opts.Reader == nil {
128+
if opts.Stdin == nil {
129129
option.Stdin = false
130130
}
131131

@@ -141,9 +141,9 @@ func (kc KubeClient) Exec(ctx context.Context, projectName string, opts api.RunO
141141
return err
142142
}
143143
return exec.Stream(remotecommand.StreamOptions{
144-
Stdin: opts.Reader,
145-
Stdout: opts.Writer,
146-
Stderr: opts.Writer,
144+
Stdin: opts.Stdin,
145+
Stdout: opts.Stdout,
146+
Stderr: opts.Stdout,
147147
Tty: opts.Tty,
148148
})
149149
}

pkg/api/api.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,9 @@ type RunOptions struct {
208208
Entrypoint []string
209209
Detach bool
210210
AutoRemove bool
211-
Writer io.WriteCloser
212-
Reader io.ReadCloser
211+
Stdin io.ReadCloser
212+
Stdout io.WriteCloser
213+
Stderr io.WriteCloser
213214
Tty bool
214215
WorkingDir string
215216
User string

pkg/compose/attach.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -78,21 +78,21 @@ func (s *composeService) attachContainer(ctx context.Context, container moby.Con
7878
Line: line,
7979
})
8080
})
81-
_, _, err = s.attachContainerStreams(ctx, container.ID, service.Tty, nil, w)
81+
_, _, err = s.attachContainerStreams(ctx, container.ID, service.Tty, nil, w, w)
8282
return err
8383
}
8484

85-
func (s *composeService) attachContainerStreams(ctx context.Context, container string, tty bool, r io.ReadCloser, w io.Writer) (func(), chan bool, error) {
85+
func (s *composeService) attachContainerStreams(ctx context.Context, container string, tty bool, stdin io.ReadCloser, stdout, stderr io.Writer) (func(), chan bool, error) {
8686
detached := make(chan bool)
8787
var (
8888
in *streams.In
8989
restore = func() { /* noop */ }
9090
)
91-
if r != nil {
92-
in = streams.NewIn(r)
91+
if stdin != nil {
92+
in = streams.NewIn(stdin)
9393
}
9494

95-
stdin, stdout, err := s.getContainerStreams(ctx, container)
95+
streamIn, streamOut, err := s.getContainerStreams(ctx, container)
9696
if err != nil {
9797
return restore, detached, err
9898
}
@@ -102,10 +102,10 @@ func (s *composeService) attachContainerStreams(ctx context.Context, container s
102102
if in != nil {
103103
in.Close() //nolint:errcheck
104104
}
105-
stdout.Close() //nolint:errcheck
105+
streamOut.Close() //nolint:errcheck
106106
}()
107107

108-
if in != nil && stdin != nil {
108+
if in != nil && streamIn != nil {
109109
if in.IsTerminal() {
110110
state, err := term.SetRawTerminal(in.FD())
111111
if err != nil {
@@ -116,19 +116,19 @@ func (s *composeService) attachContainerStreams(ctx context.Context, container s
116116
}
117117
}
118118
go func() {
119-
_, err := io.Copy(stdin, r)
119+
_, err := io.Copy(streamIn, stdin)
120120
if _, ok := err.(term.EscapeError); ok {
121121
close(detached)
122122
}
123123
}()
124124
}
125125

126-
if w != nil {
126+
if stdout != nil {
127127
go func() {
128128
if tty {
129-
io.Copy(w, stdout) // nolint:errcheck
129+
io.Copy(stdout, streamOut) // nolint:errcheck
130130
} else {
131-
stdcopy.StdCopy(w, w, stdout) // nolint:errcheck
131+
stdcopy.StdCopy(stdout, stderr, streamOut) // nolint:errcheck
132132
}
133133
}()
134134
}

pkg/compose/exec.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,12 @@ func (s *composeService) interactiveExec(ctx context.Context, opts api.RunOption
9696

9797
stdout := ContainerStdout{HijackedResponse: resp}
9898
stdin := ContainerStdin{HijackedResponse: resp}
99-
r, err := s.getEscapeKeyProxy(opts.Reader)
99+
r, err := s.getEscapeKeyProxy(opts.Stdin)
100100
if err != nil {
101101
return err
102102
}
103103

104-
in := streams.NewIn(opts.Reader)
104+
in := streams.NewIn(opts.Stdin)
105105
if in.IsTerminal() {
106106
state, err := term.SetRawTerminal(in.FD())
107107
if err != nil {
@@ -112,10 +112,10 @@ func (s *composeService) interactiveExec(ctx context.Context, opts api.RunOption
112112

113113
go func() {
114114
if opts.Tty {
115-
_, err := io.Copy(opts.Writer, stdout)
115+
_, err := io.Copy(opts.Stdout, stdout)
116116
outputDone <- err
117117
} else {
118-
_, err := stdcopy.StdCopy(opts.Writer, opts.Writer, stdout)
118+
_, err := stdcopy.StdCopy(opts.Stdout, opts.Stderr, stdout)
119119
outputDone <- err
120120
}
121121
}()

pkg/compose/run.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,15 @@ func (s *composeService) RunOneOffContainer(ctx context.Context, project *types.
7474
if err != nil {
7575
return 0, err
7676
}
77-
fmt.Fprintln(opts.Writer, containerID)
77+
fmt.Fprintln(opts.Stdout, containerID)
7878
return 0, nil
7979
}
8080

81-
r, err := s.getEscapeKeyProxy(opts.Reader)
81+
r, err := s.getEscapeKeyProxy(opts.Stdin)
8282
if err != nil {
8383
return 0, err
8484
}
85-
restore, detachC, err := s.attachContainerStreams(ctx, containerID, service.Tty, r, opts.Writer)
85+
restore, detachC, err := s.attachContainerStreams(ctx, containerID, service.Tty, r, opts.Stdout, opts.Stderr)
8686
if err != nil {
8787
return 0, err
8888
}

0 commit comments

Comments
 (0)