mirror of
https://github.com/agent-substrate/substrate.git
synced 2026-10-02 03:24:42 +08:00
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>
This commit is contained in:
@@ -29,9 +29,9 @@ var (
|
||||
)
|
||||
|
||||
var getActorsCmd = &cobra.Command{
|
||||
Use: "actors <actor-name>",
|
||||
Use: "actors <actor-name ...>",
|
||||
Aliases: []string{"actor"},
|
||||
Short: "List all actors or get a specific actor",
|
||||
Short: "List all actors or get one or more actors",
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
ctx := cmd.Context()
|
||||
|
||||
@@ -42,21 +42,26 @@ var getActorsCmd = &cobra.Command{
|
||||
}
|
||||
defer apiClient.Close()
|
||||
|
||||
// 2. Handle Get Single Actor
|
||||
// 2. Handle Get Actors
|
||||
if len(args) > 0 {
|
||||
// A single actor is addressed by (atespace, name), so the atespace is
|
||||
// An actor is addressed by (atespace, name), so the atespace is
|
||||
// mandatory and "all atespaces" is meaningless here.
|
||||
if getActorsAllAtespaces {
|
||||
return fmt.Errorf("-A/--all-atespaces cannot be used when getting a specific actor; pass --atespace")
|
||||
return fmt.Errorf("-A/--all-atespaces cannot be used when getting actors; pass --atespace")
|
||||
}
|
||||
if getActorsAtespaceFlag == "" {
|
||||
return fmt.Errorf("--atespace is required when getting a specific actor")
|
||||
return fmt.Errorf("--atespace is required when getting actors")
|
||||
}
|
||||
resp, err := apiClient.GetActor(ctx, &ateapipb.GetActorRequest{Actor: &ateapipb.ObjectRef{Atespace: getActorsAtespaceFlag, Name: args[0]}})
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get actor: %w", err)
|
||||
|
||||
actors := make([]*ateapipb.Actor, 0, len(args))
|
||||
for _, actorName := range args {
|
||||
resp, err := apiClient.GetActor(ctx, &ateapipb.GetActorRequest{Actor: &ateapipb.ObjectRef{Atespace: getActorsAtespaceFlag, Name: actorName}})
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get actor %q: %w", actorName, err)
|
||||
}
|
||||
actors = append(actors, resp)
|
||||
}
|
||||
return printer.PrintActor(resp, outputFmt)
|
||||
return printer.PrintActors(actors, outputFmt)
|
||||
}
|
||||
|
||||
// Listing requires exactly one of --atespace (one atespace) or -A (all
|
||||
@@ -94,7 +99,7 @@ var getActorsCmd = &cobra.Command{
|
||||
}
|
||||
|
||||
func init() {
|
||||
getActorsCmd.Flags().StringVarP(&getActorsAtespaceFlag, "atespace", "a", "", "Atespace to list/get actors in. Required for a single actor; for listing, use this or -A.")
|
||||
getActorsCmd.Flags().StringVarP(&getActorsAtespaceFlag, "atespace", "a", "", "Atespace to list/get actors in. Required when getting actors; for listing, use this or -A.")
|
||||
getActorsCmd.Flags().BoolVarP(&getActorsAllAtespaces, "all-atespaces", "A", false, "List actors across all atespaces (listing only; mutually exclusive with --atespace)")
|
||||
getCmd.AddCommand(getActorsCmd)
|
||||
}
|
||||
|
||||
@@ -24,9 +24,9 @@ import (
|
||||
)
|
||||
|
||||
var getAtespacesCmd = &cobra.Command{
|
||||
Use: "atespaces [name]",
|
||||
Use: "atespaces [name ...]",
|
||||
Aliases: []string{"atespace"},
|
||||
Short: "List all atespaces or get a specific atespace",
|
||||
Short: "List all atespaces or get one or more atespaces",
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
ctx := cmd.Context()
|
||||
apiClient, err := ateclient.NewClient(ctx, kubeconfig, k8sContext, endpoint, traceEnabled)
|
||||
@@ -36,11 +36,15 @@ var getAtespacesCmd = &cobra.Command{
|
||||
defer apiClient.Close()
|
||||
|
||||
if len(args) > 0 {
|
||||
resp, err := apiClient.GetAtespace(ctx, &ateapipb.GetAtespaceRequest{Atespace: &ateapipb.ObjectRef{Name: args[0]}})
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get atespace: %w", err)
|
||||
atespaces := make([]*ateapipb.Atespace, 0, len(args))
|
||||
for _, atespaceName := range args {
|
||||
resp, err := apiClient.GetAtespace(ctx, &ateapipb.GetAtespaceRequest{Atespace: &ateapipb.ObjectRef{Name: atespaceName}})
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get atespace %q: %w", atespaceName, err)
|
||||
}
|
||||
atespaces = append(atespaces, resp)
|
||||
}
|
||||
return printer.PrintAtespace(resp, outputFmt)
|
||||
return printer.PrintAtespaces(atespaces, outputFmt)
|
||||
}
|
||||
|
||||
resp, err := apiClient.ListAtespaces(ctx, &ateapipb.ListAtespacesRequest{})
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
// Copyright 2026 Google LLC
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
func TestGetCommandArgs(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
command *cobra.Command
|
||||
args []string
|
||||
wantErr bool
|
||||
}{
|
||||
{name: "actors list", command: getActorsCmd},
|
||||
{name: "actors get", command: getActorsCmd, args: []string{"actor-1"}},
|
||||
{name: "actors get multiple", command: getActorsCmd, args: []string{"actor-1", "actor-2"}},
|
||||
{name: "atespaces list", command: getAtespacesCmd},
|
||||
{name: "atespaces get", command: getAtespacesCmd, args: []string{"team-a"}},
|
||||
{name: "atespaces get multiple", command: getAtespacesCmd, args: []string{"team-a", "team-b"}},
|
||||
{name: "workers list", command: getWorkersCmd},
|
||||
{name: "workers reject argument", command: getWorkersCmd, args: []string{"worker-1"}, wantErr: true},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
var err error
|
||||
if test.command.Args != nil {
|
||||
err = test.command.Args(test.command, test.args)
|
||||
}
|
||||
if (err != nil) != test.wantErr {
|
||||
t.Fatalf("Args(%q) error = %v, wantErr %t", test.args, err, test.wantErr)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -27,6 +27,7 @@ var getWorkersCmd = &cobra.Command{
|
||||
Use: "workers",
|
||||
Aliases: []string{"worker"},
|
||||
Short: "List all workers",
|
||||
Args: cobra.NoArgs,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
ctx := cmd.Context()
|
||||
apiClient, err := ateclient.NewClient(ctx, kubeconfig, k8sContext, endpoint, traceEnabled)
|
||||
|
||||
Reference in New Issue
Block a user