-
Notifications
You must be signed in to change notification settings - Fork 462
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
[refactor][kubectl-plugin] use cobra's posargs length validator #2985
base: master
Are you sure you want to change the base?
Conversation
@@ -22,7 +22,7 @@ import ( | |||
type GetClusterOptions struct { | |||
configFlags *genericclioptions.ConfigFlags | |||
ioStreams *genericclioptions.IOStreams | |||
args []string | |||
cluster string |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rename to be more obvious what this variable is for
@@ -43,6 +43,7 @@ func NewGetClusterCommand(streams genericclioptions.IOStreams) *cobra.Command { | |||
Short: "Get cluster information.", | |||
SilenceUsage: true, | |||
ValidArgsFunction: completion.RayClusterCompletionFunc(cmdFactory), | |||
Args: cobra.MaximumNArgs(1), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the important part
@@ -68,7 +69,10 @@ func (options *GetClusterOptions) Complete(args []string) error { | |||
options.AllNamespaces = true | |||
} | |||
|
|||
options.args = args | |||
if len(args) >= 1 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there should never be more than one positional argument, but just ignore the rest instead of erroring if there is for some reason
if len(options.args) > 1 { | ||
return fmt.Errorf("too many arguments, either one or no arguments are allowed") | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
now we don't need this
@@ -21,22 +21,62 @@ import ( | |||
rayClientFake "github.com/ray-project/kuberay/ray-operator/pkg/client/clientset/versioned/fake" | |||
) | |||
|
|||
// This is to test Complete() and ensure that it is setting the namespace and arguments correctly | |||
// This is to test Complete() and ensure that it is setting the namespace and cluster correctly | |||
// No validation test is done here | |||
func TestRayClusterGetComplete(t *testing.T) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
test all the cases
@@ -98,24 +138,6 @@ func TestRayClusterGetValidate(t *testing.T) { | |||
ioStreams: &testStreams, | |||
}, | |||
}, | |||
{ | |||
name: "Test validation when more than 1 arg", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
test no longer needed
@davidxia would you mind resolving the conflict? |
We're currently validating the number of positional arguments ourselves. We can do this with `cobra.Command{Args: cobra.MaximumNArgs(1)}` instead to simplify logic. There are no functional changes. The error message changes slightly but still conveys the same information. ## Before ```console $ kubectl ray --context gke_kubeflow-platform_europe-west4-b_ml-compute-1 get cluster foo bar Error: too many arguments, either one or no arguments are allowed ``` ## After ```console $ kubectl ray get cluster foo bar Error: accepts at most 1 arg(s), received 2 ``` Signed-off-by: David Xia <david@davidxia.com>
1cf6d45
to
b9e593d
Compare
thanks, resolved the conflicts |
We're currently validating the number of positional arguments ourselves.
We can do this with
cobra.Command{Args: cobra.MaximumNArgs(1)}
insteadto simplify logic. There are no functional changes. The error message changes
slightly but still conveys the same information.
Before
After
Signed-off-by: David Xia david@davidxia.com
Checks