github.com/fighterlyt/hugo@v0.47.1/common/hugio/writers.go (about)

     1  // Copyright 2018 The Hugo Authors. All rights reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  // http://www.apache.org/licenses/LICENSE-2.0
     7  //
     8  // Unless required by applicable law or agreed to in writing, software
     9  // distributed under the License is distributed on an "AS IS" BASIS,
    10  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package hugio
    15  
    16  import (
    17  	"io"
    18  )
    19  
    20  type multiWriteCloser struct {
    21  	io.Writer
    22  	closers []io.WriteCloser
    23  }
    24  
    25  func (m multiWriteCloser) Close() error {
    26  	var err error
    27  	for _, c := range m.closers {
    28  		if closeErr := c.Close(); err != nil {
    29  			err = closeErr
    30  		}
    31  	}
    32  	return err
    33  }
    34  
    35  // NewMultiWriteCloser creates a new io.WriteCloser that duplicates its writes to all the
    36  // provided writers.
    37  func NewMultiWriteCloser(writeClosers ...io.WriteCloser) io.WriteCloser {
    38  	writers := make([]io.Writer, len(writeClosers))
    39  	for i, w := range writeClosers {
    40  		writers[i] = w
    41  	}
    42  	return multiWriteCloser{Writer: io.MultiWriter(writers...), closers: writeClosers}
    43  }