github.com/u-root/u-root@v7.0.1-0.20200915234505-ad7babab0a8e+incompatible/pkg/uio/multiwriter.go (about)

     1  // Copyright 2019 the u-root Authors. All rights reserved
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package uio
     6  
     7  import (
     8  	"io"
     9  )
    10  
    11  type multiCloser struct {
    12  	io.Writer
    13  	writers []io.Writer
    14  }
    15  
    16  // Close implements io.Closer and closes any io.Writers that are also
    17  // io.Closers.
    18  func (mc *multiCloser) Close() error {
    19  	var allErr error
    20  	for _, w := range mc.writers {
    21  		if c, ok := w.(io.Closer); ok {
    22  			if err := c.Close(); err != nil {
    23  				allErr = err
    24  			}
    25  		}
    26  	}
    27  	return allErr
    28  }
    29  
    30  // MultiWriteCloser is an io.MultiWriter that has an io.Closer and attempts to
    31  // close those w's that have optional io.Closers.
    32  func MultiWriteCloser(w ...io.Writer) io.WriteCloser {
    33  	return &multiCloser{
    34  		Writer:  io.MultiWriter(w...),
    35  		writers: w,
    36  	}
    37  }