Skip to content

Commit

Permalink
Merge pull request #1936 from gdbranco/fix/ocm-6391-2
Browse files Browse the repository at this point in the history
OCM-6391 | fix: only show message when region is changed and no output mode set
  • Loading branch information
openshift-merge-bot[bot] committed Apr 16, 2024
2 parents f1d01e7 + 4af4b30 commit a43c113
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 5 deletions.
8 changes: 7 additions & 1 deletion pkg/arguments/arguments.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
45 changes: 41 additions & 4 deletions pkg/arguments/arguments_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ var _ = Describe("Client", func() {
var (
cmd *cobra.Command
childCmd *cobra.Command
o string
)

Context("Region deprecation test", func() {
Expand All @@ -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() {
Expand Down

0 comments on commit a43c113

Please sign in to comment.