-
Notifications
You must be signed in to change notification settings - Fork 0
/
io.go
41 lines (33 loc) · 878 Bytes
/
io.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
package cobrax
import (
"errors"
"io"
"os"
"github.com/spf13/afero"
"golang.org/x/term"
)
var ErrNoFileSpecified = errors.New("no file specified")
type Option struct {
stdin io.Reader
enableManualInput bool
}
type OptionFunc func(*Option)
func WithStdin(stdin io.Reader) OptionFunc { return func(o *Option) { o.stdin = stdin } }
func WithManualInputEnabled(o *Option) { o.enableManualInput = true }
func OpenOrStdIn(filename string, fs afero.Fs, opts ...OptionFunc) (io.ReadCloser, error) {
o := &Option{stdin: os.Stdin}
for _, f := range opts {
f(o)
}
if filename != "" {
f, err := fs.Open(filename)
if err != nil {
return nil, err
}
return f, nil
} else if f, ok := o.stdin.(*os.File); ok && !o.enableManualInput && term.IsTerminal(int(f.Fd())) {
return nil, ErrNoFileSpecified
} else {
return io.NopCloser(o.stdin), nil
}
}