Files
stack/components/fctl/cmd/ledger/accounts/set_metadata.go

82 lines
2.1 KiB
Go

package accounts
import (
"github.com/formancehq/stack/libs/go-libs/collectionutils"
"github.com/formancehq/fctl/cmd/ledger/internal"
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 SetMetadataStore struct {
Success bool `json:"success"`
}
type SetMetadataController struct {
store *SetMetadataStore
}
var _ fctl.Controller[*SetMetadataStore] = (*SetMetadataController)(nil)
func NewDefaultSetMetadataStore() *SetMetadataStore {
return &SetMetadataStore{
Success: false,
}
}
func NewSetMetadataController() *SetMetadataController {
return &SetMetadataController{
store: NewDefaultSetMetadataStore(),
}
}
func NewSetMetadataCommand() *cobra.Command {
return fctl.NewCommand("set-metadata <address> [<key>=<value>...]",
fctl.WithConfirmFlag(),
fctl.WithShortDescription("Set metadata on address"),
fctl.WithAliases("sm", "set-meta"),
fctl.WithArgs(cobra.MinimumNArgs(2)),
fctl.WithController[*SetMetadataStore](NewSetMetadataController()),
)
}
func (c *SetMetadataController) GetStore() *SetMetadataStore {
return c.store
}
func (c *SetMetadataController) Run(cmd *cobra.Command, args []string) (fctl.Renderable, error) {
store := fctl.GetStackStore(cmd.Context())
metadata, err := fctl.ParseMetadata(args[1:])
if err != nil {
return nil, err
}
address := args[0]
if !fctl.CheckStackApprobation(cmd, store.Stack(), "You are about to set a metadata on address '%s'", address) {
return nil, fctl.ErrMissingApproval
}
request := operations.AddMetadataToAccountRequest{
Ledger: fctl.GetString(cmd, internal.LedgerFlag),
Address: address,
RequestBody: collectionutils.ConvertMap(metadata, collectionutils.ToAny[string]),
}
response, err := store.Client().Ledger.AddMetadataToAccount(cmd.Context(), request)
if err != nil {
return nil, err
}
c.store.Success = response.StatusCode == 204
return c, nil
}
func (c *SetMetadataController) Render(cmd *cobra.Command, args []string) error {
pterm.Success.WithWriter(cmd.OutOrStdout()).Printfln("Metadata added!")
return nil
}