forked from sensu/sensu-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
input.go
47 lines (42 loc) · 897 Bytes
/
input.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
package helpers
import (
"errors"
"io"
"os"
)
// DetectEmptyStdin determines if stdin is empty
func DetectEmptyStdin(f *os.File) error {
fi, err := f.Stat()
if err != nil {
return err
}
if fi.Size() == 0 {
if fi.Mode()&os.ModeNamedPipe == 0 {
return errors.New("empty stdin")
}
}
return nil
}
// InputData returns the content of filename, if provided, or the standard
// input. An error is returned if no input data is provided or if the file could
// not be open
func InputData(filename string) (io.Reader, error) {
if filename == "" {
if err := DetectEmptyStdin(os.Stdin); err != nil {
return nil, err
}
return os.Stdin, nil
}
file, err := os.Open(filename)
if err != nil {
return nil, err
}
stat, err := file.Stat()
if err != nil {
return nil, err
}
if stat.IsDir() {
return nil, errors.New("directories not supported yet")
}
return file, nil
}