Files

69 lines
1.7 KiB
Go

package transactions
import (
"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/spf13/cobra"
)
type ShowStore struct {
Transaction shared.Transaction `json:"transaction"`
}
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 <transaction-id>",
fctl.WithShortDescription("Print a transaction"),
fctl.WithArgs(cobra.ExactArgs(1)),
fctl.WithAliases("sh"),
fctl.WithValidArgs("last"),
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)
txId, err := internal.TransactionIDOrLastN(cmd.Context(), store.Client(), ledger, args[0])
if err != nil {
return nil, err
}
response, err := store.Client().Ledger.V1.GetTransaction(cmd.Context(), operations.GetTransactionRequest{
Ledger: ledger,
Txid: txId,
})
if err != nil {
return nil, err
}
c.store.Transaction = response.TransactionResponse.Data
return c, nil
}
func (c *ShowController) Render(cmd *cobra.Command, args []string) error {
return internal.PrintExpandedTransaction(cmd.OutOrStdout(), internal.WrapV1Transaction(c.store.Transaction))
}