Skip to content

Commit

Permalink
cli: add get + delete warehouse commands (#1087)
Browse files Browse the repository at this point in the history
Signed-off-by: Kent <[email protected]>
  • Loading branch information
krancour authored Nov 11, 2023
1 parent 6683c4c commit db85549
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 0 deletions.
1 change: 1 addition & 0 deletions internal/cli/delete/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,6 @@ kargo delete -f stage.yaml
// Subcommands
cmd.AddCommand(newProjectCommand(opt))
cmd.AddCommand(newStageCommand(opt))
cmd.AddCommand(newWarehouseCommand(opt))
return cmd
}
60 changes: 60 additions & 0 deletions internal/cli/delete/warehouse.go
Original file line number Diff line number Diff line change
@@ -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
}
1 change: 1 addition & 0 deletions internal/cli/get/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
89 changes: 89 additions & 0 deletions internal/cli/get/warehouse.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit db85549

Please sign in to comment.