From 4af4b300c5c866563f1ba3c6e3fcb1deca71d287 Mon Sep 17 00:00:00 2001 From: Guilherme Branco Date: Tue, 16 Apr 2024 10:27:09 -0300 Subject: [PATCH] OCM-6391 | fix: only show message when region is changed and no output mode set --- pkg/arguments/arguments.go | 8 +++++- pkg/arguments/arguments_test.go | 45 ++++++++++++++++++++++++++++++--- 2 files changed, 48 insertions(+), 5 deletions(-) diff --git a/pkg/arguments/arguments.go b/pkg/arguments/arguments.go index 570a0f8ab..ac6cc1f1c 100644 --- a/pkg/arguments/arguments.go +++ b/pkg/arguments/arguments.go @@ -333,7 +333,13 @@ func MarkRegionDeprecated(parentCmd *cobra.Command, childrenCmds []*cobra.Comman }) currentRun := cmd.Run cmd.Run = func(c *cobra.Command, args []string) { - _, _ = fmt.Fprintf(os.Stdout, "%s%s\n", "\u001B[0;33mW:\u001B[m ", regionDeprecationMessage) + outputFlag := cmd.Flag("output") + regionFlag := cmd.Flag("region") + hasChangedOutputFlag := outputFlag != nil && outputFlag.Value.String() != outputFlag.DefValue + hasChangedRegionFlag := regionFlag != nil && regionFlag.Value.String() != regionFlag.DefValue + if hasChangedRegionFlag && !hasChangedOutputFlag { + _, _ = fmt.Fprintf(os.Stdout, "%s%s\n", "\u001B[0;33mW:\u001B[m ", regionDeprecationMessage) + } currentRun(c, args) } } diff --git a/pkg/arguments/arguments_test.go b/pkg/arguments/arguments_test.go index f87788063..12164324c 100644 --- a/pkg/arguments/arguments_test.go +++ b/pkg/arguments/arguments_test.go @@ -14,6 +14,7 @@ var _ = Describe("Client", func() { var ( cmd *cobra.Command childCmd *cobra.Command + o string ) Context("Region deprecation test", func() { @@ -29,27 +30,63 @@ var _ = Describe("Client", func() { Short: "Child command used for testing deprecation", Long: "This child command is used for testing the deprecation of the 'region' flag in " + "arguments.go - it is used for nothing else.", - Run: func(c *cobra.Command, a []string) {}, + Run: func(c *cobra.Command, a []string) { + //nothing to be done + }, } cmd.AddCommand(childCmd) AddRegionFlag(cmd.PersistentFlags()) AddDebugFlag(cmd.PersistentFlags()) + flagSet := cmd.PersistentFlags() + flagSet.StringVarP( + &o, + "output", + "o", + "", + "", + ) + }) + It("Without setting region", func() { + MarkRegionDeprecated(cmd, []*cobra.Command{childCmd}) + original := os.Stdout + r, w, _ := os.Pipe() + os.Stdout = w + childCmd.Run(childCmd, []string{}) + err := w.Close() + Expect(err).ToNot(HaveOccurred()) + out, _ := io.ReadAll(r) + os.Stdout = original + Expect(string(out)).To(BeEmpty()) }) - It("Test deprecation of region flag", func() { + It("Setting region", func() { MarkRegionDeprecated(cmd, []*cobra.Command{childCmd}) original := os.Stdout r, w, _ := os.Pipe() os.Stdout = w - childCmd.SetArgs([]string{"--region", "us-east-1"}) - childCmd.Run(childCmd, []string{"--region", "us-east-1"}) + childCmd.Flag("region").Value.Set("us-east-1") + childCmd.Run(childCmd, []string{}) err := w.Close() Expect(err).ToNot(HaveOccurred()) out, _ := io.ReadAll(r) os.Stdout = original Expect(string(out)).To(ContainSubstring(regionDeprecationMessage)) }) + It("Setting output to json", func() { + MarkRegionDeprecated(cmd, []*cobra.Command{childCmd}) + original := os.Stdout + r, w, _ := os.Pipe() + os.Stdout = w + childCmd.Flag("region").Value.Set("us-east-1") + childCmd.Flag("output").Value.Set("json") + childCmd.Run(childCmd, []string{}) + err := w.Close() + Expect(err).ToNot(HaveOccurred()) + out, _ := io.ReadAll(r) + os.Stdout = original + Expect(string(out)).To(BeEmpty()) + }) }) Context("Test PreprocessUnknownFlagsWithId func", func() {