Skip to content

Commit 35f9138

Browse files
committed
refactor(attach): simplify attachContainerStreams signature
1 parent 2fcc415 commit 35f9138

File tree

1 file changed

+5
-49
lines changed

1 file changed

+5
-49
lines changed

pkg/compose/attach.go

Lines changed: 5 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,8 @@ import (
2424
"strings"
2525

2626
"github.com/compose-spec/compose-go/v2/types"
27-
"github.com/docker/cli/cli/streams"
2827
containerType "github.com/docker/docker/api/types/container"
2928
"github.com/docker/docker/pkg/stdcopy"
30-
"github.com/moby/term"
3129
"github.com/sirupsen/logrus"
3230

3331
"github.com/docker/compose/v5/pkg/api"
@@ -95,60 +93,18 @@ func (s *composeService) doAttachContainer(ctx context.Context, service, id, nam
9593
})
9694
})
9795

98-
restore, _, err := s.attachContainerStreams(ctx, id, inspect.Config.Tty, nil, wOut, wErr)
96+
err = s.attachContainerStreams(ctx, id, inspect.Config.Tty, wOut, wErr)
9997
if err != nil {
10098
return err
10199
}
102-
defer restore()
103100

104101
return nil
105102
}
106103

107-
//nolint:gocyclo
108-
func (s *composeService) attachContainerStreams(ctx context.Context, container string, tty bool, stdin io.ReadCloser, stdout, stderr io.WriteCloser) (func(), chan bool, error) {
109-
detached := make(chan bool)
110-
restore := func() { /* noop */ }
111-
if stdin != nil {
112-
in := streams.NewIn(stdin)
113-
if in.IsTerminal() {
114-
state, err := term.SetRawTerminal(in.FD())
115-
if err != nil {
116-
return restore, detached, err
117-
}
118-
restore = func() {
119-
err := term.RestoreTerminal(in.FD(), state)
120-
if err != nil {
121-
logrus.Warnf("failed to restore terminal: %v", err)
122-
}
123-
}
124-
}
125-
}
126-
127-
streamIn, streamOut, err := s.getContainerStreams(ctx, container)
104+
func (s *composeService) attachContainerStreams(ctx context.Context, container string, tty bool, stdout, stderr io.WriteCloser) error {
105+
_, streamOut, err := s.getContainerStreams(ctx, container)
128106
if err != nil {
129-
return restore, detached, err
130-
}
131-
132-
go func() {
133-
<-ctx.Done()
134-
if stdin != nil {
135-
err := stdin.Close()
136-
if err != nil {
137-
logrus.Debugf("failed to close stdin: %v", err)
138-
}
139-
}
140-
}()
141-
142-
if streamIn != nil && stdin != nil {
143-
go func() {
144-
_, err := io.Copy(streamIn, stdin)
145-
var escapeErr term.EscapeError
146-
if errors.As(err, &escapeErr) {
147-
close(detached)
148-
} else if err != nil && !errors.Is(err, io.EOF) {
149-
logrus.Debugf("stdin copy error for container %s: %v", container, err)
150-
}
151-
}()
107+
return err
152108
}
153109

154110
if stdout != nil {
@@ -176,7 +132,7 @@ func (s *composeService) attachContainerStreams(ctx context.Context, container s
176132
}
177133
}()
178134
}
179-
return restore, detached, nil
135+
return nil
180136
}
181137

182138
func (s *composeService) getContainerStreams(ctx context.Context, container string) (io.WriteCloser, io.ReadCloser, error) {

0 commit comments

Comments
 (0)