-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathcommand.go
More file actions
281 lines (253 loc) · 10.7 KB
/
command.go
File metadata and controls
281 lines (253 loc) · 10.7 KB
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
package internal
import (
"fmt"
"os"
"runtime/debug"
"strings"
shell "github.com/brianstrauch/cobra-shell"
"github.com/spf13/cobra"
ccloudv1 "github.com/confluentinc/ccloud-sdk-go-v1-public"
cliv1 "github.com/confluentinc/ccloud-sdk-go-v2/cli/v1"
"github.com/confluentinc/cli/v4/internal/ai"
apikey "github.com/confluentinc/cli/v4/internal/api-key"
"github.com/confluentinc/cli/v4/internal/asyncapi"
auditlog "github.com/confluentinc/cli/v4/internal/audit-log"
"github.com/confluentinc/cli/v4/internal/billing"
"github.com/confluentinc/cli/v4/internal/byok"
ccpm "github.com/confluentinc/cli/v4/internal/ccpm"
cloudsignup "github.com/confluentinc/cli/v4/internal/cloud-signup"
"github.com/confluentinc/cli/v4/internal/cluster"
"github.com/confluentinc/cli/v4/internal/completion"
"github.com/confluentinc/cli/v4/internal/configuration"
"github.com/confluentinc/cli/v4/internal/connect"
"github.com/confluentinc/cli/v4/internal/context"
ccl "github.com/confluentinc/cli/v4/internal/custom-code-logging"
"github.com/confluentinc/cli/v4/internal/endpoint"
"github.com/confluentinc/cli/v4/internal/environment"
"github.com/confluentinc/cli/v4/internal/feedback"
"github.com/confluentinc/cli/v4/internal/flink"
"github.com/confluentinc/cli/v4/internal/iam"
"github.com/confluentinc/cli/v4/internal/kafka"
"github.com/confluentinc/cli/v4/internal/ksql"
"github.com/confluentinc/cli/v4/internal/local"
"github.com/confluentinc/cli/v4/internal/login"
"github.com/confluentinc/cli/v4/internal/logout"
"github.com/confluentinc/cli/v4/internal/network"
"github.com/confluentinc/cli/v4/internal/organization"
"github.com/confluentinc/cli/v4/internal/plugin"
"github.com/confluentinc/cli/v4/internal/prompt"
providerintegration "github.com/confluentinc/cli/v4/internal/provider-integration"
schemaregistry "github.com/confluentinc/cli/v4/internal/schema-registry"
"github.com/confluentinc/cli/v4/internal/secret"
servicequota "github.com/confluentinc/cli/v4/internal/service-quota"
streamshare "github.com/confluentinc/cli/v4/internal/stream-share"
"github.com/confluentinc/cli/v4/internal/tableflow"
unifiedstreammanager "github.com/confluentinc/cli/v4/internal/unified-stream-manager"
"github.com/confluentinc/cli/v4/internal/update"
"github.com/confluentinc/cli/v4/internal/version"
pauth "github.com/confluentinc/cli/v4/pkg/auth"
"github.com/confluentinc/cli/v4/pkg/ccloudv2"
pcmd "github.com/confluentinc/cli/v4/pkg/cmd"
"github.com/confluentinc/cli/v4/pkg/config"
"github.com/confluentinc/cli/v4/pkg/errors"
"github.com/confluentinc/cli/v4/pkg/featureflags"
"github.com/confluentinc/cli/v4/pkg/form"
"github.com/confluentinc/cli/v4/pkg/help"
"github.com/confluentinc/cli/v4/pkg/jwt"
"github.com/confluentinc/cli/v4/pkg/log"
"github.com/confluentinc/cli/v4/pkg/output"
ppanic "github.com/confluentinc/cli/v4/pkg/panic-recovery"
pplugin "github.com/confluentinc/cli/v4/pkg/plugin"
secrets "github.com/confluentinc/cli/v4/pkg/secret"
"github.com/confluentinc/cli/v4/pkg/usage"
pversion "github.com/confluentinc/cli/v4/pkg/version"
)
func NewConfluentCommand(cfg *config.Config) *cobra.Command {
cmd := &cobra.Command{
Use: pversion.CLIName,
Short: fmt.Sprintf("%s.", pversion.FullCLIName),
Long: getLongDescription(cfg),
Version: cfg.Version.Version,
}
cmd.Flags().Bool("version", false, fmt.Sprintf("Show version of the %s.", pversion.FullCLIName))
cmd.PersistentFlags().BoolP("help", "h", false, "Show help for this command.")
cmd.PersistentFlags().CountP("verbose", "v", "Increase verbosity (-v for warn, -vv for info, -vvv for debug, -vvvv for trace).")
cmd.PersistentFlags().Bool("unsafe-trace", false, "Equivalent to -vvvv, but also log HTTP requests and responses which might contain plaintext secrets.")
authTokenHandler := pauth.NewAuthTokenHandler()
ccloudClientFactory := pauth.NewCCloudClientFactory(cfg.Version.UserAgent)
jwtValidator := jwt.NewValidator()
ccloudClient := getCloudClient(cfg, ccloudClientFactory)
loginCredentialsManager := pauth.NewLoginCredentialsManager(form.NewPrompt(), ccloudClient)
loginOrganizationManager := pauth.NewLoginOrganizationManagerImpl()
mdsClientManager := &pauth.MDSClientManagerImpl{}
featureflags.Init(cfg)
cmd.SetHelpFunc(func(cmd *cobra.Command, args []string) {
pcmd.LabelRequiredFlags(cmd)
_ = help.WriteHelpTemplate(cmd)
})
prerunner := &pcmd.PreRun{
Config: cfg,
AuthTokenHandler: authTokenHandler,
CCloudClientFactory: ccloudClientFactory,
JWTValidator: jwtValidator,
LoginCredentialsManager: loginCredentialsManager,
MDSClientManager: mdsClientManager,
Version: cfg.Version,
}
cmd.AddCommand(ai.New(prerunner))
cmd.AddCommand(apikey.New(prerunner))
cmd.AddCommand(asyncapi.New(prerunner))
cmd.AddCommand(auditlog.New(prerunner))
cmd.AddCommand(billing.New(prerunner))
cmd.AddCommand(byok.New(prerunner))
cmd.AddCommand(ccpm.New(cfg, prerunner))
cmd.AddCommand(cluster.New(prerunner, cfg.Version.UserAgent))
cmd.AddCommand(cloudsignup.New(prerunner))
cmd.AddCommand(completion.New())
cmd.AddCommand(configuration.New(cfg, prerunner))
cmd.AddCommand(context.New(prerunner))
cmd.AddCommand(connect.New(cfg, prerunner))
cmd.AddCommand(ccl.New(cfg, prerunner))
cmd.AddCommand(endpoint.New(prerunner))
cmd.AddCommand(environment.New(prerunner))
cmd.AddCommand(feedback.New(prerunner))
cmd.AddCommand(flink.New(cfg, prerunner))
cmd.AddCommand(iam.New(cfg, prerunner))
cmd.AddCommand(kafka.New(cfg, prerunner))
cmd.AddCommand(ksql.New(cfg, prerunner))
cmd.AddCommand(local.New(cfg, prerunner))
cmd.AddCommand(login.New(cfg, prerunner, ccloudClientFactory, mdsClientManager, loginCredentialsManager, loginOrganizationManager, authTokenHandler))
cmd.AddCommand(logout.New(cfg, prerunner, authTokenHandler))
cmd.AddCommand(network.New(prerunner, cfg))
cmd.AddCommand(organization.New(prerunner))
cmd.AddCommand(plugin.New(cfg, prerunner))
cmd.AddCommand(prompt.New(cfg))
cmd.AddCommand(providerintegration.New(prerunner))
cmd.AddCommand(servicequota.New(prerunner))
cmd.AddCommand(schemaregistry.New(cfg, prerunner))
cmd.AddCommand(secret.New(prerunner, secrets.NewPasswordProtectionPlugin()))
cmd.AddCommand(shell.New(cmd, func() *cobra.Command { return NewConfluentCommand(cfg) }))
cmd.AddCommand(streamshare.New(prerunner))
cmd.AddCommand(tableflow.New(prerunner))
cmd.AddCommand(unifiedstreammanager.New(prerunner, cfg))
cmd.AddCommand(update.New(cfg, prerunner))
cmd.AddCommand(version.New(prerunner, cfg.Version))
_ = cfg.ParseFlagsIntoConfig(cmd)
changeDefaults(cmd, cfg)
deprecateCommandsAndFlags(cmd, cfg)
featureflags.Manager.SetCommandAndFlags(cmd, os.Args[1:])
disableCommandAndFlagHelpText(cmd, cfg)
return cmd
}
func Execute(cmd *cobra.Command, args []string, cfg *config.Config) error {
defer func() {
if r := recover(); r != nil {
if !cfg.IsCloudLogin() || cfg.HasGovHostname() || !cfg.Version.IsReleased() {
log.CliLogger.Trace(string(debug.Stack()))
}
u := ppanic.CollectPanic(cmd, args, cfg)
if err := reportUsage(cmd, cfg, u); err != nil {
output.ErrPrint(cfg.EnableColor, errors.DisplaySuggestionsMessage(err))
}
cobra.CheckErr(r)
}
}()
if !cfg.DisablePlugins {
if plugin := pplugin.FindPlugin(cmd, args, cfg); plugin != nil {
return pplugin.ExecPlugin(plugin)
}
}
// Usage collection is a wrapper around Execute() instead of a post-run function so we can collect the error status.
u := usage.New(cfg.Version.Version)
if !cfg.IsTest && cfg.Version.IsReleased() {
cmd.PersistentPostRun = u.Collect
}
err := cmd.Execute()
output.ErrPrint(cfg.EnableColor, errors.DisplaySuggestionsMessage(err))
u.Error = cliv1.PtrBool(err != nil)
if err := reportUsage(cmd, cfg, u); err != nil {
return err
}
return err
}
func reportUsage(cmd *cobra.Command, cfg *config.Config, u *usage.Usage) error {
if cfg.IsCloudLogin() && !cfg.HasGovHostname() && u.Command != nil && *u.Command != "" {
unsafeTrace, err := cmd.Flags().GetBool("unsafe-trace")
if err != nil {
return err
}
u.Report(ccloudv2.NewClient(cfg, unsafeTrace))
}
return nil
}
func getLongDescription(cfg *config.Config) string {
switch {
case cfg.IsCloudLogin():
return "Manage your Confluent Cloud."
case cfg.IsOnPremLogin():
return "Manage your Confluent Platform."
default:
return "Manage your Confluent Cloud or Confluent Platform. Log in to see all available commands."
}
}
func changeDefaults(cmd *cobra.Command, cfg *config.Config) {
hideAndErrIfMissingRunRequirement(cmd, cfg)
catchErrors(cmd)
cmd.Flags().SortFlags = false
for _, subcommand := range cmd.Commands() {
changeDefaults(subcommand, cfg)
}
}
// hideAndErrIfMissingRunRequirement hides commands that don't meet a requirement and errs if a user attempts to use it;
// for example, an on-prem command shouldn't be used by a cloud user.
func hideAndErrIfMissingRunRequirement(cmd *cobra.Command, cfg *config.Config) {
if err := pcmd.ErrIfMissingRunRequirement(cmd, cfg); err != nil {
cmd.Hidden = true
// Show err for internal commands. Leaf commands will err in the PreRun function.
if cmd.HasSubCommands() {
cmd.RunE = func(_ *cobra.Command, _ []string) error { return err }
}
}
}
// catchErrors catches (and modifies) errors from any of the built-in error-producing functions.
func catchErrors(cmd *cobra.Command) {
if cmd.PersistentPreRunE != nil {
cmd.PersistentPreRunE = pcmd.CatchErrors(cmd.PersistentPreRunE)
}
if cmd.PreRunE != nil {
cmd.PreRunE = pcmd.CatchErrors(cmd.PreRunE)
}
if cmd.RunE != nil {
cmd.RunE = pcmd.CatchErrors(cmd.RunE)
}
}
func getCloudClient(cfg *config.Config, ccloudClientFactory pauth.CCloudClientFactory) *ccloudv1.Client {
if cfg.IsCloudLogin() {
return ccloudClientFactory.AnonHTTPClientFactory(pauth.CCloudURL)
}
return nil
}
func deprecateCommandsAndFlags(cmd *cobra.Command, cfg *config.Config) {
deprecatedCmds := featureflags.Manager.JsonVariation(featureflags.DeprecationNotices, cfg.Context(), config.CliLaunchDarklyClient, true, []any{})
cmdToFlagsAndMsg := featureflags.GetAnnouncementsOrDeprecation(deprecatedCmds)
for name, flagsAndMsg := range cmdToFlagsAndMsg {
if cmd, _, err := cmd.Find(strings.Split(name, " ")); err == nil {
if flagsAndMsg.Flags == nil {
featureflags.DeprecateCommandTree(cmd)
} else {
featureflags.DeprecateFlags(cmd, flagsAndMsg.Flags)
}
}
}
}
func disableCommandAndFlagHelpText(cmd *cobra.Command, cfg *config.Config) {
disableResp := featureflags.GetLDDisableMap(cfg.Context())
disabledCmdsAndFlags, ok := disableResp["patterns"].([]any)
if ok && len(disabledCmdsAndFlags) > 0 {
for _, pattern := range disabledCmdsAndFlags {
if command, flags, err := cmd.Find(strings.Split(pattern.(string), " ")); err == nil {
featureflags.DisableHelpText(command, flags)
}
}
}
}