Skip to content

Commit 7a468a3

Browse files
committed
cio.copyIO: refactor to use cio.Close() (windows)
Use the existing `.Close()` method instead of implementing the same logic in this function. The defer sets `cios` to `nil` if an error occurred to preserve the existing behavior. Signed-off-by: Sebastiaan van Stijn <[email protected]>
1 parent 219fa3d commit 7a468a3

1 file changed

Lines changed: 7 additions & 15 deletions

File tree

cio/io_windows.go

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -43,19 +43,11 @@ func NewFIFOSetInDir(_, id string, terminal bool) (*FIFOSet, error) {
4343
}
4444

4545
func copyIO(fifos *FIFOSet, ioset *Streams) (_ *cio, retErr error) {
46-
var (
47-
set []io.Closer
48-
)
46+
cios := &cio{config: fifos.Config}
4947

5048
defer func() {
51-
if retErr == nil {
52-
return
53-
}
54-
for _, closer := range set {
55-
if closer == nil {
56-
continue
57-
}
58-
_ = closer.Close()
49+
if retErr != nil {
50+
_ = cios.Close()
5951
}
6052
}()
6153

@@ -64,7 +56,7 @@ func copyIO(fifos *FIFOSet, ioset *Streams) (_ *cio, retErr error) {
6456
if err != nil {
6557
return nil, errors.Wrapf(err, "failed to create stdin pipe %s", fifos.Stdin)
6658
}
67-
set = append(set, l)
59+
cios.closers = append(cios.closers, l)
6860

6961
go func() {
7062
c, err := l.Accept()
@@ -87,7 +79,7 @@ func copyIO(fifos *FIFOSet, ioset *Streams) (_ *cio, retErr error) {
8779
if err != nil {
8880
return nil, errors.Wrapf(err, "failed to create stdout pipe %s", fifos.Stdout)
8981
}
90-
set = append(set, l)
82+
cios.closers = append(cios.closers, l)
9183

9284
go func() {
9385
c, err := l.Accept()
@@ -110,7 +102,7 @@ func copyIO(fifos *FIFOSet, ioset *Streams) (_ *cio, retErr error) {
110102
if err != nil {
111103
return nil, errors.Wrapf(err, "failed to create stderr pipe %s", fifos.Stderr)
112104
}
113-
set = append(set, l)
105+
cios.closers = append(cios.closers, l)
114106

115107
go func() {
116108
c, err := l.Accept()
@@ -128,7 +120,7 @@ func copyIO(fifos *FIFOSet, ioset *Streams) (_ *cio, retErr error) {
128120
}()
129121
}
130122

131-
return &cio{config: fifos.Config, closers: set}, nil
123+
return cios, nil
132124
}
133125

134126
// NewDirectIO returns an IO implementation that exposes the IO streams as io.ReadCloser

0 commit comments

Comments
 (0)