Skip to content

Commit a74b28d

Browse files
committed
rm GetStandardPrinter
1 parent 5a34e4f commit a74b28d

File tree

9 files changed

+23
-237
lines changed

9 files changed

+23
-237
lines changed

pkg/kubectl/cmd/config/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func NewCmdConfig(f cmdutil.Factory, pathOptions *clientcmd.PathOptions, streams
4848
1. If the --` + pathOptions.ExplicitFileFlag + ` flag is set, then only that file is loaded. The flag may only be set once and no merging takes place.
4949
2. If $` + pathOptions.EnvVar + ` environment variable is set, then it is used a list of paths (normal path delimitting rules for your system). These paths are merged. When a value is modified, it is modified in the file that defines the stanza. When a value is created, it is created in the first file that exists. If no files in the chain exist, then it creates the last file in the list.
5050
3. Otherwise, ` + path.Join("${HOME}", pathOptions.GlobalFileSubpath) + ` is used and no merging takes place.`),
51-
Run: cmdutil.DefaultSubCommandRun(streams.Out),
51+
Run: cmdutil.DefaultSubCommandRun(streams.ErrOut),
5252
}
5353

5454
// file paths are common to all sub commands

pkg/kubectl/cmd/config/flags.go

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,22 @@ import (
2222
"k8s.io/kubernetes/pkg/printers"
2323
)
2424

25-
// PrintFlags composes common printer flag structs
25+
// kubectlConfigPrintFlags composes common printer flag structs
2626
// used across all config commands, and provides a method
2727
// of retrieving a known printer based on flag values provided.
28-
type PrintFlags struct {
28+
type kubectlConfigPrintFlags struct {
2929
JSONYamlPrintFlags *printers.JSONYamlPrintFlags
3030
NamePrintFlags *printers.NamePrintFlags
3131
TemplateFlags *printers.KubeTemplatePrintFlags
3232

3333
OutputFormat *string
3434
}
3535

36-
func (f *PrintFlags) Complete(successTemplate string) error {
36+
func (f *kubectlConfigPrintFlags) Complete(successTemplate string) error {
3737
return f.NamePrintFlags.Complete(successTemplate)
3838
}
3939

40-
func (f *PrintFlags) ToPrinter() (printers.ResourcePrinter, error) {
40+
func (f *kubectlConfigPrintFlags) ToPrinter() (printers.ResourcePrinter, error) {
4141
outputFormat := ""
4242
if f.OutputFormat != nil {
4343
outputFormat = *f.OutputFormat
@@ -58,34 +58,26 @@ func (f *PrintFlags) ToPrinter() (printers.ResourcePrinter, error) {
5858
return nil, printers.NoCompatiblePrinterError{Options: f}
5959
}
6060

61-
func (f *PrintFlags) AddFlags(cmd *cobra.Command) {
61+
func (f *kubectlConfigPrintFlags) AddFlags(cmd *cobra.Command) {
6262
f.JSONYamlPrintFlags.AddFlags(cmd)
6363
f.NamePrintFlags.AddFlags(cmd)
6464
f.TemplateFlags.AddFlags(cmd)
6565

6666
if f.OutputFormat != nil {
67-
cmd.Flags().StringVarP(f.OutputFormat, "output", "o", *f.OutputFormat, "Output format. One of: json|yaml|wide|name|custom-columns=...|custom-columns-file=...|go-template=...|go-template-file=...|jsonpath=...|jsonpath-file=... See custom columns [http://kubernetes.io/docs/user-guide/kubectl-overview/#custom-columns], golang template [http://golang.org/pkg/text/template/#pkg-overview] and jsonpath template [http://kubernetes.io/docs/user-guide/jsonpath].")
67+
cmd.Flags().StringVarP(f.OutputFormat, "output", "o", *f.OutputFormat, "Output format. One of: json|yaml|name|go-template=...|go-template-file=...|jsonpath=...|jsonpath-file=... See custom columns [http://kubernetes.io/docs/user-guide/kubectl-overview/#custom-columns], golang template [http://golang.org/pkg/text/template/#pkg-overview] and jsonpath template [http://kubernetes.io/docs/user-guide/jsonpath].")
6868
}
6969
}
7070

7171
// WithDefaultOutput sets a default output format if one is not provided through a flag value
72-
func (f *PrintFlags) WithDefaultOutput(output string) *PrintFlags {
73-
existingFormat := ""
74-
if f.OutputFormat != nil {
75-
existingFormat = *f.OutputFormat
76-
}
77-
if len(existingFormat) == 0 {
78-
existingFormat = output
79-
}
80-
f.OutputFormat = &existingFormat
81-
72+
func (f *kubectlConfigPrintFlags) WithDefaultOutput(output string) *kubectlConfigPrintFlags {
73+
f.OutputFormat = &output
8274
return f
8375
}
8476

85-
func NewPrintFlags(operation string) *PrintFlags {
77+
func newKubeConfigPrintFlags(operation string) *kubectlConfigPrintFlags {
8678
outputFormat := ""
8779

88-
return &PrintFlags{
80+
return &kubectlConfigPrintFlags{
8981
OutputFormat: &outputFormat,
9082

9183
JSONYamlPrintFlags: printers.NewJSONYamlPrintFlags(),

pkg/kubectl/cmd/config/view.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import (
3333
)
3434

3535
type ViewOptions struct {
36-
PrintFlags *PrintFlags
36+
PrintFlags *kubectlConfigPrintFlags
3737
PrintObject printers.ResourcePrinterFunc
3838

3939
ConfigAccess clientcmd.ConfigAccess
@@ -68,7 +68,7 @@ var (
6868

6969
func NewCmdConfigView(f cmdutil.Factory, streams genericclioptions.IOStreams, ConfigAccess clientcmd.ConfigAccess) *cobra.Command {
7070
o := &ViewOptions{
71-
PrintFlags: NewPrintFlags("").WithDefaultOutput("yaml"),
71+
PrintFlags: newKubeConfigPrintFlags("").WithDefaultOutput("yaml"),
7272
ConfigAccess: ConfigAccess,
7373

7474
IOStreams: streams,

pkg/kubectl/cmd/delete.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,9 +183,8 @@ func (o *DeleteOptions) Complete(f cmdutil.Factory, out, errOut io.Writer, args
183183
}
184184

185185
func (o *DeleteOptions) Validate(cmd *cobra.Command) error {
186-
outputMode := cmdutil.GetFlagString(cmd, "output")
187-
if outputMode != "" && outputMode != "name" {
188-
return cmdutil.UsageErrorf(cmd, "Unexpected -o output mode: %v. We only support '-o name'.", outputMode)
186+
if o.Output != "" && o.Output != "name" {
187+
return cmdutil.UsageErrorf(cmd, "Unexpected -o output mode: %v. We only support '-o name'.", o.Output)
189188
}
190189

191190
if o.DeleteAll && len(o.LabelSelector) > 0 {

pkg/kubectl/cmd/util/printing.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ import (
2020
"fmt"
2121

2222
"k8s.io/kubernetes/pkg/kubectl/cmd/templates"
23-
"k8s.io/kubernetes/pkg/printers"
24-
printersinternal "k8s.io/kubernetes/pkg/printers/internalversion"
2523
)
2624

2725
// SuggestApiResources returns a suggestion to use the "api-resources" command

pkg/printers/BUILD

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ go_library(
2121
"kube_template_flags.go",
2222
"name.go",
2323
"name_flags.go",
24-
"printers.go",
2524
"tabwriter.go",
2625
"template.go",
2726
"template_flags.go",

pkg/printers/flags.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -91,15 +91,7 @@ func (f *PrintFlags) AddFlags(cmd *cobra.Command) {
9191

9292
// WithDefaultOutput sets a default output format if one is not provided through a flag value
9393
func (f *PrintFlags) WithDefaultOutput(output string) *PrintFlags {
94-
existingFormat := ""
95-
if f.OutputFormat != nil {
96-
existingFormat = *f.OutputFormat
97-
}
98-
if len(existingFormat) == 0 {
99-
existingFormat = output
100-
}
101-
f.OutputFormat = &existingFormat
102-
94+
f.OutputFormat = &output
10395
return f
10496
}
10597

pkg/printers/internalversion/printers_test.go

Lines changed: 7 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -229,78 +229,6 @@ func (obj *TestUnknownType) DeepCopyObject() runtime.Object {
229229
return &clone
230230
}
231231

232-
func TestPrinter(t *testing.T) {
233-
//test inputs
234-
simpleTest := &TestPrintType{"foo"}
235-
podTest := &api.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo"}}
236-
podListTest := &api.PodList{
237-
Items: []api.Pod{
238-
{ObjectMeta: metav1.ObjectMeta{Name: "foo"}},
239-
{ObjectMeta: metav1.ObjectMeta{Name: "bar"}},
240-
},
241-
}
242-
emptyListTest := &api.PodList{}
243-
testapi, err := legacyscheme.Scheme.ConvertToVersion(podTest, schema.GroupVersion{Group: "", Version: "v1"})
244-
if err != nil {
245-
t.Fatalf("unexpected error: %v", err)
246-
}
247-
248-
printerTests := []struct {
249-
Name string
250-
PrintOpts *printers.PrintOptions
251-
Input runtime.Object
252-
OutputVersions []schema.GroupVersion
253-
Expect string
254-
}{
255-
{"test json", &printers.PrintOptions{OutputFormatType: "json", AllowMissingKeys: true}, simpleTest, nil, "{\n \"Data\": \"foo\"\n}\n"},
256-
{"test yaml", &printers.PrintOptions{OutputFormatType: "yaml", AllowMissingKeys: true}, simpleTest, nil, "Data: foo\n"},
257-
{"test template", &printers.PrintOptions{OutputFormatType: "template", OutputFormatArgument: "{{if .id}}{{.id}}{{end}}{{if .metadata.name}}{{.metadata.name}}{{end}}", AllowMissingKeys: true},
258-
podTest, []schema.GroupVersion{v1.SchemeGroupVersion}, "foo"},
259-
{"test jsonpath", &printers.PrintOptions{OutputFormatType: "jsonpath", OutputFormatArgument: "{.metadata.name}", AllowMissingKeys: true}, podTest, []schema.GroupVersion{v1.SchemeGroupVersion}, "foo"},
260-
{"test jsonpath list", &printers.PrintOptions{OutputFormatType: "jsonpath", OutputFormatArgument: "{.items[*].metadata.name}", AllowMissingKeys: true}, podListTest, []schema.GroupVersion{v1.SchemeGroupVersion}, "foo bar"},
261-
{"test jsonpath empty list", &printers.PrintOptions{OutputFormatType: "jsonpath", OutputFormatArgument: "{.items[*].metadata.name}", AllowMissingKeys: true}, emptyListTest, []schema.GroupVersion{v1.SchemeGroupVersion}, ""},
262-
{"test name", &printers.PrintOptions{OutputFormatType: "name", AllowMissingKeys: true}, podTest, []schema.GroupVersion{v1.SchemeGroupVersion}, "pod/foo\n"},
263-
{"emits versioned objects", &printers.PrintOptions{OutputFormatType: "template", OutputFormatArgument: "{{.kind}}", AllowMissingKeys: true}, testapi, []schema.GroupVersion{v1.SchemeGroupVersion}, "Pod"},
264-
}
265-
for _, test := range printerTests {
266-
buf := bytes.NewBuffer([]byte{})
267-
printer, err := printers.GetStandardPrinter(legacyscheme.Scheme, legacyscheme.Codecs.LegacyCodec(legacyscheme.Registry.RegisteredGroupVersions()...), []runtime.Decoder{legacyscheme.Codecs.UniversalDecoder(), unstructured.UnstructuredJSONScheme}, *test.PrintOpts)
268-
if err != nil {
269-
t.Errorf("in %s, unexpected error: %#v", test.Name, err)
270-
}
271-
if len(test.OutputVersions) > 0 {
272-
printer = printers.NewVersionedPrinter(printer, legacyscheme.Scheme, legacyscheme.Scheme, test.OutputVersions...)
273-
}
274-
if err := printer.PrintObj(test.Input, buf); err != nil {
275-
t.Errorf("in %s, unexpected error: %#v", test.Name, err)
276-
}
277-
if buf.String() != test.Expect {
278-
t.Errorf("in %s, expect %q, got %q", test.Name, test.Expect, buf.String())
279-
}
280-
}
281-
282-
}
283-
284-
func TestBadPrinter(t *testing.T) {
285-
badPrinterTests := []struct {
286-
Name string
287-
PrintOpts *printers.PrintOptions
288-
Error error
289-
}{
290-
{"empty template", &printers.PrintOptions{OutputFormatType: "template", AllowMissingKeys: false}, fmt.Errorf("template format specified but no template given")},
291-
{"bad template", &printers.PrintOptions{OutputFormatType: "template", OutputFormatArgument: "{{ .Name", AllowMissingKeys: false}, fmt.Errorf("error parsing template {{ .Name, template: output:1: unclosed action\n")},
292-
{"bad templatefile", &printers.PrintOptions{OutputFormatType: "templatefile", AllowMissingKeys: false}, fmt.Errorf("template format specified but no template given")},
293-
{"bad jsonpath", &printers.PrintOptions{OutputFormatType: "jsonpath", OutputFormatArgument: "{.Name", AllowMissingKeys: false}, fmt.Errorf("error parsing jsonpath {.Name, unclosed action\n")},
294-
{"unknown format", &printers.PrintOptions{OutputFormatType: "anUnknownFormat", OutputFormatArgument: "", AllowMissingKeys: false}, fmt.Errorf("output format \"anUnknownFormat\" not recognized")},
295-
}
296-
for _, test := range badPrinterTests {
297-
_, err := printers.GetStandardPrinter(legacyscheme.Scheme, legacyscheme.Codecs.LegacyCodec(legacyscheme.Registry.RegisteredGroupVersions()...), []runtime.Decoder{legacyscheme.Codecs.UniversalDecoder(), unstructured.UnstructuredJSONScheme}, *test.PrintOpts)
298-
if err == nil || err.Error() != test.Error.Error() {
299-
t.Errorf("in %s, expect %s, got %s", test.Name, test.Error, err)
300-
}
301-
}
302-
}
303-
304232
func testPrinter(t *testing.T, printer printers.ResourcePrinter, unmarshalFunc func(data []byte, v interface{}) error) {
305233
buf := bytes.NewBuffer([]byte{})
306234

@@ -471,8 +399,13 @@ func TestNamePrinter(t *testing.T) {
471399
},
472400
"pod/foo\npod/bar\n"},
473401
}
474-
printOpts := &printers.PrintOptions{OutputFormatType: "name", AllowMissingKeys: false}
475-
printer, _ := printers.GetStandardPrinter(legacyscheme.Scheme, legacyscheme.Codecs.LegacyCodec(legacyscheme.Registry.RegisteredGroupVersions()...), []runtime.Decoder{legacyscheme.Codecs.UniversalDecoder(), unstructured.UnstructuredJSONScheme}, *printOpts)
402+
403+
printFlags := printers.NewPrintFlags("").WithDefaultOutput("name")
404+
printer, err := printFlags.ToPrinter()
405+
if err != nil {
406+
t.Fatalf("unexpected err: %v", err)
407+
}
408+
476409
for name, item := range tests {
477410
buff := &bytes.Buffer{}
478411
err := printer.PrintObj(item.obj, buff)
@@ -2987,44 +2920,6 @@ func TestPrintPodDisruptionBudget(t *testing.T) {
29872920
}
29882921
}
29892922

2990-
func TestAllowMissingKeys(t *testing.T) {
2991-
tests := []struct {
2992-
Name string
2993-
PrintOpts *printers.PrintOptions
2994-
Input runtime.Object
2995-
Expect string
2996-
Error string
2997-
}{
2998-
{"test template, allow missing keys", &printers.PrintOptions{OutputFormatType: "template", OutputFormatArgument: "{{.blarg}}", AllowMissingKeys: true}, &api.Pod{}, "<no value>", ""},
2999-
{"test template, strict", &printers.PrintOptions{OutputFormatType: "template", OutputFormatArgument: "{{.blarg}}", AllowMissingKeys: false}, &api.Pod{}, "", `error executing template "{{.blarg}}": template: output:1:2: executing "output" at <.blarg>: map has no entry for key "blarg"`},
3000-
{"test jsonpath, allow missing keys", &printers.PrintOptions{OutputFormatType: "jsonpath", OutputFormatArgument: "{.blarg}", AllowMissingKeys: true}, &api.Pod{}, "", ""},
3001-
{"test jsonpath, strict", &printers.PrintOptions{OutputFormatType: "jsonpath", OutputFormatArgument: "{.blarg}", AllowMissingKeys: false}, &api.Pod{}, "", "error executing jsonpath \"{.blarg}\": blarg is not found\n"},
3002-
}
3003-
for _, test := range tests {
3004-
buf := bytes.NewBuffer([]byte{})
3005-
printer, err := printers.GetStandardPrinter(legacyscheme.Scheme, legacyscheme.Codecs.LegacyCodec(legacyscheme.Registry.RegisteredGroupVersions()...), []runtime.Decoder{legacyscheme.Codecs.UniversalDecoder(), unstructured.UnstructuredJSONScheme}, *test.PrintOpts)
3006-
if err != nil {
3007-
t.Errorf("in %s, unexpected error: %#v", test.Name, err)
3008-
}
3009-
err = printer.PrintObj(test.Input, buf)
3010-
if len(test.Error) == 0 && err != nil {
3011-
t.Errorf("in %s, unexpected error: %v", test.Name, err)
3012-
continue
3013-
}
3014-
if len(test.Error) > 0 {
3015-
if err == nil {
3016-
t.Errorf("in %s, expected to get error: %v", test.Name, test.Error)
3017-
} else if e, a := test.Error, err.Error(); e != a {
3018-
t.Errorf("in %s, expected error %q, got %q", test.Name, e, a)
3019-
}
3020-
continue
3021-
}
3022-
if buf.String() != test.Expect {
3023-
t.Errorf("in %s, expect %q, got %q", test.Name, test.Expect, buf.String())
3024-
}
3025-
}
3026-
}
3027-
30282923
func TestPrintControllerRevision(t *testing.T) {
30292924
tests := []struct {
30302925
history apps.ControllerRevision

pkg/printers/printers.go

Lines changed: 0 additions & 89 deletions
This file was deleted.

0 commit comments

Comments
 (0)