-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
show.go
91 lines (74 loc) · 2.36 KB
/
show.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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/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 {
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) {
store := fctl.GetStackStore(cmd.Context())
ledger := fctl.GetString(cmd, internal.LedgerFlag)
response, err := store.Client().Ledger.GetAccount(cmd.Context(), operations.GetAccountRequest{
Address: args[0],
Ledger: ledger,
})
if err != nil {
return nil, err
}
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))
}