Skip to content

Commit 45f6b9c

Browse files
committed
feat(ledger): fctl ledger logs list
1 parent e38d0a6 commit 45f6b9c

File tree

3 files changed

+160
-0
lines changed

3 files changed

+160
-0
lines changed

cmd/ledger/logs/list.go

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
package logs
2+
3+
import (
4+
"fmt"
5+
"strconv"
6+
"time"
7+
8+
"github.com/formancehq/fctl/cmd/ledger/internal"
9+
fctl "github.com/formancehq/fctl/pkg"
10+
"github.com/formancehq/formance-sdk-go/v3/pkg/models/operations"
11+
"github.com/formancehq/formance-sdk-go/v3/pkg/models/shared"
12+
"github.com/formancehq/go-libs/pointer"
13+
"github.com/pterm/pterm"
14+
"github.com/spf13/cobra"
15+
)
16+
17+
var timeRFC = time.RFC3339Nano
18+
var (
19+
pageSizeFlag = "page-size"
20+
afterFlag = "after"
21+
startTimeFlag = "start-time"
22+
endTimeFLag = "end-time"
23+
cursorFlag = "cursor"
24+
)
25+
26+
type ListStore struct {
27+
Cursor shared.LogsCursorResponseCursor `json:"cursor"`
28+
}
29+
30+
type ListController struct {
31+
store *ListStore
32+
}
33+
34+
var _ fctl.Controller[*ListStore] = (*ListController)(nil)
35+
36+
func NewDefaultListStore() *ListStore {
37+
return &ListStore{}
38+
}
39+
40+
func NewListController() *ListController {
41+
return &ListController{
42+
store: NewDefaultListStore(),
43+
}
44+
}
45+
46+
func NewListCommand() *cobra.Command {
47+
c := NewListController()
48+
return fctl.NewCommand("list",
49+
fctl.WithAliases("ls", "l"),
50+
fctl.WithShortDescription("List logs"),
51+
fctl.WithArgs(cobra.ExactArgs(0)),
52+
fctl.WithIntFlag(pageSizeFlag, 15, "Page size"),
53+
fctl.WithStringFlag(cursorFlag, "", "Logs Cursor"),
54+
fctl.WithStringFlag(afterFlag, "", "Pagination cursor, will return the logs after a given ID. (in descending order)."),
55+
fctl.WithStringFlag(startTimeFlag, "", fmt.Sprintf("Start time (time.RFC %s)", timeRFC)),
56+
fctl.WithStringFlag(endTimeFLag, "", fmt.Sprintf("End time (time.RFC %s)", timeRFC)),
57+
fctl.WithController(c),
58+
)
59+
}
60+
61+
func (c *ListController) GetStore() *ListStore {
62+
return c.store
63+
}
64+
65+
func (c *ListController) Run(cmd *cobra.Command, args []string) (fctl.Renderable, error) {
66+
67+
store := fctl.GetStackStore(cmd.Context())
68+
request := operations.ListLogsRequest{
69+
Ledger: fctl.GetString(cmd, internal.LedgerFlag),
70+
PageSize: pointer.For(int64(fctl.GetInt(cmd, pageSizeFlag))),
71+
}
72+
73+
if after := fctl.GetString(cmd, afterFlag); after != "" {
74+
request.After = &after
75+
}
76+
77+
if startTime := fctl.GetString(cmd, startTimeFlag); startTime != "" {
78+
str, err := time.Parse(timeRFC, startTime)
79+
if err != nil {
80+
return nil, fmt.Errorf(`invalid start time parsing with %s: %w`, timeRFC, err)
81+
}
82+
request.StartTime = &str
83+
}
84+
85+
if endTime := fctl.GetString(cmd, endTimeFLag); endTime != "" {
86+
str, err := time.Parse(timeRFC, endTime)
87+
if err != nil {
88+
return nil, fmt.Errorf(`invalid end time parsing with %s: %w`, timeRFC, err)
89+
}
90+
request.EndTime = &str
91+
}
92+
93+
rsp, err := store.Client().Ledger.V1.ListLogs(cmd.Context(), request)
94+
if err != nil {
95+
return nil, err
96+
}
97+
98+
c.store.Cursor = rsp.LogsCursorResponse.Cursor
99+
100+
return c, nil
101+
}
102+
103+
func (c *ListController) Render(cmd *cobra.Command, args []string) error {
104+
fmt.Println("")
105+
tableData := pterm.TableData{}
106+
tableData = append(tableData, []string{pterm.LightCyan("HasMore"), fmt.Sprintf("%v", c.store.Cursor.HasMore)})
107+
tableData = append(tableData, []string{pterm.LightCyan("PageSize"), fmt.Sprintf("%d", c.store.Cursor.PageSize)})
108+
tableData = append(tableData, []string{pterm.LightCyan("Next"), func() string {
109+
if c.store.Cursor.Next == nil {
110+
return ""
111+
}
112+
return *c.store.Cursor.Next
113+
}()})
114+
tableData = append(tableData, []string{pterm.LightCyan("Previous"), func() string {
115+
if c.store.Cursor.Previous == nil {
116+
return ""
117+
}
118+
return *c.store.Cursor.Previous
119+
}()})
120+
121+
if err := pterm.DefaultTable.
122+
WithWriter(cmd.OutOrStdout()).
123+
WithData(tableData).
124+
Render(); err != nil {
125+
return err
126+
}
127+
128+
tableData = fctl.Map(c.store.Cursor.Data, func(log shared.Log) []string {
129+
return []string{
130+
log.Date.Format(timeRFC),
131+
strconv.FormatInt(log.ID, 10),
132+
string(log.Type),
133+
log.Hash,
134+
}
135+
})
136+
tableData = fctl.Prepend(tableData, []string{"Date", "ID", "Type", "Hash"})
137+
return pterm.DefaultTable.
138+
WithHasHeader().
139+
WithWriter(cmd.OutOrStdout()).
140+
WithData(tableData).
141+
Render()
142+
}

cmd/ledger/logs/root.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package logs
2+
3+
import (
4+
fctl "github.com/formancehq/fctl/pkg"
5+
"github.com/spf13/cobra"
6+
)
7+
8+
func NewLogsCommand() *cobra.Command {
9+
return fctl.NewCommand("logs",
10+
fctl.WithAliases("log", "l"),
11+
fctl.WithShortDescription("Logs management"),
12+
fctl.WithChildCommands(
13+
NewListCommand(),
14+
),
15+
)
16+
}

cmd/ledger/root.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package ledger
33
import (
44
"github.com/formancehq/fctl/cmd/ledger/accounts"
55
"github.com/formancehq/fctl/cmd/ledger/internal"
6+
"github.com/formancehq/fctl/cmd/ledger/logs"
67
"github.com/formancehq/fctl/cmd/ledger/transactions"
78
"github.com/formancehq/fctl/cmd/ledger/volumes"
89
fctl "github.com/formancehq/fctl/pkg"
@@ -24,6 +25,7 @@ func NewCommand() *cobra.Command {
2425
NewDeleteMetadataCommand(),
2526
NewExportCommand(),
2627
NewImportCommand(),
28+
logs.NewLogsCommand(),
2729
transactions.NewLedgerTransactionsCommand(),
2830
accounts.NewLedgerAccountsCommand(),
2931
volumes.NewLedgerVolumesCommand(),

0 commit comments

Comments
 (0)