mirror of
https://github.com/hanzo-fi/stack.git
synced 2026-10-30 20:34:43 +00:00
98 lines
2.3 KiB
Go
98 lines
2.3 KiB
Go
package accounts
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
|
|
"github.com/formancehq/fctl/cmd/payments/versions"
|
|
fctl "github.com/formancehq/fctl/pkg"
|
|
"github.com/formancehq/formance-sdk-go/v2/pkg/models/shared"
|
|
"github.com/pterm/pterm"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
type CreateStore struct {
|
|
AccountID string `json:"accountID"`
|
|
}
|
|
type CreateController struct {
|
|
PaymentsVersion versions.Version
|
|
|
|
store *CreateStore
|
|
}
|
|
|
|
func (c *CreateController) SetVersion(version versions.Version) {
|
|
c.PaymentsVersion = version
|
|
}
|
|
|
|
var _ fctl.Controller[*CreateStore] = (*CreateController)(nil)
|
|
|
|
func NewCreateStore() *CreateStore {
|
|
return &CreateStore{}
|
|
}
|
|
|
|
func NewCreateController() *CreateController {
|
|
return &CreateController{
|
|
store: NewCreateStore(),
|
|
}
|
|
}
|
|
|
|
func NewCreateCommand() *cobra.Command {
|
|
c := NewCreateController()
|
|
return fctl.NewCommand("create <file>|-",
|
|
fctl.WithConfirmFlag(),
|
|
fctl.WithShortDescription("Create an account on formance platform"),
|
|
fctl.WithAliases("cr", "c"),
|
|
fctl.WithArgs(cobra.ExactArgs(1)),
|
|
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 err := versions.GetPaymentsVersion(cmd, args, c); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if c.PaymentsVersion < versions.V1 {
|
|
return nil, fmt.Errorf("accounts creation are only supported in >= v1.0.0")
|
|
}
|
|
|
|
if !fctl.CheckStackApprobation(cmd, store.Stack(), "You are about to create an account") {
|
|
return nil, fctl.ErrMissingApproval
|
|
}
|
|
|
|
script, err := fctl.ReadFile(cmd, store.Stack(), args[0])
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
request := shared.AccountRequest{}
|
|
if err := json.Unmarshal([]byte(script), &request); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
//nolint:gosimple
|
|
response, err := store.Client().Payments.V1.CreateAccount(cmd.Context(), request)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if response.StatusCode >= 300 {
|
|
return nil, fmt.Errorf("unexpected status code: %d", response.StatusCode)
|
|
}
|
|
|
|
c.store.AccountID = response.PaymentsAccountResponse.Data.ID
|
|
|
|
return c, nil
|
|
}
|
|
|
|
func (c *CreateController) Render(cmd *cobra.Command, args []string) error {
|
|
pterm.Success.WithWriter(cmd.OutOrStdout()).Printfln("Account created with ID: %s", c.store.AccountID)
|
|
|
|
return nil
|
|
}
|