-
Notifications
You must be signed in to change notification settings - Fork 111
/
printer.go
163 lines (138 loc) · 3.34 KB
/
printer.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package printer
import (
"encoding/json"
"fmt"
"io"
"os"
"strings"
"github.com/fatih/color"
"github.com/gocarina/gocsv"
"github.com/lensesio/tableprinter"
)
type Format int
const (
FormatUnspecified Format = iota
FormatHuman
FormatJSON
FormatCSV
)
func (f Format) String() string {
switch f {
case FormatHuman:
return "human"
case FormatJSON:
return "json"
case FormatCSV:
return "csv"
}
return "unknown format"
}
func (f *Format) Type() string {
return "string"
}
func (f *Format) Set(s string) error {
var v Format
switch s {
case "human":
v = FormatHuman
case "json":
v = FormatJSON
case "csv":
v = FormatCSV
default:
return fmt.Errorf("failed to parse Format: %q. Valid values: %+v", s, []string{"human", "json", "csv"})
}
*f = v
return nil
}
var (
ColorBold = color.New(color.Bold)
ColorGreenBold = color.New(color.FgGreen).Add(color.Bold)
ColorYellowBold = color.New(color.FgYellow).Add(color.Bold)
ColorRedBold = color.New(color.FgRed).Add(color.Bold)
)
// Printer is a helper for printing output in a specific format.
// It differentiates between human-readable and machine-readable output.
// Regular log messages are always produced as human-readable output.
// Human-readable output is discarded when the format is not FormatHuman.
type Printer struct {
Format Format
humanOutOverride io.Writer
dataOutOverride io.Writer
}
func NewPrinter(format Format) *Printer {
return &Printer{
Format: format,
}
}
func (p *Printer) PrintData(v interface{}) {
out := p.dataOut()
switch p.Format {
case FormatHuman:
var b strings.Builder
tableprinter.Print(&b, v)
fmt.Fprint(out, b.String())
case FormatJSON:
buf, err := json.MarshalIndent(v, "", " ")
if err != nil {
panic(fmt.Errorf("failed to marshal JSON: %w", err))
}
fmt.Fprintln(out, string(buf))
case FormatCSV:
buf, err := gocsv.MarshalString(v)
if err != nil {
panic(fmt.Errorf("failed to marshal CSV: %w", err))
}
fmt.Fprint(out, buf)
default:
panic(fmt.Errorf("unexpected print format <%v>", p.Format))
}
}
func (p *Printer) PrintDataWithTitle(v interface{}, title string) {
if p.Format == FormatHuman {
p.Printf(" %s\n", strings.ToUpper(title))
}
p.PrintData(v)
}
func (p *Printer) Print(i ...interface{}) {
fmt.Fprint(p.humanOut(), i...)
}
func (p *Printer) Println(i ...interface{}) {
fmt.Fprintln(p.humanOut(), i...)
}
func (p *Printer) Printf(format string, i ...interface{}) {
fmt.Fprintf(p.humanOut(), format, i...)
}
func (p *Printer) PrintfBold(str string, i ...interface{}) {
p.Print(ColorBold.Sprintf(str, i...))
}
func (p *Printer) PrintfSuccess(str string, i ...interface{}) {
p.Print(ColorGreenBold.Sprintf(str, i...))
}
func (p *Printer) PrintfWarn(str string, i ...interface{}) {
p.Print(ColorYellowBold.Sprintf(str, i...))
}
func (p *Printer) PrintfError(str string, i ...interface{}) {
p.Print(ColorRedBold.Sprintf(str, i...))
}
func (p *Printer) OverrideHumanOutput(out io.Writer) {
p.humanOutOverride = out
}
func (p *Printer) OverrideDataOutput(out io.Writer) {
p.dataOutOverride = out
}
func (p *Printer) humanOut() io.Writer {
if p.humanOutOverride != nil {
return p.humanOutOverride
}
if p.Format == FormatHuman {
return color.Output
}
return io.Discard
}
func (p *Printer) dataOut() io.Writer {
if p.dataOutOverride != nil {
return p.dataOutOverride
}
return os.Stdout
}