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

108 lines
2.6 KiB
Go

package wallets
import (
"fmt"
fctl "github.com/formancehq/fctl/pkg"
"github.com/formancehq/formance-sdk-go/v2/pkg/models/shared"
"github.com/pkg/errors"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
)
type CreateStore struct {
WalletID string `json:"walletId"`
}
type CreateController struct {
store *CreateStore
metadataFlag string
}
var _ fctl.Controller[*CreateStore] = (*CreateController)(nil)
func NewDefaultCreateStore() *CreateStore {
return &CreateStore{}
}
func NewCreateController() *CreateController {
return &CreateController{
store: NewDefaultCreateStore(),
metadataFlag: "metadata",
}
}
func NewCreateCommand() *cobra.Command {
c := NewCreateController()
return fctl.NewCommand("create <name>",
fctl.WithShortDescription("Create a new wallet"),
fctl.WithAliases("cr"),
fctl.WithConfirmFlag(),
fctl.WithArgs(cobra.ExactArgs(1)),
fctl.WithStringSliceFlag(c.metadataFlag, []string{""}, "Metadata to use"),
fctl.WithController[*CreateStore](c),
)
}
func (c *CreateController) GetStore() *CreateStore {
return c.store
}
func (c *CreateController) 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
}
if !fctl.CheckStackApprobation(cmd, stack, "You are about to create a wallet") {
return nil, fctl.ErrMissingApproval
}
client, err := fctl.NewStackClient(cmd, cfg, stack)
if err != nil {
return nil, errors.Wrap(err, "creating stack client")
}
metadata, err := fctl.ParseMetadata(fctl.GetStringSlice(cmd, c.metadataFlag))
if err != nil {
return nil, err
}
request := shared.CreateWalletRequest{
Name: args[0],
Metadata: metadata,
}
response, err := client.Wallets.CreateWallet(cmd.Context(), &request)
if err != nil {
return nil, errors.Wrap(err, "creating wallet")
}
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.WalletID = response.CreateWalletResponse.Data.ID
return c, nil
}
func (c *CreateController) Render(cmd *cobra.Command, args []string) error {
pterm.Success.WithWriter(cmd.OutOrStdout()).Printfln(
"Wallet created successfully with ID: %s", c.store.WalletID)
return nil
}