Files
stack/components/fctl/cmd/wallets/create.go

86 lines
2.2 KiB
Go

package wallets
import (
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/pterm/pterm"
"github.com/spf13/cobra"
)
type CreateStore struct {
WalletID string `json:"walletId"`
}
type CreateController struct {
store *CreateStore
metadataFlag string
ikFlag string
}
var _ fctl.Controller[*CreateStore] = (*CreateController)(nil)
func NewDefaultCreateStore() *CreateStore {
return &CreateStore{}
}
func NewCreateController() *CreateController {
return &CreateController{
store: NewDefaultCreateStore(),
metadataFlag: "metadata",
ikFlag: "ik",
}
}
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.WithStringFlag(c.ikFlag, "", "Idempotency key"),
fctl.WithController[*CreateStore](c),
)
}
func (c *CreateController) GetStore() *CreateStore {
return c.store
}
func (c *CreateController) Run(cmd *cobra.Command, args []string) (fctl.Renderable, error) {
store := fctl.GetStackStore(cmd.Context())
if !fctl.CheckStackApprobation(cmd, store.Stack(), "You are about to create a wallet") {
return nil, fctl.ErrMissingApproval
}
metadata, err := fctl.ParseMetadata(fctl.GetStringSlice(cmd, c.metadataFlag))
if err != nil {
return nil, err
}
request := operations.CreateWalletRequest{
CreateWalletRequest: &shared.CreateWalletRequest{
Name: args[0],
Metadata: metadata,
},
IdempotencyKey: fctl.Ptr(fctl.GetString(cmd, c.ikFlag)),
}
response, err := store.Client().Wallets.V1.CreateWallet(cmd.Context(), request)
if err != nil {
return nil, errors.Wrap(err, "creating wallet")
}
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
}