-
Notifications
You must be signed in to change notification settings - Fork 66
/
config.go
51 lines (42 loc) · 956 Bytes
/
config.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
package output
import (
"github.com/hokaccha/go-prettyjson"
"golang.org/x/term"
)
// New creates a new outputter with the specific config options
func New(opts ...ConfigOption) (*Output, error) {
config := &Output{
format: DefaultFormat,
prettyPrint: DefaultPretty,
terminalWidth: DefaultTerminalWidth,
}
// Set some defaults
w, _, err := term.GetSize(0)
if err == nil {
config.terminalWidth = w
}
// Loop through config options
for _, fn := range opts {
if nil != fn {
if err := fn(config); err != nil {
return nil, err
}
}
}
// JSON output
config.jsonFormatter = prettyjson.NewFormatter()
return config, nil
}
type ConfigOption func(*Output) error
func ConfigFormat(format Format) ConfigOption {
return func(cfg *Output) error {
cfg.format = format
return nil
}
}
func ConfigPrettyPrint(pretty bool) ConfigOption {
return func(cfg *Output) error {
cfg.prettyPrint = pretty
return nil
}
}