1
16
17 package output
18
19 import (
20 "fmt"
21 "io"
22 "strings"
23
24 "github.com/spf13/cobra"
25
26 "k8s.io/apimachinery/pkg/runtime"
27 "k8s.io/cli-runtime/pkg/genericclioptions"
28 "k8s.io/cli-runtime/pkg/printers"
29 )
30
31 const (
32
33 TextOutput = "text"
34
35
36 JSONOutput = "json"
37
38
39 YAMLOutput = "yaml"
40 )
41
42
43 type TextPrintFlags interface {
44 ToPrinter(outputFormat string) (Printer, error)
45 }
46
47
48
49
50 type PrintFlags struct {
51
52 JSONYamlPrintFlags *genericclioptions.JSONYamlPrintFlags
53
54 KubeTemplatePrintFlags *genericclioptions.KubeTemplatePrintFlags
55
56 TextPrintFlags TextPrintFlags
57
58 TypeSetterPrinter *printers.TypeSetterPrinter
59
60 OutputFormat *string
61 }
62
63
64 func (pf *PrintFlags) AllowedFormats() []string {
65 ret := []string{TextOutput}
66 ret = append(ret, pf.JSONYamlPrintFlags.AllowedFormats()...)
67 ret = append(ret, pf.KubeTemplatePrintFlags.AllowedFormats()...)
68
69 return ret
70 }
71
72
73
74
75 func (pf *PrintFlags) ToPrinter() (Printer, error) {
76 outputFormat := ""
77 if pf.OutputFormat != nil {
78 outputFormat = *pf.OutputFormat
79 }
80
81 if pf.TextPrintFlags != nil {
82 if p, err := pf.TextPrintFlags.ToPrinter(outputFormat); !genericclioptions.IsNoCompatiblePrinterError(err) {
83 return p, err
84 }
85 }
86
87 if pf.JSONYamlPrintFlags != nil {
88 if p, err := pf.JSONYamlPrintFlags.ToPrinter(outputFormat); !genericclioptions.IsNoCompatiblePrinterError(err) {
89 return NewResourcePrinterWrapper(pf.TypeSetterPrinter.WrapToPrinter(p, err))
90 }
91 }
92
93 if pf.KubeTemplatePrintFlags != nil {
94 if p, err := pf.KubeTemplatePrintFlags.ToPrinter(outputFormat); !genericclioptions.IsNoCompatiblePrinterError(err) {
95 return NewResourcePrinterWrapper(pf.TypeSetterPrinter.WrapToPrinter(p, err))
96 }
97 }
98
99 return nil, genericclioptions.NoCompatiblePrinterError{OutputFormat: pf.OutputFormat, AllowedFormats: pf.AllowedFormats()}
100 }
101
102
103
104 func (pf *PrintFlags) AddFlags(cmd *cobra.Command) {
105 pf.JSONYamlPrintFlags.AddFlags(cmd)
106 pf.KubeTemplatePrintFlags.AddFlags(cmd)
107 cmd.Flags().StringVarP(pf.OutputFormat, "experimental-output", "o", *pf.OutputFormat, fmt.Sprintf("Output format. One of: %s.", strings.Join(pf.AllowedFormats(), "|")))
108 }
109
110
111 func (pf *PrintFlags) WithDefaultOutput(outputFormat string) *PrintFlags {
112 pf.OutputFormat = &outputFormat
113 return pf
114 }
115
116
117 func (pf *PrintFlags) WithTypeSetter(scheme *runtime.Scheme) *PrintFlags {
118 pf.TypeSetterPrinter = printers.NewTypeSetter(scheme)
119 return pf
120 }
121
122
123 func NewOutputFlags(textPrintFlags TextPrintFlags) *PrintFlags {
124 outputFormat := ""
125
126 pf := &PrintFlags{
127 OutputFormat: &outputFormat,
128
129 JSONYamlPrintFlags: genericclioptions.NewJSONYamlPrintFlags(),
130 KubeTemplatePrintFlags: genericclioptions.NewKubeTemplatePrintFlags(),
131 TextPrintFlags: textPrintFlags,
132 }
133
134
135 pf.KubeTemplatePrintFlags.TemplateArgument = nil
136
137 return pf
138 }
139
140
141 type Printer interface {
142 PrintObj(obj runtime.Object, writer io.Writer) error
143 Fprintf(writer io.Writer, format string, args ...interface{}) (n int, err error)
144 Fprintln(writer io.Writer, args ...interface{}) (n int, err error)
145 Printf(format string, args ...interface{}) (n int, err error)
146 Println(args ...interface{}) (n int, err error)
147 }
148
149
150 type TextPrinter struct {
151 }
152
153
154 func (tp *TextPrinter) PrintObj(obj runtime.Object, writer io.Writer) error {
155 _, err := fmt.Fprintf(writer, "%+v\n", obj)
156 return err
157 }
158
159
160 func (tp *TextPrinter) Fprintf(writer io.Writer, format string, args ...interface{}) (n int, err error) {
161 return fmt.Fprintf(writer, format, args...)
162 }
163
164
165 func (tp *TextPrinter) Fprintln(writer io.Writer, args ...interface{}) (n int, err error) {
166 return fmt.Fprintln(writer, args...)
167 }
168
169
170 func (tp *TextPrinter) Printf(format string, args ...interface{}) (n int, err error) {
171 return fmt.Printf(format, args...)
172 }
173
174
175 func (tp *TextPrinter) Println(args ...interface{}) (n int, err error) {
176 return fmt.Println(args...)
177 }
178
179
180 type ResourcePrinterWrapper struct {
181 Printer printers.ResourcePrinter
182 }
183
184
185 func NewResourcePrinterWrapper(resourcePrinter printers.ResourcePrinter, err error) (Printer, error) {
186 if err != nil {
187 return nil, err
188 }
189 return &ResourcePrinterWrapper{Printer: resourcePrinter}, nil
190 }
191
192
193 func (rpw *ResourcePrinterWrapper) PrintObj(obj runtime.Object, writer io.Writer) error {
194 return rpw.Printer.PrintObj(obj, writer)
195 }
196
197
198
199
200 func (rpw *ResourcePrinterWrapper) Fprintf(writer io.Writer, format string, args ...interface{}) (n int, err error) {
201 return 0, nil
202 }
203
204
205
206
207 func (rpw *ResourcePrinterWrapper) Fprintln(writer io.Writer, args ...interface{}) (n int, err error) {
208 return 0, nil
209 }
210
211
212
213
214 func (rpw *ResourcePrinterWrapper) Printf(format string, args ...interface{}) (n int, err error) {
215 return 0, nil
216 }
217
218
219
220
221 func (rpw *ResourcePrinterWrapper) Println(args ...interface{}) (n int, err error) {
222 return 0, nil
223 }
224
View as plain text