Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OCM-6391 | fix: only show message when region is changed and no output mode set #1936

Merged
merged 1 commit into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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