Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cmd/payments/connectors/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/formancehq/fctl/v3/cmd/payments/connectors/configs"
"github.com/formancehq/fctl/v3/cmd/payments/connectors/install"
"github.com/formancehq/fctl/v3/cmd/payments/connectors/schedules"
fctl "github.com/formancehq/fctl/v3/pkg"
)

Expand All @@ -19,6 +20,7 @@ func NewConnectorsCommand() *cobra.Command {
configs.NewUpdateConfigCommand(),
configs.NewGetConfigCommand(),
NewConnectorListAvailableCommand(),
schedules.NewSchedulesCommand(),
),
)
}
158 changes: 158 additions & 0 deletions cmd/payments/connectors/schedules/instances/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
package instances

import (
"fmt"
"time"

"github.com/pterm/pterm"
"github.com/spf13/cobra"

"github.com/formancehq/formance-sdk-go/v3/pkg/models/operations"
"github.com/formancehq/formance-sdk-go/v3/pkg/models/shared"

"github.com/formancehq/fctl/v3/cmd/payments/versions"
fctl "github.com/formancehq/fctl/v3/pkg"
)

type ListStore struct {
Cursor *shared.V3ConnectorScheduleInstancesCursorResponseCursor `json:"cursor"`
}

type ListController struct {
PaymentsVersion versions.Version

store *ListStore
}

func (c *ListController) SetVersion(version versions.Version) {
c.PaymentsVersion = version
}

var _ fctl.Controller[*ListStore] = (*ListController)(nil)

func NewListStore() *ListStore {
return &ListStore{
Cursor: &shared.V3ConnectorScheduleInstancesCursorResponseCursor{},
}
}

func NewListController() *ListController {
return &ListController{
store: NewListStore(),
}
}

func NewListCommand() *cobra.Command {
c := NewListController()
return fctl.NewCommand("list <connectorID> <scheduleID>",
fctl.WithAliases("ls", "l"),
fctl.WithArgs(cobra.ExactArgs(2)),
fctl.WithValidArgsFunction(cobra.NoFileCompletions),
fctl.WithShortDescription("List connector schedule instances"),
fctl.WithCursorFlag(),
fctl.WithPageSizeFlag(),
fctl.WithController[*ListStore](c),
)
}

func (c *ListController) GetStore() *ListStore {
return c.store
}

func (c *ListController) Run(cmd *cobra.Command, args []string) (fctl.Renderable, error) {

_, profile, profileName, relyingParty, err := fctl.LoadAndAuthenticateCurrentProfile(cmd)
if err != nil {
return nil, err
}

stackClient, err := fctl.NewStackClientFromFlags(cmd, relyingParty, fctl.NewPTermDialog(), profileName, *profile)
if err != nil {
return nil, err
}

if err := versions.GetPaymentsVersion(cmd, args, c); err != nil {
return nil, err
}

if c.PaymentsVersion.Major < versions.V3 {
return nil, fmt.Errorf("connector schedules are only supported in >= v3.0.0")
}

connectorID, scheduleID := args[0], args[1]
cursor, err := fctl.GetCursor(cmd)
if err != nil {
return nil, err
}
pageSize, err := fctl.GetPageSize(cmd)
if err != nil {
return nil, err
}

request := operations.V3ListConnectorScheduleInstancesRequest{
ConnectorID: connectorID,
ScheduleID: scheduleID,
PageSize: fctl.Ptr(int64(pageSize)),
}
if cursor != "" {
request.Cursor = &cursor
}

response, err := stackClient.Payments.V3.ListConnectorScheduleInstances(cmd.Context(), request)
if err != nil {
return nil, err
}

if response.StatusCode >= 300 {
return nil, fmt.Errorf("unexpected status code: %d", response.StatusCode)
}

if response.V3ConnectorScheduleInstancesCursorResponse == nil {
return nil, fmt.Errorf("unexpected response: %v", response)
}

c.store.Cursor = &response.V3ConnectorScheduleInstancesCursorResponse.Cursor

return c, nil
}

func (c *ListController) Render(cmd *cobra.Command, args []string) error {
tableData := fctl.Map(c.store.Cursor.Data, func(i shared.V3Instance) []string {
errStr := ""
if i.Error != nil {
errStr = *i.Error
}
terminatedAt := ""
if i.TerminatedAt != nil {
terminatedAt = i.TerminatedAt.Format(time.RFC3339)
}
return []string{
i.ID,
i.ScheduleID,
i.ConnectorID,
Comment on lines +131 to +132
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same deal here with the output being too wide for it to be practical. Let's remove both scheduleID and connector ID columns because they are already specified as command line arguments

fmt.Sprintf("%t", i.Terminated),
i.CreatedAt.Format(time.RFC3339),
i.UpdatedAt.Format(time.RFC3339),
terminatedAt,
errStr,
}
})
tableData = fctl.Prepend(tableData, []string{
"ID", "ScheduleID", "ConnectorID", "Terminated",
"CreatedAt", "UpdatedAt", "TerminatedAt", "Error",
})
if err := pterm.DefaultTable.
WithHasHeader().
WithWriter(cmd.OutOrStdout()).
WithData(tableData).
Render(); err != nil {
return err
}

return fctl.RenderCursor(cmd.OutOrStdout(), fctl.Cursor{
HasMore: c.store.Cursor.HasMore,
PageSize: c.store.Cursor.PageSize,
Next: c.store.Cursor.Next,
Previous: c.store.Cursor.Previous,
})
}
17 changes: 17 additions & 0 deletions cmd/payments/connectors/schedules/instances/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package instances

import (
"github.com/spf13/cobra"

fctl "github.com/formancehq/fctl/v3/pkg"
)

func NewInstancesCommand() *cobra.Command {
return fctl.NewCommand("instances",
fctl.WithAliases("inst"),
fctl.WithShortDescription("Manage connector schedule instances"),
fctl.WithChildCommands(
NewListCommand(),
),
)
}
141 changes: 141 additions & 0 deletions cmd/payments/connectors/schedules/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
package schedules

import (
"fmt"
"time"

"github.com/pterm/pterm"
"github.com/spf13/cobra"

"github.com/formancehq/formance-sdk-go/v3/pkg/models/operations"
"github.com/formancehq/formance-sdk-go/v3/pkg/models/shared"

"github.com/formancehq/fctl/v3/cmd/payments/versions"
fctl "github.com/formancehq/fctl/v3/pkg"
)

type ListStore struct {
Cursor *shared.V3ConnectorSchedulesCursorResponseCursor `json:"cursor"`
}

type ListController struct {
PaymentsVersion versions.Version

store *ListStore
}

func (c *ListController) SetVersion(version versions.Version) {
c.PaymentsVersion = version
}

var _ fctl.Controller[*ListStore] = (*ListController)(nil)

func NewListStore() *ListStore {
return &ListStore{
Cursor: &shared.V3ConnectorSchedulesCursorResponseCursor{},
}
}

func NewListController() *ListController {
return &ListController{
store: NewListStore(),
}
}

func NewListCommand() *cobra.Command {
c := NewListController()
return fctl.NewCommand("list <connectorID>",
fctl.WithAliases("ls", "l"),
fctl.WithArgs(cobra.ExactArgs(1)),
fctl.WithValidArgsFunction(cobra.NoFileCompletions),
fctl.WithShortDescription("List connector schedules"),
fctl.WithCursorFlag(),
fctl.WithPageSizeFlag(),
fctl.WithController[*ListStore](c),
)
}

func (c *ListController) GetStore() *ListStore {
return c.store
}

func (c *ListController) Run(cmd *cobra.Command, args []string) (fctl.Renderable, error) {

_, profile, profileName, relyingParty, err := fctl.LoadAndAuthenticateCurrentProfile(cmd)
if err != nil {
return nil, err
}

stackClient, err := fctl.NewStackClientFromFlags(cmd, relyingParty, fctl.NewPTermDialog(), profileName, *profile)
if err != nil {
return nil, err
}

if err := versions.GetPaymentsVersion(cmd, args, c); err != nil {
return nil, err
}

if c.PaymentsVersion.Major < versions.V3 {
return nil, fmt.Errorf("connector schedules are only supported in >= v3.0.0")
}

connectorID := args[0]
cursor, err := fctl.GetCursor(cmd)
if err != nil {
return nil, err
}
pageSize, err := fctl.GetPageSize(cmd)
if err != nil {
return nil, err
}

request := operations.V3ListConnectorSchedulesRequest{
ConnectorID: connectorID,
PageSize: fctl.Ptr(int64(pageSize)),
}
if cursor != "" {
request.Cursor = &cursor
}

response, err := stackClient.Payments.V3.ListConnectorSchedules(cmd.Context(), request)
if err != nil {
return nil, err
}

if response.StatusCode >= 300 {
return nil, fmt.Errorf("unexpected status code: %d", response.StatusCode)
}

if response.V3ConnectorSchedulesCursorResponse == nil {
return nil, fmt.Errorf("unexpected response: %v", response)
}

c.store.Cursor = &response.V3ConnectorSchedulesCursorResponse.Cursor

return c, nil
}

func (c *ListController) Render(cmd *cobra.Command, args []string) error {
tableData := fctl.Map(c.store.Cursor.Data, func(s shared.V3Schedule) []string {
return []string{
s.ID,
s.ConnectorID,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The output is incredibly wide making this command not super useful. Since connectorID is already a required param in this command, let's not even include ConnectorID as a column to make the whole thing more legible.

s.CreatedAt.Format(time.RFC3339),
}
})
tableData = fctl.Prepend(tableData, []string{"ID", "ConnectorID", "CreatedAt"})
if err := pterm.DefaultTable.
WithHasHeader().
WithWriter(cmd.OutOrStdout()).
WithData(tableData).
Render(); err != nil {
return err
}

return fctl.RenderCursor(cmd.OutOrStdout(), fctl.Cursor{
HasMore: c.store.Cursor.HasMore,
PageSize: c.store.Cursor.PageSize,
Next: c.store.Cursor.Next,
Previous: c.store.Cursor.Previous,
})
}
20 changes: 20 additions & 0 deletions cmd/payments/connectors/schedules/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package schedules

import (
"github.com/spf13/cobra"

"github.com/formancehq/fctl/v3/cmd/payments/connectors/schedules/instances"
fctl "github.com/formancehq/fctl/v3/pkg"
)

func NewSchedulesCommand() *cobra.Command {
return fctl.NewCommand("schedules",
fctl.WithAliases("sch"),
fctl.WithShortDescription("Manage connector schedules"),
fctl.WithChildCommands(
NewListCommand(),
NewShowCommand(),
instances.NewInstancesCommand(),
),
)
}
Loading
Loading