...
1
16
17 package genericclioptions
18
19 import (
20 "strings"
21
22 "github.com/spf13/cobra"
23 "github.com/spf13/pflag"
24
25 "k8s.io/cli-runtime/pkg/resource"
26 )
27
28
29
30
31
32 type FileNameFlags struct {
33 Usage string
34
35 Filenames *[]string
36 Kustomize *string
37 Recursive *bool
38 }
39
40
41 func (o *FileNameFlags) ToOptions() resource.FilenameOptions {
42 options := resource.FilenameOptions{}
43
44 if o == nil {
45 return options
46 }
47
48 if o.Recursive != nil {
49 options.Recursive = *o.Recursive
50 }
51 if o.Filenames != nil {
52 options.Filenames = *o.Filenames
53 }
54 if o.Kustomize != nil {
55 options.Kustomize = *o.Kustomize
56 }
57
58 return options
59 }
60
61
62 func (o *FileNameFlags) AddFlags(flags *pflag.FlagSet) {
63 if o == nil {
64 return
65 }
66
67 if o.Recursive != nil {
68 flags.BoolVarP(o.Recursive, "recursive", "R", *o.Recursive, "Process the directory used in -f, --filename recursively. Useful when you want to manage related manifests organized within the same directory.")
69 }
70 if o.Filenames != nil {
71 flags.StringSliceVarP(o.Filenames, "filename", "f", *o.Filenames, o.Usage)
72 annotations := make([]string, 0, len(resource.FileExtensions))
73 for _, ext := range resource.FileExtensions {
74 annotations = append(annotations, strings.TrimLeft(ext, "."))
75 }
76 flags.SetAnnotation("filename", cobra.BashCompFilenameExt, annotations)
77 }
78 if o.Kustomize != nil {
79 flags.StringVarP(o.Kustomize, "kustomize", "k", *o.Kustomize,
80 "Process a kustomization directory. This flag can't be used together with -f or -R.")
81 }
82 }
83
View as plain text