Files
RAGOT DavidandGitHub 0df34a4d94 feat: fctl output=<json> (#363)
+ Add controller interface
+ Add renderable interface

- A controller define his store. And Running cycle returning a renderable interface
- Json is handle between Run & Render is the proper flag is set
2023-07-07 09:36:12 +02:00

93 lines
2.1 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) {
cfg, err := fctl.GetConfig(cmd)
if err != nil {
return nil, err
}
apiClient, err := fctl.NewMembershipClient(cmd, cfg)
if err != nil {
return nil, err
}
organizationID, err := fctl.ResolveOrganizationID(cmd, cfg)
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 := apiClient.DefaultApi.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
}