mirror of
https://github.com/hanzo-fi/stack.git
synced 2026-10-30 20:34:43 +00:00
84 lines
1.9 KiB
Go
84 lines
1.9 KiB
Go
package policies
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
|
|
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 {
|
|
PolicyID string `json:"policyID"`
|
|
}
|
|
type CreateController struct {
|
|
store *CreateStore
|
|
}
|
|
|
|
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 a policy"),
|
|
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 !fctl.CheckStackApprobation(cmd, store.Stack(), "You are about to create a new policy") {
|
|
return nil, fctl.ErrMissingApproval
|
|
}
|
|
|
|
script, err := fctl.ReadFile(cmd, store.Stack(), args[0])
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
request := shared.PolicyRequest{}
|
|
if err := json.Unmarshal([]byte(script), &request); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
//nolint:gosimple
|
|
response, err := store.Client().Reconciliation.V1.CreatePolicy(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.PolicyID = response.PolicyResponse.Data.ID
|
|
|
|
return c, nil
|
|
}
|
|
|
|
func (c *CreateController) Render(cmd *cobra.Command, args []string) error {
|
|
pterm.Success.WithWriter(cmd.OutOrStdout()).Printfln("Policy created with ID: %s", c.store.PolicyID)
|
|
|
|
return nil
|
|
}
|