Skip to content

Commit 6dc80c0

Browse files
authored
add argument validation for get commands (#440)
Adds argument count validation to get actors/atespaces/workers. - [x] Tests pass - [ ] Appropriate changes to documentation are included in the PR --------- Signed-off-by: Peter Jausovec <peter.jausovec@solo.io>
1 parent a320da4 commit 6dc80c0

4 files changed

Lines changed: 78 additions & 17 deletions

File tree

cmd/kubectl-ate/internal/cmd/get_actors.go

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ var (
2929
)
3030

3131
var getActorsCmd = &cobra.Command{
32-
Use: "actors <actor-name>",
32+
Use: "actors <actor-name ...>",
3333
Aliases: []string{"actor"},
34-
Short: "List all actors or get a specific actor",
34+
Short: "List all actors or get one or more actors",
3535
RunE: func(cmd *cobra.Command, args []string) error {
3636
ctx := cmd.Context()
3737

@@ -42,21 +42,26 @@ var getActorsCmd = &cobra.Command{
4242
}
4343
defer apiClient.Close()
4444

45-
// 2. Handle Get Single Actor
45+
// 2. Handle Get Actors
4646
if len(args) > 0 {
47-
// A single actor is addressed by (atespace, name), so the atespace is
47+
// An actor is addressed by (atespace, name), so the atespace is
4848
// mandatory and "all atespaces" is meaningless here.
4949
if getActorsAllAtespaces {
50-
return fmt.Errorf("-A/--all-atespaces cannot be used when getting a specific actor; pass --atespace")
50+
return fmt.Errorf("-A/--all-atespaces cannot be used when getting actors; pass --atespace")
5151
}
5252
if getActorsAtespaceFlag == "" {
53-
return fmt.Errorf("--atespace is required when getting a specific actor")
53+
return fmt.Errorf("--atespace is required when getting actors")
5454
}
55-
resp, err := apiClient.GetActor(ctx, &ateapipb.GetActorRequest{Actor: &ateapipb.ObjectRef{Atespace: getActorsAtespaceFlag, Name: args[0]}})
56-
if err != nil {
57-
return fmt.Errorf("failed to get actor: %w", err)
55+
56+
actors := make([]*ateapipb.Actor, 0, len(args))
57+
for _, actorName := range args {
58+
resp, err := apiClient.GetActor(ctx, &ateapipb.GetActorRequest{Actor: &ateapipb.ObjectRef{Atespace: getActorsAtespaceFlag, Name: actorName}})
59+
if err != nil {
60+
return fmt.Errorf("failed to get actor %q: %w", actorName, err)
61+
}
62+
actors = append(actors, resp)
5863
}
59-
return printer.PrintActor(resp, outputFmt)
64+
return printer.PrintActors(actors, outputFmt)
6065
}
6166

6267
// Listing requires exactly one of --atespace (one atespace) or -A (all
@@ -94,7 +99,7 @@ var getActorsCmd = &cobra.Command{
9499
}
95100

96101
func init() {
97-
getActorsCmd.Flags().StringVarP(&getActorsAtespaceFlag, "atespace", "a", "", "Atespace to list/get actors in. Required for a single actor; for listing, use this or -A.")
102+
getActorsCmd.Flags().StringVarP(&getActorsAtespaceFlag, "atespace", "a", "", "Atespace to list/get actors in. Required when getting actors; for listing, use this or -A.")
98103
getActorsCmd.Flags().BoolVarP(&getActorsAllAtespaces, "all-atespaces", "A", false, "List actors across all atespaces (listing only; mutually exclusive with --atespace)")
99104
getCmd.AddCommand(getActorsCmd)
100105
}

cmd/kubectl-ate/internal/cmd/get_atespaces.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ import (
2424
)
2525

2626
var getAtespacesCmd = &cobra.Command{
27-
Use: "atespaces [name]",
27+
Use: "atespaces [name ...]",
2828
Aliases: []string{"atespace"},
29-
Short: "List all atespaces or get a specific atespace",
29+
Short: "List all atespaces or get one or more atespaces",
3030
RunE: func(cmd *cobra.Command, args []string) error {
3131
ctx := cmd.Context()
3232
apiClient, err := ateclient.NewClient(ctx, kubeconfig, k8sContext, endpoint, traceEnabled)
@@ -36,11 +36,15 @@ var getAtespacesCmd = &cobra.Command{
3636
defer apiClient.Close()
3737

3838
if len(args) > 0 {
39-
resp, err := apiClient.GetAtespace(ctx, &ateapipb.GetAtespaceRequest{Atespace: &ateapipb.ObjectRef{Name: args[0]}})
40-
if err != nil {
41-
return fmt.Errorf("failed to get atespace: %w", err)
39+
atespaces := make([]*ateapipb.Atespace, 0, len(args))
40+
for _, atespaceName := range args {
41+
resp, err := apiClient.GetAtespace(ctx, &ateapipb.GetAtespaceRequest{Atespace: &ateapipb.ObjectRef{Name: atespaceName}})
42+
if err != nil {
43+
return fmt.Errorf("failed to get atespace %q: %w", atespaceName, err)
44+
}
45+
atespaces = append(atespaces, resp)
4246
}
43-
return printer.PrintAtespace(resp, outputFmt)
47+
return printer.PrintAtespaces(atespaces, outputFmt)
4448
}
4549

4650
resp, err := apiClient.ListAtespaces(ctx, &ateapipb.ListAtespacesRequest{})
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright 2026 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package cmd
16+
17+
import (
18+
"testing"
19+
20+
"github.com/spf13/cobra"
21+
)
22+
23+
func TestGetCommandArgs(t *testing.T) {
24+
tests := []struct {
25+
name string
26+
command *cobra.Command
27+
args []string
28+
wantErr bool
29+
}{
30+
{name: "actors list", command: getActorsCmd},
31+
{name: "actors get", command: getActorsCmd, args: []string{"actor-1"}},
32+
{name: "actors get multiple", command: getActorsCmd, args: []string{"actor-1", "actor-2"}},
33+
{name: "atespaces list", command: getAtespacesCmd},
34+
{name: "atespaces get", command: getAtespacesCmd, args: []string{"team-a"}},
35+
{name: "atespaces get multiple", command: getAtespacesCmd, args: []string{"team-a", "team-b"}},
36+
{name: "workers list", command: getWorkersCmd},
37+
{name: "workers reject argument", command: getWorkersCmd, args: []string{"worker-1"}, wantErr: true},
38+
}
39+
40+
for _, test := range tests {
41+
t.Run(test.name, func(t *testing.T) {
42+
var err error
43+
if test.command.Args != nil {
44+
err = test.command.Args(test.command, test.args)
45+
}
46+
if (err != nil) != test.wantErr {
47+
t.Fatalf("Args(%q) error = %v, wantErr %t", test.args, err, test.wantErr)
48+
}
49+
})
50+
}
51+
}

cmd/kubectl-ate/internal/cmd/get_workers.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ var getWorkersCmd = &cobra.Command{
2727
Use: "workers",
2828
Aliases: []string{"worker"},
2929
Short: "List all workers",
30+
Args: cobra.NoArgs,
3031
RunE: func(cmd *cobra.Command, args []string) error {
3132
ctx := cmd.Context()
3233
apiClient, err := ateclient.NewClient(ctx, kubeconfig, k8sContext, endpoint, traceEnabled)

0 commit comments

Comments
 (0)