File: multiwriter.go

package info (click to toggle)
golang-github-insomniacslk-dhcp 0.0~git20200621.d74cd86-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,132 kB
  • sloc: makefile: 9
file content (37 lines) | stat: -rw-r--r-- 805 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// Copyright 2019 the u-root Authors. All rights reserved
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package uio

import (
	"io"
)

type multiCloser struct {
	io.Writer
	writers []io.Writer
}

// Close implements io.Closer and closes any io.Writers that are also
// io.Closers.
func (mc *multiCloser) Close() error {
	var allErr error
	for _, w := range mc.writers {
		if c, ok := w.(io.Closer); ok {
			if err := c.Close(); err != nil {
				allErr = err
			}
		}
	}
	return allErr
}

// MultiWriteCloser is an io.MultiWriter that has an io.Closer and attempts to
// close those w's that have optional io.Closers.
func MultiWriteCloser(w ...io.Writer) io.WriteCloser {
	return &multiCloser{
		Writer:  io.MultiWriter(w...),
		writers: w,
	}
}