Files
2024-03-15 15:16:03 +00:00

85 lines
2.0 KiB
Go

package regions
import (
"github.com/formancehq/fctl/membershipclient"
fctl "github.com/formancehq/fctl/pkg"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
)
type CreateStore struct {
RegionId string `json:"regionId"`
Secret string `json:"secret"`
}
type CreateController struct {
store *CreateStore
}
var _ fctl.Controller[*CreateStore] = (*CreateController)(nil)
func NewDefaultCreateStore() *CreateStore {
return &CreateStore{}
}
func NewCreateController() *CreateController {
return &CreateController{
store: NewDefaultCreateStore(),
}
}
func NewCreateCommand() *cobra.Command {
return fctl.NewCommand("create [name]",
fctl.WithAliases("sh", "s"),
fctl.WithShortDescription("Show region details"),
fctl.WithArgs(cobra.RangeArgs(0, 1)),
fctl.WithController[*CreateStore](NewCreateController()),
)
}
func (c *CreateController) GetStore() *CreateStore {
return c.store
}
func (c *CreateController) Run(cmd *cobra.Command, args []string) (fctl.Renderable, error) {
store := fctl.GetMembershipStore(cmd.Context())
organizationID, err := fctl.ResolveOrganizationID(cmd, store.Config, store.Client())
if err != nil {
return nil, err
}
name := ""
if len(args) > 0 {
name = args[0]
} else {
name, err = pterm.DefaultInteractiveTextInput.WithMultiLine(false).Show("Enter a name")
if err != nil {
return nil, err
}
}
regionResponse, _, err := store.Client().CreatePrivateRegion(cmd.Context(), organizationID).
CreatePrivateRegionRequest(membershipclient.CreatePrivateRegionRequest{
Name: name,
}).
Execute()
if err != nil {
return nil, err
}
c.store.RegionId = regionResponse.Data.Id
c.store.Secret = *regionResponse.Data.Secret.Clear
return c, nil
}
func (c *CreateController) Render(cmd *cobra.Command, args []string) error {
pterm.Success.WithWriter(cmd.OutOrStdout()).Printfln(
"Region created successfully with ID: %s", c.store.RegionId)
pterm.Success.WithWriter(cmd.OutOrStdout()).Printfln(
"Your secret is (keep it safe, we will not be able to give it to you again): %s", c.store.Secret)
return nil
}