forked from tsenart/vegeta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.go
62 lines (52 loc) · 1.1 KB
/
file.go
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package main
import (
"errors"
"fmt"
"io"
"os"
"strings"
vegeta "github.com/ernestrc/vegeta/lib"
)
func file(name string, create bool) (*os.File, error) {
switch name {
case "stdin":
return os.Stdin, nil
case "stdout":
return os.Stdout, nil
default:
if create {
return os.Create(name)
}
return os.Open(name)
}
}
func decoder(files []string) (vegeta.Decoder, io.Closer, error) {
closer := make(multiCloser, 0, len(files))
decs := make([]vegeta.Decoder, 0, len(files))
for _, f := range files {
rc, err := file(f, false)
if err != nil {
return nil, closer, err
}
dec := vegeta.DecoderFor(rc)
if dec == nil {
return nil, closer, fmt.Errorf("encode: can't detect encoding of %q", f)
}
decs = append(decs, dec)
closer = append(closer, rc)
}
return vegeta.NewRoundRobinDecoder(decs...), closer, nil
}
type multiCloser []io.Closer
func (mc multiCloser) Close() error {
var errs []string
for _, c := range mc {
if err := c.Close(); err != nil {
errs = append(errs, err.Error())
}
}
if len(errs) > 0 {
return errors.New(strings.Join(errs, "; "))
}
return nil
}