Files

99 lines
2.3 KiB
Go

package pools
import (
"fmt"
"strings"
"github.com/formancehq/fctl/cmd/payments/versions"
fctl "github.com/formancehq/fctl/pkg"
"github.com/formancehq/formance-sdk-go/v2/pkg/models/operations"
"github.com/formancehq/formance-sdk-go/v2/pkg/models/shared"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
)
type ShowStore struct {
Pool *shared.Pool `json:"pool"`
}
type ShowController struct {
PaymentsVersion versions.Version
store *ShowStore
}
func (c *ShowController) SetVersion(version versions.Version) {
c.PaymentsVersion = version
}
var _ fctl.Controller[*ShowStore] = (*ShowController)(nil)
func NewShowStore() *ShowStore {
return &ShowStore{}
}
func NewShowController() *ShowController {
return &ShowController{
store: NewShowStore(),
}
}
func NewShowCommand() *cobra.Command {
c := NewShowController()
return fctl.NewCommand("get <poolOD>",
fctl.WithShortDescription("Get pool"),
fctl.WithArgs(cobra.ExactArgs(1)),
fctl.WithAliases("sh", "s"),
fctl.WithController[*ShowStore](c),
)
}
func (c *ShowController) GetStore() *ShowStore {
return c.store
}
func (c *ShowController) Run(cmd *cobra.Command, args []string) (fctl.Renderable, error) {
store := fctl.GetStackStore(cmd.Context())
if err := versions.GetPaymentsVersion(cmd, args, c); err != nil {
return nil, err
}
if c.PaymentsVersion < versions.V1 {
return nil, fmt.Errorf("pools are only supported in >= v1.0.0")
}
response, err := store.Client().Payments.V1.GetPool(cmd.Context(), operations.GetPoolRequest{
PoolID: args[0],
})
if err != nil {
return nil, err
}
if response.StatusCode >= 300 {
return nil, fmt.Errorf("unexpected status code: %d", response.StatusCode)
}
c.store.Pool = &response.PoolResponse.Data
return c, nil
}
func (c *ShowController) Render(cmd *cobra.Command, args []string) error {
fctl.Section.WithWriter(cmd.OutOrStdout()).Println("Information")
tableData := pterm.TableData{}
tableData = append(tableData, []string{pterm.LightCyan("ID"), c.store.Pool.ID})
tableData = append(tableData, []string{pterm.LightCyan("Name"), c.store.Pool.Name})
tableData = append(tableData, []string{pterm.LightCyan("Accounts"), func() string {
return strings.Join(c.store.Pool.Accounts, ", ")
}()})
if err := pterm.DefaultTable.
WithWriter(cmd.OutOrStdout()).
WithData(tableData).
Render(); err != nil {
return err
}
return nil
}