-
Notifications
You must be signed in to change notification settings - Fork 3
/
printers.go
82 lines (68 loc) · 1.65 KB
/
printers.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
package printers
import (
"io"
"github.com/gotidy/ptr"
"github.com/redhat-marketplace/datactl/pkg/printers/output"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/kubectl/pkg/cmd/get"
)
type PrintObj interface {
Print(obj runtime.Object) error
}
type HumanPrinterCallback func(*output.HumanOutput) *output.HumanOutput
type TableOutputCallback func(PrintObj)
type TablePrinter interface {
TableOutput(TableOutputCallback)
}
type HumanPrinter interface {
HumanOutput(HumanPrinterCallback)
}
type Printer interface {
TablePrinter
HumanPrinter
}
type printWrapper struct {
isHumanOutput bool
h *output.HumanOutput
t *output.TableOrStructPrinter
}
// HumanOutput provides a function with a printer. The printer can be saved for
// later calls by returning it. Or returning nil to reset it.
func (p *printWrapper) HumanOutput(print HumanPrinterCallback) {
if p.isHumanOutput {
h := print(p.h)
if h != nil {
p.h = h
} else {
p.h = output.NewHumanOutput()
}
}
}
func (p *printWrapper) TableOutput(print TableOutputCallback) {
print(p.t)
p.t.Flush()
}
func NewPrinter(
out io.Writer,
printFlags *get.PrintFlags,
) (Printer, error) {
p := output.NewHumanOutput()
print, err := printFlags.ToPrinter()
if err != nil {
return nil, err
}
humanOutput := false
if printFlags.OutputFormat == nil ||
*printFlags.OutputFormat == "wide" ||
*printFlags.OutputFormat == "" {
humanOutput = true
printFlags.OutputFormat = ptr.String("wide")
} else {
output.DisableColor()
}
return &printWrapper{
isHumanOutput: humanOutput,
h: p,
t: output.NewActionCLITableOrStruct(out, printFlags, print),
}, nil
}