...
1
16
17 package genericclioptions
18
19 import (
20 "github.com/spf13/cobra"
21
22 "k8s.io/cli-runtime/pkg/printers"
23 )
24
25
26
27
28 type KubeTemplatePrintFlags struct {
29 GoTemplatePrintFlags *GoTemplatePrintFlags
30 JSONPathPrintFlags *JSONPathPrintFlags
31
32 AllowMissingKeys *bool
33 TemplateArgument *string
34 }
35
36
37 func (f *KubeTemplatePrintFlags) AllowedFormats() []string {
38 if f == nil {
39 return []string{}
40 }
41 return append(f.GoTemplatePrintFlags.AllowedFormats(), f.JSONPathPrintFlags.AllowedFormats()...)
42 }
43
44
45
46
47
48 func (f *KubeTemplatePrintFlags) ToPrinter(outputFormat string) (printers.ResourcePrinter, error) {
49 if f == nil {
50 return nil, NoCompatiblePrinterError{}
51 }
52
53 if p, err := f.JSONPathPrintFlags.ToPrinter(outputFormat); !IsNoCompatiblePrinterError(err) {
54 return p, err
55 }
56 return f.GoTemplatePrintFlags.ToPrinter(outputFormat)
57 }
58
59
60
61 func (f *KubeTemplatePrintFlags) AddFlags(c *cobra.Command) {
62 if f == nil {
63 return
64 }
65
66 if f.TemplateArgument != nil {
67 c.Flags().StringVar(f.TemplateArgument, "template", *f.TemplateArgument, "Template string or path to template file to use when -o=go-template, -o=go-template-file. The template format is golang templates [http://golang.org/pkg/text/template/#pkg-overview].")
68 c.MarkFlagFilename("template")
69 }
70 if f.AllowMissingKeys != nil {
71 c.Flags().BoolVar(f.AllowMissingKeys, "allow-missing-template-keys", *f.AllowMissingKeys, "If true, ignore any errors in templates when a field or map key is missing in the template. Only applies to golang and jsonpath output formats.")
72 }
73 }
74
75
76
77 func NewKubeTemplatePrintFlags() *KubeTemplatePrintFlags {
78 allowMissingKeysPtr := true
79 templateArgPtr := ""
80
81 return &KubeTemplatePrintFlags{
82 GoTemplatePrintFlags: &GoTemplatePrintFlags{
83 TemplateArgument: &templateArgPtr,
84 AllowMissingKeys: &allowMissingKeysPtr,
85 },
86 JSONPathPrintFlags: &JSONPathPrintFlags{
87 TemplateArgument: &templateArgPtr,
88 AllowMissingKeys: &allowMissingKeysPtr,
89 },
90
91 TemplateArgument: &templateArgPtr,
92 AllowMissingKeys: &allowMissingKeysPtr,
93 }
94 }
95
View as plain text