From 8367740c6fa796dfef36834f47e34c50df0bcddb Mon Sep 17 00:00:00 2001 From: Mohab Abd El-Dayem Date: Sun, 15 May 2022 17:26:29 +0200 Subject: [PATCH 1/2] Add Cobra flag helpers for file options --- pkg/commands/options/filestuff.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/pkg/commands/options/filestuff.go b/pkg/commands/options/filestuff.go index 2820aa5468..e368897f11 100644 --- a/pkg/commands/options/filestuff.go +++ b/pkg/commands/options/filestuff.go @@ -34,6 +34,24 @@ func AddFileArg(cmd *cobra.Command, fo *FilenameOptions) { "Filename, directory, or URL to files to use to create the resource") cmd.Flags().BoolVarP(&fo.Recursive, "recursive", "R", fo.Recursive, "Process the directory used in -f, --filename recursively. Useful when you want to manage related manifests organized within the same directory.") + + err := cmd.MarkFlagFilename("filename", "yaml", "yml", "json") + + if err != nil { + log.Fatalf("Error marking filename flag as Cobra's filename: %v", err) + } + + err = cmd.MarkFlagDirname("filename") + + if err != nil { + log.Fatalf("Error marking filename flag as Cobra's Dirname: %v", err) + } + + err = cmd.MarkFlagRequired("filename") + + if err != nil { + log.Fatalf("Error marking filename flag as required: %v", err) + } } // Based heavily on pkg/kubectl From 48b79fb270158cab687f344d0f0de5d89d33d91c Mon Sep 17 00:00:00 2001 From: Mohab Abd El-Dayem Date: Sun, 29 May 2022 15:02:09 +0200 Subject: [PATCH 2/2] Keep error out of the parent scope in options/filestuff.go --- pkg/commands/options/filestuff.go | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/pkg/commands/options/filestuff.go b/pkg/commands/options/filestuff.go index e368897f11..4c547b0cb8 100644 --- a/pkg/commands/options/filestuff.go +++ b/pkg/commands/options/filestuff.go @@ -35,21 +35,15 @@ func AddFileArg(cmd *cobra.Command, fo *FilenameOptions) { cmd.Flags().BoolVarP(&fo.Recursive, "recursive", "R", fo.Recursive, "Process the directory used in -f, --filename recursively. Useful when you want to manage related manifests organized within the same directory.") - err := cmd.MarkFlagFilename("filename", "yaml", "yml", "json") - - if err != nil { + if err := cmd.MarkFlagFilename("filename", "yaml", "yml", "json"); err != nil { log.Fatalf("Error marking filename flag as Cobra's filename: %v", err) } - err = cmd.MarkFlagDirname("filename") - - if err != nil { + if err := cmd.MarkFlagDirname("filename"); err != nil { log.Fatalf("Error marking filename flag as Cobra's Dirname: %v", err) } - err = cmd.MarkFlagRequired("filename") - - if err != nil { + if err := cmd.MarkFlagRequired("filename"); err != nil { log.Fatalf("Error marking filename flag as required: %v", err) } }