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: Fixed bug where deprecation shows for parent cmd #1931

Merged
merged 1 commit into from
Apr 15, 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
2 changes: 1 addition & 1 deletion cmd/create/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func init() {
oidcprovider.Cmd, breakglasscredential.Cmd,
admin.Cmd, autoscaler.Cmd, dnsdomains.Cmd,
externalauthprovider.Cmd, idp.Cmd, kubeletconfig.Cmd,
ocmrole.Cmd, oidcprovider.Cmd, tuningconfigs.Cmd,
oidcprovider.Cmd, tuningconfigs.Cmd,
gdbranco marked this conversation as resolved.
Show resolved Hide resolved
}
arguments.MarkRegionDeprecated(Cmd, globallyAvailableCommands)
}
8 changes: 7 additions & 1 deletion pkg/arguments/arguments.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package arguments

import (
"fmt"
"os"
"strings"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -325,10 +326,15 @@ func deprecateRegion(command *cobra.Command) {
}

func MarkRegionDeprecated(parentCmd *cobra.Command, childrenCmds []*cobra.Command) {
deprecateRegion(parentCmd)
for _, cmd := range childrenCmds {
cmd.SetHelpFunc(func(command *cobra.Command, strings []string) {
deprecateRegion(parentCmd)
command.Parent().HelpFunc()(command, strings)
})
currentRun := cmd.Run
cmd.Run = func(c *cobra.Command, args []string) {
_, _ = fmt.Fprintf(os.Stdout, "%s%s\n", "\u001B[0;33mW:\u001B[m ", regionDeprecationMessage)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can check if output flag is being requested before printing

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, seems fixed in #1936 and #1937. Thanks!

currentRun(c, args)
}
}
}
18 changes: 14 additions & 4 deletions pkg/arguments/arguments_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package arguments

import (
"fmt"
"io"
"os"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
Expand All @@ -27,18 +29,26 @@ 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) {},
}

cmd.AddCommand(childCmd)

AddRegionFlag(cmd.PersistentFlags())
AddDebugFlag(cmd.PersistentFlags())
})
It("Test deprecation of region flag", func() {
MarkRegionDeprecated(cmd, []*cobra.Command{childCmd})
regionFlag := cmd.PersistentFlags().Lookup("region")
debugFlag := cmd.PersistentFlags().Lookup("debug")
Expect(regionFlag.Deprecated).To(Equal(regionDeprecationMessage))
Expect(debugFlag.Deprecated).To(Equal(""))
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"})
err := w.Close()
Expect(err).ToNot(HaveOccurred())
out, _ := io.ReadAll(r)
os.Stdout = original
Expect(string(out)).To(ContainSubstring(regionDeprecationMessage))
})
})

Expand Down