Skip to content

Commit

Permalink
[refactor][kubectl-plugin] require one posarg in session
Browse files Browse the repository at this point in the history
command with Cobra's built-in `cobra.ExactArgs(1)` instead of writing our own
logic.

## Before

```console
$ kubectl ray session

Error: session (RAYCLUSTER | TYPE/NAME)
See 'ray session -h' for help and examples
```

## After

```console
$ kubectl ray session

Error: accepts 1 arg(s), received 0
```

Signed-off-by: David Xia <[email protected]>
  • Loading branch information
davidxia committed Feb 11, 2025
1 parent 5cc2364 commit 838854d
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 9 deletions.
5 changes: 1 addition & 4 deletions kubectl-plugin/pkg/cmd/session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ func NewSessionCommand(streams genericiooptions.IOStreams) *cobra.Command {
Short: "Forward local ports to the Ray resources.",
Long: sessionLong,
Example: sessionExample,
Args: cobra.ExactArgs(1),
ValidArgsFunction: completion.RayClusterResourceNameCompletionFunc(factory),
RunE: func(cmd *cobra.Command, args []string) error {
if err := options.Complete(cmd, args); err != nil {
Expand All @@ -106,10 +107,6 @@ func NewSessionCommand(streams genericiooptions.IOStreams) *cobra.Command {
}

func (options *SessionOptions) Complete(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return cmdutil.UsageErrorf(cmd, "%s", cmd.Use)
}

typeAndName := strings.Split(args[0], "/")
if len(typeAndName) == 1 {
options.ResourceType = util.RayCluster
Expand Down
5 changes: 0 additions & 5 deletions kubectl-plugin/pkg/cmd/session/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,6 @@ func TestComplete(t *testing.T) {
expectedName: "test-resource",
hasErr: false,
},
{
name: "invalid args (no args)",
args: []string{},
hasErr: true,
},
{
name: "invalid args (too many args)",
args: []string{"raycluster/test-raycluster", "extra-arg"},
Expand Down

0 comments on commit 838854d

Please sign in to comment.