-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroot.go
154 lines (143 loc) · 4.76 KB
/
root.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
package cmd
import (
"context"
"encoding/json"
"errors"
"fmt"
"os"
"os/signal"
"runtime/debug"
"github.com/formancehq/fctl/cmd/auth"
"github.com/formancehq/fctl/cmd/cloud"
"github.com/formancehq/fctl/cmd/ledger"
"github.com/formancehq/fctl/cmd/login"
"github.com/formancehq/fctl/cmd/orchestration"
"github.com/formancehq/fctl/cmd/payments"
"github.com/formancehq/fctl/cmd/profiles"
"github.com/formancehq/fctl/cmd/reconciliation"
"github.com/formancehq/fctl/cmd/search"
"github.com/formancehq/fctl/cmd/stack"
"github.com/formancehq/fctl/cmd/ui"
"github.com/formancehq/fctl/cmd/version"
"github.com/formancehq/fctl/cmd/wallets"
"github.com/formancehq/fctl/cmd/webhooks"
"github.com/formancehq/fctl/membershipclient"
fctl "github.com/formancehq/fctl/pkg"
"github.com/formancehq/formance-sdk-go/v3/pkg/models/sdkerrors"
"github.com/formancehq/go-libs/api"
"github.com/formancehq/go-libs/logging"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
)
func init() {
cobra.EnableTraverseRunHooks = true
}
func NewRootCommand() *cobra.Command {
homedir, err := os.UserHomeDir()
if err != nil {
panic(err)
}
cmd := fctl.NewCommand("fctl",
fctl.WithSilenceError(),
fctl.WithShortDescription("Formance Control CLI"),
fctl.WithChildCommands(
ui.NewCommand(),
version.NewCommand(),
login.NewCommand(),
NewPromptCommand(),
ledger.NewCommand(),
payments.NewCommand(),
reconciliation.NewCommand(),
profiles.NewCommand(),
stack.NewCommand(),
auth.NewCommand(),
cloud.NewCommand(),
search.NewCommand(),
webhooks.NewCommand(),
wallets.NewCommand(),
orchestration.NewCommand(),
),
fctl.WithPersistentStringPFlag(fctl.ProfileFlag, "p", "", "config profile to use"),
fctl.WithPersistentStringPFlag(fctl.FileFlag, "c", fmt.Sprintf("%s/.formance/fctl.config", homedir), "Debug mode"),
fctl.WithPersistentBoolPFlag(fctl.DebugFlag, "d", false, "Debug mode"),
fctl.WithPersistentStringPFlag(fctl.OutputFlag, "o", "plain", "Output format (plain, json)"),
fctl.WithPersistentBoolFlag(fctl.InsecureTlsFlag, false, "Insecure TLS"),
fctl.WithPersistentBoolFlag(fctl.TelemetryFlag, false, "Telemetry enabled"),
fctl.WithPersistentPreRunE(func(cmd *cobra.Command, args []string) error {
logger := logging.NewDefaultLogger(cmd.OutOrStdout(), fctl.GetBool(cmd, fctl.DebugFlag), false)
ctx := logging.ContextWithLogger(cmd.Context(), logger)
cmd.SetContext(ctx)
return nil
}),
)
cmd.Version = version.Version
cmd.RegisterFlagCompletionFunc(fctl.ProfileFlag, func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
cfg, err := fctl.GetConfig(cmd)
if err != nil {
return []string{}, cobra.ShellCompDirectiveError
}
ret := make([]string, 0)
for name := range cfg.GetProfiles() {
ret = append(ret, name)
}
return ret, cobra.ShellCompDirectiveDefault
})
return cmd
}
func Execute() {
defer func() {
if e := recover(); e != nil {
pterm.Error.WithWriter(os.Stderr).Printfln("%s", e)
debug.PrintStack()
}
}()
ctx, _ := signal.NotifyContext(context.TODO(), os.Interrupt)
cmd := NewRootCommand()
if err := cmd.ExecuteContext(ctx); err != nil {
switch {
case errors.Is(err, fctl.ErrMissingApproval):
pterm.Error.WithWriter(os.Stderr).Printfln("Command aborted as you didn't approve.")
os.Exit(1)
case fctl.IsInvalidAuthentication(err):
pterm.Error.WithWriter(os.Stderr).Printfln("Your authentication is invalid, please login :)")
default:
unwrapped := err
for unwrapped != nil {
//notes(gfyrag): not a clean assertion but following errors does not implements standard Is() helper for errors
switch err := unwrapped.(type) {
case *sdkerrors.ErrorResponse:
printErrorResponse(err)
return
case *sdkerrors.V2ErrorResponse:
printV2ErrorResponse(err)
return
case *membershipclient.GenericOpenAPIError:
body := err.Body()
errResponse := api.ErrorResponse{}
if err := json.Unmarshal(body, &errResponse); err != nil {
panic(err)
}
printError(errResponse.ErrorCode, errResponse.ErrorMessage, &errResponse.Details)
return
default:
pterm.Error.WithWriter(os.Stderr).Println(unwrapped)
unwrapped = errors.Unwrap(unwrapped)
}
}
}
os.Exit(255)
}
}
func printError(code string, message string, details *string) {
pterm.Error.WithWriter(os.Stderr).Printfln("Got error with code %s: %s", code, message)
if details != nil && *details != "" {
pterm.Error.WithWriter(os.Stderr).Printfln("Details:\r\n%s", *details)
}
os.Exit(2)
}
func printV2ErrorResponse(target *sdkerrors.V2ErrorResponse) {
printError(string(target.ErrorCode), target.ErrorMessage, target.Details)
}
func printErrorResponse(target *sdkerrors.ErrorResponse) {
printError(string(target.ErrorCode), target.ErrorMessage, target.Details)
}