...
1
16
17 package genericclioptions
18
19 import (
20 "strings"
21
22 "github.com/spf13/cobra"
23
24 "k8s.io/cli-runtime/pkg/printers"
25 )
26
27
28 func (f *JSONYamlPrintFlags) AllowedFormats() []string {
29 if f == nil {
30 return []string{}
31 }
32 return []string{"json", "yaml"}
33 }
34
35
36
37
38 type JSONYamlPrintFlags struct {
39 ShowManagedFields bool
40 }
41
42
43
44
45
46 func (f *JSONYamlPrintFlags) ToPrinter(outputFormat string) (printers.ResourcePrinter, error) {
47 var printer printers.ResourcePrinter
48
49 outputFormat = strings.ToLower(outputFormat)
50 switch outputFormat {
51 case "json":
52 printer = &printers.JSONPrinter{}
53 case "yaml":
54 printer = &printers.YAMLPrinter{}
55 default:
56 return nil, NoCompatiblePrinterError{OutputFormat: &outputFormat, AllowedFormats: f.AllowedFormats()}
57 }
58
59 if !f.ShowManagedFields {
60 printer = &printers.OmitManagedFieldsPrinter{Delegate: printer}
61 }
62 return printer, nil
63 }
64
65
66
67 func (f *JSONYamlPrintFlags) AddFlags(c *cobra.Command) {
68 if f == nil {
69 return
70 }
71
72 c.Flags().BoolVar(&f.ShowManagedFields, "show-managed-fields", f.ShowManagedFields, "If true, keep the managedFields when printing objects in JSON or YAML format.")
73 }
74
75
76
77 func NewJSONYamlPrintFlags() *JSONYamlPrintFlags {
78 return &JSONYamlPrintFlags{}
79 }
80
View as plain text