-
Notifications
You must be signed in to change notification settings - Fork 39.6k
/
output.go
242 lines (196 loc) · 8.21 KB
/
output.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*
Copyright 2019 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package output
import (
"fmt"
"io"
"strings"
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/cli-runtime/pkg/printers"
)
const (
// TextOutput describes the plain text output
TextOutput = "text"
// JSONOutput describes the JSON output
JSONOutput = "json"
// YAMLOutput describes the YAML output
YAMLOutput = "yaml"
)
// TextPrintFlags is an interface to handle custom text output
type TextPrintFlags interface {
ToPrinter(outputFormat string) (Printer, error)
}
// PrintFlags composes common printer flag structs
// used across kubeadm commands, and provides a method
// of retrieving a known printer based on flag values provided.
type PrintFlags struct {
// JSONYamlPrintFlags provides default flags necessary for json/yaml printing.
JSONYamlPrintFlags *genericclioptions.JSONYamlPrintFlags
// KubeTemplatePrintFlags composes print flags that provide both a JSONPath and a go-template printer.
KubeTemplatePrintFlags *genericclioptions.KubeTemplatePrintFlags
// TextPrintFlags provides default flags necessary for kubeadm text printing.
TextPrintFlags TextPrintFlags
// TypeSetterPrinter is an implementation of ResourcePrinter that wraps another printer with types set on the objects
TypeSetterPrinter *printers.TypeSetterPrinter
// OutputFormat contains currently set output format
OutputFormat *string
}
// AllowedFormats returns a list of allowed output formats
func (pf *PrintFlags) AllowedFormats() []string {
ret := []string{TextOutput}
ret = append(ret, pf.JSONYamlPrintFlags.AllowedFormats()...)
ret = append(ret, pf.KubeTemplatePrintFlags.AllowedFormats()...)
return ret
}
// ToPrinter receives an outputFormat and returns a printer capable of
// handling format printing.
// Returns error if the specified outputFormat does not match supported formats.
func (pf *PrintFlags) ToPrinter() (Printer, error) {
outputFormat := ""
if pf.OutputFormat != nil {
outputFormat = *pf.OutputFormat
}
if pf.TextPrintFlags != nil {
if p, err := pf.TextPrintFlags.ToPrinter(outputFormat); !genericclioptions.IsNoCompatiblePrinterError(err) {
return p, err
}
}
if pf.JSONYamlPrintFlags != nil {
if p, err := pf.JSONYamlPrintFlags.ToPrinter(outputFormat); !genericclioptions.IsNoCompatiblePrinterError(err) {
return NewResourcePrinterWrapper(pf.TypeSetterPrinter.WrapToPrinter(p, err))
}
}
if pf.KubeTemplatePrintFlags != nil {
if p, err := pf.KubeTemplatePrintFlags.ToPrinter(outputFormat); !genericclioptions.IsNoCompatiblePrinterError(err) {
return NewResourcePrinterWrapper(pf.TypeSetterPrinter.WrapToPrinter(p, err))
}
}
return nil, genericclioptions.NoCompatiblePrinterError{OutputFormat: pf.OutputFormat, AllowedFormats: pf.AllowedFormats()}
}
// AddFlags receives a *cobra.Command reference and binds
// flags related to Kubeadm printing to it
func (pf *PrintFlags) AddFlags(cmd *cobra.Command) {
pf.JSONYamlPrintFlags.AddFlags(cmd)
pf.KubeTemplatePrintFlags.AddFlags(cmd)
cmd.Flags().StringVarP(pf.OutputFormat, "experimental-output", "o", *pf.OutputFormat, fmt.Sprintf("Output format. One of: %s.", strings.Join(pf.AllowedFormats(), "|")))
}
// WithDefaultOutput sets a default output format if one is not provided through a flag value
func (pf *PrintFlags) WithDefaultOutput(outputFormat string) *PrintFlags {
pf.OutputFormat = &outputFormat
return pf
}
// WithTypeSetter sets a wrapper than will surround the returned printer with a printer to type resources
func (pf *PrintFlags) WithTypeSetter(scheme *runtime.Scheme) *PrintFlags {
pf.TypeSetterPrinter = printers.NewTypeSetter(scheme)
return pf
}
// NewOutputFlags creates new KubeadmOutputFlags
func NewOutputFlags(textPrintFlags TextPrintFlags) *PrintFlags {
outputFormat := ""
pf := &PrintFlags{
OutputFormat: &outputFormat,
JSONYamlPrintFlags: genericclioptions.NewJSONYamlPrintFlags(),
KubeTemplatePrintFlags: genericclioptions.NewKubeTemplatePrintFlags(),
TextPrintFlags: textPrintFlags,
}
// disable deprecated --template option
pf.KubeTemplatePrintFlags.TemplateArgument = nil
return pf
}
// Printer is a common printing interface in Kubeadm
type Printer interface {
PrintObj(obj runtime.Object, writer io.Writer) error
Fprintf(writer io.Writer, format string, args ...interface{}) (n int, err error)
Fprintln(writer io.Writer, args ...interface{}) (n int, err error)
Printf(format string, args ...interface{}) (n int, err error)
Println(args ...interface{}) (n int, err error)
Flush(writer io.Writer, last bool)
Close(writer io.Writer)
}
// TextPrinter implements Printer interface for generic text output
type TextPrinter struct {
}
// PrintObj is an implementation of ResourcePrinter.PrintObj that prints object
func (tp *TextPrinter) PrintObj(obj runtime.Object, writer io.Writer) error {
_, err := fmt.Fprintf(writer, "%+v\n", obj)
return err
}
// Fprintf is a wrapper around fmt.Fprintf
func (tp *TextPrinter) Fprintf(writer io.Writer, format string, args ...interface{}) (n int, err error) {
return fmt.Fprintf(writer, format, args...)
}
// Fprintln is a wrapper around fmt.Fprintln
func (tp *TextPrinter) Fprintln(writer io.Writer, args ...interface{}) (n int, err error) {
return fmt.Fprintln(writer, args...)
}
// Printf is a wrapper around fmt.Printf
func (tp *TextPrinter) Printf(format string, args ...interface{}) (n int, err error) {
return fmt.Printf(format, args...)
}
// Println is a wrapper around fmt.Printf
func (tp *TextPrinter) Println(args ...interface{}) (n int, err error) {
return fmt.Println(args...)
}
// Flush writes any buffered data
func (tp *TextPrinter) Flush(writer io.Writer, last bool) {
}
// Close flushes any buffered data and closes the printer
func (tp *TextPrinter) Close(writer io.Writer) {
}
// ResourcePrinterWrapper wraps ResourcePrinter and implements Printer interface
type ResourcePrinterWrapper struct {
Printer printers.ResourcePrinter
}
// NewResourcePrinterWrapper creates new ResourcePrinter object
func NewResourcePrinterWrapper(resourcePrinter printers.ResourcePrinter, err error) (Printer, error) {
if err != nil {
return nil, err
}
return &ResourcePrinterWrapper{Printer: resourcePrinter}, nil
}
// Flush writes any buffered data
func (rpw *ResourcePrinterWrapper) Flush(writer io.Writer, last bool) {
}
// Close flushes any buffered data and closes the printer
func (rpw *ResourcePrinterWrapper) Close(writer io.Writer) {
}
// PrintObj is an implementation of ResourcePrinter.PrintObj that calls underlying printer API
func (rpw *ResourcePrinterWrapper) PrintObj(obj runtime.Object, writer io.Writer) error {
return rpw.Printer.PrintObj(obj, writer)
}
// Fprintf is an empty method to satisfy Printer interface
// and silent info printing for structured output
// This method is usually redefined for the text output
func (rpw *ResourcePrinterWrapper) Fprintf(writer io.Writer, format string, args ...interface{}) (n int, err error) {
return 0, nil
}
// Fprintln is an empty method to satisfy the Printer interface
// and silent info printing for structured output
// This method is usually redefined for the text output
func (rpw *ResourcePrinterWrapper) Fprintln(writer io.Writer, args ...interface{}) (n int, err error) {
return 0, nil
}
// Printf is an empty method to satisfy Printer interface
// and silent info printing for structured output
// This method is usually redefined for the text output
func (rpw *ResourcePrinterWrapper) Printf(format string, args ...interface{}) (n int, err error) {
return 0, nil
}
// Println is an empty method to satisfy Printer interface
// and silent info printing for structured output
// This method is usually redefined for the text output
func (rpw *ResourcePrinterWrapper) Println(args ...interface{}) (n int, err error) {
return 0, nil
}