-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
describe.go
295 lines (266 loc) · 11.1 KB
/
describe.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
package instances
import (
"fmt"
"io"
"time"
fctl "github.com/formancehq/fctl/pkg"
formance "github.com/formancehq/formance-sdk-go/v2"
"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 InstancesDescribeStore struct {
WorkflowInstancesHistory []shared.WorkflowInstanceHistory `json:"workflowInstanceHistory"`
}
type InstancesDescribeController struct {
store *InstancesDescribeStore
}
var _ fctl.Controller[*InstancesDescribeStore] = (*InstancesDescribeController)(nil)
func NewDefaultInstancesDescribeStore() *InstancesDescribeStore {
return &InstancesDescribeStore{}
}
func NewInstancesDescribeController() *InstancesDescribeController {
return &InstancesDescribeController{
store: NewDefaultInstancesDescribeStore(),
}
}
func NewDescribeCommand() *cobra.Command {
c := NewInstancesDescribeController()
return fctl.NewCommand("describe <instance-id>",
fctl.WithShortDescription("Describe a specific workflow instance"),
fctl.WithArgs(cobra.ExactArgs(1)),
fctl.WithController[*InstancesDescribeStore](c),
)
}
func (c *InstancesDescribeController) GetStore() *InstancesDescribeStore {
return c.store
}
func (c *InstancesDescribeController) Run(cmd *cobra.Command, args []string) (fctl.Renderable, error) {
store := fctl.GetStackStore(cmd.Context())
response, err := store.Client().Orchestration.GetInstanceHistory(cmd.Context(), operations.GetInstanceHistoryRequest{
InstanceID: args[0],
})
if err != nil {
return nil, err
}
c.store.WorkflowInstancesHistory = response.GetWorkflowInstanceHistoryResponse.Data
return c, nil
}
func (c *InstancesDescribeController) Render(cmd *cobra.Command, args []string) error {
store := fctl.GetStackStore(cmd.Context())
for i, history := range c.store.WorkflowInstancesHistory {
if err := printStage(cmd, i, store.Client(), args[0], history); err != nil {
return err
}
}
return nil
}
func printHistoryBaseInfo(out io.Writer, name string, ind int, history shared.WorkflowInstanceHistory) {
fctl.Section.WithWriter(out).Printf("Stage %d : %s\n", ind, name)
fctl.BasicText.WithWriter(out).Printfln("Started at: %s", history.StartedAt.Format(time.RFC3339))
if history.Terminated {
fctl.BasicText.WithWriter(out).Printfln("Terminated at: %s", history.StartedAt.Format(time.RFC3339))
}
}
func stageSourceName(src *shared.StageSendSource) string {
switch {
case src.Wallet != nil:
return fmt.Sprintf("wallet '%s' (balance: %s)", src.Wallet.ID, *src.Wallet.Balance)
case src.Account != nil:
return fmt.Sprintf("account '%s' (ledger: %s)", src.Account.ID, *src.Account.Ledger)
case src.Payment != nil:
return fmt.Sprintf("payment '%s'", src.Payment.ID)
default:
return "unknown_source_type"
}
}
func stageDestinationName(dst *shared.StageSendDestination) string {
switch {
case dst.Wallet != nil:
return fmt.Sprintf("wallet '%s' (balance: %s)", dst.Wallet.ID, *dst.Wallet.Balance)
case dst.Account != nil:
return fmt.Sprintf("account '%s' (ledger: %s)", dst.Account.ID, *dst.Account.Ledger)
case dst.Payment != nil:
return dst.Payment.Psp
default:
return "unknown_source_type"
}
}
func subjectName(src shared.Subject) string {
switch {
case src.WalletSubject != nil:
return fmt.Sprintf("wallet %s (balance: %s)", src.WalletSubject.Identifier, *src.WalletSubject.Balance)
case src.LedgerAccountSubject != nil:
return fmt.Sprintf("account %s", src.LedgerAccountSubject.Identifier)
default:
return "unknown_subject_type"
}
}
func printMetadata(metadata map[string]string) []pterm.BulletListItem {
ret := make([]pterm.BulletListItem, 0)
ret = append(ret, historyItemDetails("Added metadata:"))
for k, v := range metadata {
ret = append(ret, pterm.BulletListItem{
Level: 2,
Text: fmt.Sprintf("%s: %s", k, v),
})
}
return ret
}
func printStage(cmd *cobra.Command, i int, client *formance.Formance, id string, history shared.WorkflowInstanceHistory) error {
cyanWriter := fctl.BasicTextCyan
defaultWriter := fctl.BasicText
listItems := make([]pterm.BulletListItem, 0)
switch history.Input.Type {
case shared.StageTypeStageSend:
printHistoryBaseInfo(cmd.OutOrStdout(), "send", i, history)
cyanWriter.Printfln("Send %v %s from %s to %s", history.Input.StageSend.Amount.Amount,
history.Input.StageSend.Amount.Asset, stageSourceName(history.Input.StageSend.Source),
stageDestinationName(history.Input.StageSend.Destination))
fctl.Println()
stageResponse, err := client.Orchestration.GetInstanceStageHistory(cmd.Context(), operations.GetInstanceStageHistoryRequest{
InstanceID: id,
Number: int64(i),
})
if err != nil {
return err
}
for _, historyStage := range stageResponse.GetWorkflowInstanceHistoryStageResponse.Data {
switch {
case historyStage.Input.StripeTransfer != nil:
listItems = append(listItems, historyItemTitle("Send %v %s to Stripe connected account: %s",
*historyStage.Input.StripeTransfer.Amount,
*historyStage.Input.StripeTransfer.Asset,
*historyStage.Input.StripeTransfer.Destination,
))
case historyStage.Input.CreateTransaction != nil:
listItems = append(listItems, historyItemTitle("Send %v %s from account %s to account %s (ledger %s)",
historyStage.Input.CreateTransaction.Data.Postings[0].Amount,
historyStage.Input.CreateTransaction.Data.Postings[0].Asset,
historyStage.Input.CreateTransaction.Data.Postings[0].Source,
historyStage.Input.CreateTransaction.Data.Postings[0].Destination,
*historyStage.Input.CreateTransaction.Ledger,
))
if historyStage.Error == nil && historyStage.LastFailure == nil && historyStage.Terminated {
listItems = append(listItems, historyItemDetails("Created transaction: %d", historyStage.Output.CreateTransaction.Data.ID))
if historyStage.Input.CreateTransaction.Data.Reference != nil {
listItems = append(listItems, historyItemDetails("Reference: %s", *historyStage.Output.CreateTransaction.Data.Reference))
}
if len(historyStage.Input.CreateTransaction.Data.Metadata) > 0 {
listItems = append(listItems, printMetadata(historyStage.Input.CreateTransaction.Data.Metadata)...)
}
}
case historyStage.Input.ConfirmHold != nil:
listItems = append(listItems, historyItemTitle("Confirm debit hold %s", historyStage.Input.ConfirmHold.ID))
case historyStage.Input.CreditWallet != nil:
listItems = append(listItems, historyItemTitle("Credit wallet %s (balance: %s) of %v %s from %s",
*historyStage.Input.CreditWallet.ID,
*historyStage.Input.CreditWallet.Data.Balance,
historyStage.Input.CreditWallet.Data.Amount.Amount,
historyStage.Input.CreditWallet.Data.Amount.Asset,
subjectName(historyStage.Input.CreditWallet.Data.Sources[0]),
))
if historyStage.Error == nil && historyStage.LastFailure == nil && historyStage.Terminated {
if len(historyStage.Input.CreditWallet.Data.Metadata) > 0 {
listItems = append(listItems, printMetadata(historyStage.Input.CreditWallet.Data.Metadata)...)
}
}
case historyStage.Input.DebitWallet != nil:
destination := "@world"
if historyStage.Input.DebitWallet.Data.Destination != nil {
destination = subjectName(*historyStage.Input.DebitWallet.Data.Destination)
}
listItems = append(listItems, historyItemTitle("Debit wallet %s (balance: %s) of %v %s to %s",
*historyStage.Input.DebitWallet.ID,
historyStage.Input.DebitWallet.Data.Balances[0],
historyStage.Input.DebitWallet.Data.Amount.Amount,
historyStage.Input.DebitWallet.Data.Amount.Asset,
destination,
))
if historyStage.Error == nil && historyStage.LastFailure == nil && historyStage.Terminated {
if len(historyStage.Input.DebitWallet.Data.Metadata) > 0 {
listItems = append(listItems, printMetadata(historyStage.Input.DebitWallet.Data.Metadata)...)
}
}
case historyStage.Input.GetAccount != nil:
listItems = append(listItems, historyItemTitle("Read account %s of ledger %s",
historyStage.Input.GetAccount.ID,
historyStage.Input.GetAccount.Ledger,
))
case historyStage.Input.GetPayment != nil:
listItems = append(listItems, historyItemTitle("Read payment %s",
historyStage.Input.GetPayment.ID))
case historyStage.Input.GetWallet != nil:
listItems = append(listItems, historyItemTitle("Read wallet '%s'", historyStage.Input.GetWallet.ID))
case historyStage.Input.RevertTransaction != nil:
listItems = append(listItems, historyItemTitle("Revert transaction %s", historyStage.Input.RevertTransaction.ID))
if historyStage.Error == nil {
listItems = append(listItems, historyItemTitle("Created transaction: %d", historyStage.Output.RevertTransaction.Data.ID))
}
case historyStage.Input.VoidHold != nil:
listItems = append(listItems, historyItemTitle("Cancel debit hold %s", historyStage.Input.VoidHold.ID))
case historyStage.Input.ListWallets != nil:
listItems = append(listItems, historyItemTitle("List wallets"))
}
if historyStage.LastFailure != nil {
listItems = append(listItems, historyItemError(*historyStage.LastFailure))
if historyStage.NextExecution != nil {
listItems = append(listItems, historyItemError("Next try: %s", historyStage.NextExecution.Format(time.RFC3339)))
listItems = append(listItems, historyItemError("Attempt: %d", historyStage.Attempt))
}
}
if historyStage.Error != nil {
listItems = append(listItems, historyItemError(*historyStage.Error))
}
}
case shared.StageTypeStageDelay:
printHistoryBaseInfo(cmd.OutOrStdout(), "delay", i, history)
switch {
case history.Input.StageDelay.Duration != nil:
listItems = append(listItems, historyItemTitle("Pause workflow for a delay of %s", *history.Input.StageDelay.Duration))
case history.Input.StageDelay.Until != nil:
listItems = append(listItems, historyItemTitle("Pause workflow until %s", *history.Input.StageDelay.Until))
}
case shared.StageTypeStageWaitEvent:
printHistoryBaseInfo(cmd.OutOrStdout(), "wait_event", i, history)
listItems = append(listItems, historyItemTitle("Waiting event '%s'", history.Input.StageWaitEvent.Event))
if history.Error == nil {
if history.Terminated {
listItems = append(listItems, historyItemDetails("Event received!"))
} else {
listItems = append(listItems, historyItemDetails("Still waiting event..."))
}
}
default:
// Display error?
}
if history.Error != nil {
fctl.BasicTextRed.WithWriter(cmd.OutOrStdout()).Printfln("Stage terminated with error: %s", *history.Error)
}
if len(listItems) > 0 {
defaultWriter.Print("History :\n")
return pterm.DefaultBulletList.WithWriter(cmd.OutOrStdout()).WithItems(listItems).Render()
}
return nil
}
func historyItemTitle(format string, args ...any) pterm.BulletListItem {
return pterm.BulletListItem{
Level: 0,
TextStyle: fctl.StyleGreen,
Text: fmt.Sprintf(format, args...),
}
}
func historyItemDetails(format string, args ...any) pterm.BulletListItem {
return pterm.BulletListItem{
Level: 1,
Text: fmt.Sprintf(format, args...),
}
}
func historyItemError(format string, args ...any) pterm.BulletListItem {
return pterm.BulletListItem{
Level: 1,
TextStyle: fctl.StyleRed,
Text: fmt.Sprintf(format, args...),
}
}