Files
2024-01-19 11:33:42 +00:00

93 lines
2.1 KiB
Go

package holds
import (
"fmt"
"github.com/formancehq/fctl/cmd/wallets/internal/views"
fctl "github.com/formancehq/fctl/pkg"
"github.com/formancehq/formance-sdk-go/v2/pkg/models/operations"
"github.com/formancehq/formance-sdk-go/v2/pkg/models/shared"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
type ShowStore struct {
Hold shared.ExpandedDebitHold `json:"hold"`
}
type ShowController struct {
store *ShowStore
}
var _ fctl.Controller[*ShowStore] = (*ShowController)(nil)
func NewDefaultShowStore() *ShowStore {
return &ShowStore{}
}
func NewShowController() *ShowController {
return &ShowController{
store: NewDefaultShowStore(),
}
}
func NewShowCommand() *cobra.Command {
return fctl.NewCommand("show <hold-id>",
fctl.WithShortDescription("Show a hold"),
fctl.WithAliases("sh"),
fctl.WithArgs(cobra.ExactArgs(1)),
fctl.WithController[*ShowStore](NewShowController()),
)
}
func (c *ShowController) GetStore() *ShowStore {
return c.store
}
func (c *ShowController) Run(cmd *cobra.Command, args []string) (fctl.Renderable, error) {
cfg, err := fctl.GetConfig(cmd)
if err != nil {
return nil, errors.Wrap(err, "retrieving config")
}
organizationID, err := fctl.ResolveOrganizationID(cmd, cfg)
if err != nil {
return nil, err
}
stack, err := fctl.ResolveStack(cmd, cfg, organizationID)
if err != nil {
return nil, err
}
client, err := fctl.NewStackClient(cmd, cfg, stack)
if err != nil {
return nil, errors.Wrap(err, "creating stack client")
}
request := operations.GetHoldRequest{
HoldID: args[0],
}
response, err := client.Wallets.GetHold(cmd.Context(), request)
if err != nil {
return nil, errors.Wrap(err, "getting hold")
}
if response.WalletsErrorResponse != nil {
return nil, fmt.Errorf("%s: %s", response.WalletsErrorResponse.ErrorCode, response.WalletsErrorResponse.ErrorMessage)
}
if response.StatusCode >= 300 {
return nil, fmt.Errorf("unexpected status code: %d", response.StatusCode)
}
c.store.Hold = response.GetHoldResponse.Data
return c, nil
}
func (c *ShowController) Render(cmd *cobra.Command, args []string) error {
return views.PrintHold(cmd.OutOrStdout(), c.store.Hold)
}