Skip to content

Commit

Permalink
cio.copyIO: refactor to use cio.Close() (windows)
Browse files Browse the repository at this point in the history
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 <github@gone.nl>
  • Loading branch information
thaJeztah committed Feb 1, 2021
1 parent 219fa3d commit 7a468a3
Showing 1 changed file with 7 additions and 15 deletions.
22 changes: 7 additions & 15 deletions cio/io_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,11 @@ func NewFIFOSetInDir(_, id string, terminal bool) (*FIFOSet, error) {
}

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

defer func() {
if retErr == nil {
return
}
for _, closer := range set {
if closer == nil {
continue
}
_ = closer.Close()
if retErr != nil {
_ = cios.Close()
}
}()

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

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

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

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

return &cio{config: fifos.Config, closers: set}, nil
return cios, nil
}

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

0 comments on commit 7a468a3

Please sign in to comment.