From db85549092b759c0b7238edb1a45d3c09b7a0f52 Mon Sep 17 00:00:00 2001 From: Kent Rancourt Date: Fri, 10 Nov 2023 22:01:51 -0600 Subject: [PATCH] cli: add get + delete warehouse commands (#1087) Signed-off-by: Kent --- internal/cli/delete/delete.go | 1 + internal/cli/delete/warehouse.go | 60 +++++++++++++++++++++ internal/cli/get/get.go | 1 + internal/cli/get/warehouse.go | 89 ++++++++++++++++++++++++++++++++ 4 files changed, 151 insertions(+) create mode 100644 internal/cli/delete/warehouse.go create mode 100644 internal/cli/get/warehouse.go diff --git a/internal/cli/delete/delete.go b/internal/cli/delete/delete.go index 8f8184eef..fef6be33d 100644 --- a/internal/cli/delete/delete.go +++ b/internal/cli/delete/delete.go @@ -91,5 +91,6 @@ kargo delete -f stage.yaml // Subcommands cmd.AddCommand(newProjectCommand(opt)) cmd.AddCommand(newStageCommand(opt)) + cmd.AddCommand(newWarehouseCommand(opt)) return cmd } diff --git a/internal/cli/delete/warehouse.go b/internal/cli/delete/warehouse.go new file mode 100644 index 000000000..32dff9590 --- /dev/null +++ b/internal/cli/delete/warehouse.go @@ -0,0 +1,60 @@ +package delete + +import ( + goerrors "errors" + "fmt" + + "connectrpc.com/connect" + "github.com/pkg/errors" + "github.com/spf13/cobra" + "golang.org/x/exp/slices" + + "github.com/akuity/kargo/internal/cli/client" + "github.com/akuity/kargo/internal/cli/option" + v1alpha1 "github.com/akuity/kargo/pkg/api/service/v1alpha1" +) + +func newWarehouseCommand(opt *option.Option) *cobra.Command { + cmd := &cobra.Command{ + Use: "warehouse [NAME]...", + Short: "Delete warehouse by name", + Args: cobra.MinimumNArgs(1), + Example: ` +# Delete warehouse +kargo delete warehouse --project=my-project my-warehouse +`, + RunE: func(cmd *cobra.Command, args []string) error { + ctx := cmd.Context() + kargoSvcCli, err := client.GetClientFromConfig(ctx, opt) + if err != nil { + return errors.New("get client from config") + } + + project := opt.Project.OrElse("") + if project == "" { + return errors.New("project is required") + } + + var resErr error + for _, name := range slices.Compact(args) { + if _, err := kargoSvcCli.DeleteWarehouse( + ctx, + connect.NewRequest( + &v1alpha1.DeleteWarehouseRequest{ + Project: project, + Name: name, + }, + ), + ); err != nil { + resErr = goerrors.Join(resErr, errors.Wrap(err, "Error")) + continue + } + _, _ = fmt.Fprintf(opt.IOStreams.Out, "Warehouse Deleted: %q\n", name) + } + return resErr + }, + } + opt.PrintFlags.AddFlags(cmd) + option.OptionalProject(opt.Project)(cmd.Flags()) + return cmd +} diff --git a/internal/cli/get/get.go b/internal/cli/get/get.go index 11ff77819..460ebc7ae 100644 --- a/internal/cli/get/get.go +++ b/internal/cli/get/get.go @@ -32,6 +32,7 @@ kargo get promotions --project=my-project --stage=my-stage cmd.AddCommand(newGetProjectsCommand(opt)) cmd.AddCommand(newGetPromotionsCommand(opt)) cmd.AddCommand(newGetStagesCommand(opt)) + cmd.AddCommand(newGetWarehousesCommand(opt)) return cmd } diff --git a/internal/cli/get/warehouse.go b/internal/cli/get/warehouse.go new file mode 100644 index 000000000..07574e987 --- /dev/null +++ b/internal/cli/get/warehouse.go @@ -0,0 +1,89 @@ +package get + +import ( + goerrors "errors" + + "connectrpc.com/connect" + "github.com/pkg/errors" + "github.com/spf13/cobra" + "golang.org/x/exp/slices" + + kargoapi "github.com/akuity/kargo/api/v1alpha1" + typesv1alpha1 "github.com/akuity/kargo/internal/api/types/v1alpha1" + "github.com/akuity/kargo/internal/cli/client" + "github.com/akuity/kargo/internal/cli/option" + v1alpha1 "github.com/akuity/kargo/pkg/api/service/v1alpha1" +) + +func newGetWarehousesCommand(opt *option.Option) *cobra.Command { + cmd := &cobra.Command{ + Use: "warehouses --project=project [NAME...]", + Aliases: []string{"warehouse"}, + Short: "Display one or many warehouses", + Example: ` +# List all warehouses in the project +kargo get warehouses --project=my-project + +# List all warehouses in JSON output format +kargo get warehouses --project=my-project -o json + +# Get a warehouse in the project +kargo get warehouses --project=my-project my-warehouse +`, + RunE: func(cmd *cobra.Command, args []string) error { + ctx := cmd.Context() + + project := opt.Project.OrElse("") + if project == "" { + return errors.New("project is required") + } + + kargoSvcCli, err := client.GetClientFromConfig(ctx, opt) + if err != nil { + return errors.New("get client from config") + } + resp, err := kargoSvcCli.ListWarehouses( + ctx, + connect.NewRequest( + &v1alpha1.ListWarehousesRequest{ + Project: project, + }, + ), + ) + if err != nil { + return errors.Wrap(err, "list warehouses") + } + + names := slices.Compact(args) + res := make([]*kargoapi.Warehouse, 0, len(resp.Msg.GetWarehouses())) + var resErr error + if len(names) == 0 { + for _, w := range resp.Msg.GetWarehouses() { + res = append(res, typesv1alpha1.FromWarehouseProto(w)) + } + } else { + warehousesByName := + make(map[string]*kargoapi.Warehouse, len(resp.Msg.GetWarehouses())) + for _, w := range resp.Msg.GetWarehouses() { + warehousesByName[w.GetMetadata().GetName()] = + typesv1alpha1.FromWarehouseProto(w) + } + for _, name := range names { + if warehouse, ok := warehousesByName[name]; ok { + res = append(res, warehouse) + } else { + resErr = + goerrors.Join(err, errors.Errorf("warehouse %q not found", name)) + } + } + } + if err := printObjects(opt, res); err != nil { + return err + } + return resErr + }, + } + option.OptionalProject(opt.Project)(cmd.Flags()) + opt.PrintFlags.AddFlags(cmd) + return cmd +}