mirror of
https://github.com/hanzo-fi/stack.git
synced 2026-10-30 20:34:43 +00:00
118 lines
2.9 KiB
Go
118 lines
2.9 KiB
Go
package accounts
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/formancehq/stack/libs/go-libs/collectionutils"
|
|
|
|
internal "github.com/formancehq/fctl/cmd/ledger/internal"
|
|
fctl "github.com/formancehq/fctl/pkg"
|
|
"github.com/formancehq/formance-sdk-go/pkg/models/operations"
|
|
"github.com/formancehq/formance-sdk-go/pkg/models/shared"
|
|
"github.com/pterm/pterm"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
type ShowStore struct {
|
|
Account *shared.AccountWithVolumesAndBalances `json:"account"`
|
|
}
|
|
type ShowController struct {
|
|
store *ShowStore
|
|
}
|
|
|
|
var _ fctl.Controller[*ShowStore] = (*ShowController)(nil)
|
|
|
|
func NewDefaultShowStore() *ShowStore {
|
|
return &ShowStore{}
|
|
}
|
|
|
|
func NewShowController() *ShowController {
|
|
return &ShowController{
|
|
store: NewDefaultShowStore(),
|
|
}
|
|
}
|
|
|
|
func NewShowCommand() *cobra.Command {
|
|
return fctl.NewCommand("show <address>",
|
|
fctl.WithShortDescription("Show account"),
|
|
fctl.WithArgs(cobra.ExactArgs(1)),
|
|
fctl.WithAliases("sh", "s"),
|
|
fctl.WithController[*ShowStore](NewShowController()),
|
|
)
|
|
}
|
|
|
|
func (c *ShowController) GetStore() *ShowStore {
|
|
return c.store
|
|
}
|
|
|
|
func (c *ShowController) Run(cmd *cobra.Command, args []string) (fctl.Renderable, error) {
|
|
|
|
cfg, err := fctl.GetConfig(cmd)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
organizationID, err := fctl.ResolveOrganizationID(cmd, cfg)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
stack, err := fctl.ResolveStack(cmd, cfg, organizationID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
ledgerClient, err := fctl.NewStackClient(cmd, cfg, stack)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
ledger := fctl.GetString(cmd, internal.LedgerFlag)
|
|
response, err := ledgerClient.Ledger.GetAccount(cmd.Context(), operations.GetAccountRequest{
|
|
Address: args[0],
|
|
Ledger: ledger,
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if response.ErrorResponse != nil {
|
|
return nil, fmt.Errorf("%s: %s", response.ErrorResponse.ErrorCode, response.ErrorResponse.ErrorMessage)
|
|
}
|
|
|
|
if response.StatusCode >= 300 {
|
|
return nil, fmt.Errorf("unexpected status code: %d", response.StatusCode)
|
|
}
|
|
|
|
c.store.Account = &response.AccountResponse.Data
|
|
|
|
return c, nil
|
|
}
|
|
|
|
func (c *ShowController) Render(cmd *cobra.Command, args []string) error {
|
|
fctl.Section.WithWriter(cmd.OutOrStdout()).Println("Information")
|
|
if c.store.Account.Volumes != nil && len(c.store.Account.Volumes) > 0 {
|
|
tableData := pterm.TableData{}
|
|
tableData = append(tableData, []string{"Asset", "Input", "Output"})
|
|
for asset, volumes := range c.store.Account.Volumes {
|
|
input := volumes.Input
|
|
output := volumes.Output
|
|
tableData = append(tableData, []string{pterm.LightCyan(asset), fmt.Sprint(input), fmt.Sprint(output)})
|
|
}
|
|
if err := pterm.DefaultTable.
|
|
WithHasHeader(true).
|
|
WithWriter(cmd.OutOrStdout()).
|
|
WithData(tableData).
|
|
Render(); err != nil {
|
|
return err
|
|
}
|
|
} else {
|
|
fctl.Println("No balances.")
|
|
}
|
|
|
|
fmt.Fprintln(cmd.OutOrStdout())
|
|
|
|
return fctl.PrintMetadata(cmd.OutOrStdout(),
|
|
collectionutils.ConvertMap(c.store.Account.Metadata, collectionutils.ToFmtString[string]))
|
|
}
|