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

73 lines
1.7 KiB
Go

package clients
import (
"fmt"
fctl "github.com/formancehq/fctl/pkg"
"github.com/formancehq/formance-sdk-go/v2/pkg/models/operations"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
)
type DeleteStore struct {
Success bool `json:"success"`
}
type DeleteController struct {
store *DeleteStore
}
var _ fctl.Controller[*DeleteStore] = (*DeleteController)(nil)
func NewDefaultDeleteStore() *DeleteStore {
return &DeleteStore{}
}
func NewDeleteController() *DeleteController {
return &DeleteController{
store: NewDefaultDeleteStore(),
}
}
func NewDeleteCommand() *cobra.Command {
return fctl.NewCommand("delete <client-id>",
fctl.WithConfirmFlag(),
fctl.WithArgs(cobra.ExactArgs(1)),
fctl.WithAliases("d", "del"),
fctl.WithShortDescription("Delete client"),
fctl.WithController[*DeleteStore](NewDeleteController()),
)
}
func (c *DeleteController) GetStore() *DeleteStore {
return c.store
}
func (c *DeleteController) Run(cmd *cobra.Command, args []string) (fctl.Renderable, error) {
store := fctl.GetStackStore(cmd.Context())
if !fctl.CheckStackApprobation(cmd, store.Stack(), "You are about to delete an OAuth2 client") {
return nil, fctl.ErrMissingApproval
}
request := operations.DeleteClientRequest{
ClientID: args[0],
}
response, err := store.Client().Auth.DeleteClient(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.Success = true
return c, nil
}
func (c *DeleteController) Render(cmd *cobra.Command, args []string) error {
pterm.Success.WithWriter(cmd.OutOrStdout()).Printfln("Client deleted!")
return nil
}