• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

kubernetes-sigs / kubebuilder / 21709644082

05 Feb 2026 11:25AM UTC coverage: 66.786% (-7.1%) from 73.91%
21709644082

Pull #5352

github

camilamacedo86
(chore) Add delete interface and implementation for all options

Add delete functionality to remove APIs and webhooks from projects.
Users can now clean up scaffolded resources.
Pull Request #5352: ✨ Added Delete API and implemented a unified interface across all commands and plugin options

397 of 1568 new or added lines in 21 files covered. (25.32%)

1 existing line in 1 file now uncovered.

7106 of 10640 relevant lines covered (66.79%)

37.96 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

82.3
/pkg/cli/cli.go
1
/*
2
Copyright 2020 The Kubernetes Authors.
3

4
Licensed under the Apache License, Version 2.0 (the "License");
5
you may not use this file except in compliance with the License.
6
You may obtain a copy of the License at
7

8
    http://www.apache.org/licenses/LICENSE-2.0
9

10
Unless required by applicable law or agreed to in writing, software
11
distributed under the License is distributed on an "AS IS" BASIS,
12
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
See the License for the specific language governing permissions and
14
limitations under the License.
15
*/
16

17
package cli
18

19
import (
20
        "errors"
21
        "fmt"
22
        log "log/slog"
23
        "os"
24
        "slices"
25
        "strings"
26

27
        "github.com/spf13/afero"
28
        "github.com/spf13/cobra"
29
        "github.com/spf13/pflag"
30

31
        "sigs.k8s.io/kubebuilder/v4/pkg/config"
32
        yamlstore "sigs.k8s.io/kubebuilder/v4/pkg/config/store/yaml"
33
        "sigs.k8s.io/kubebuilder/v4/pkg/machinery"
34
        "sigs.k8s.io/kubebuilder/v4/pkg/model/stage"
35
        "sigs.k8s.io/kubebuilder/v4/pkg/plugin"
36
)
37

38
const (
39
        noticeColor    = "\033[1;33m%s\033[0m"
40
        deprecationFmt = "[Deprecation Notice] %s\n\n"
41

42
        pluginsFlag        = "plugins"
43
        projectVersionFlag = "project-version"
44
)
45

46
// CLI is the command line utility that is used to scaffold kubebuilder project files.
47
type CLI struct {
48
        /* Fields set by Option */
49

50
        // Root command name. It is injected downstream to provide correct help, usage, examples and errors.
51
        commandName string
52
        // Full CLI version string.
53
        version string
54
        // CLI version string (just the CLI version number, no extra information).
55
        cliVersion string
56
        // CLI root's command description.
57
        description string
58
        // Plugins registered in the CLI.
59
        plugins map[string]plugin.Plugin
60
        // Default plugins in case none is provided and a config file can't be found.
61
        defaultPlugins map[config.Version][]string
62
        // Default project version in case none is provided and a config file can't be found.
63
        defaultProjectVersion config.Version
64
        // Commands injected by options.
65
        extraCommands []*cobra.Command
66
        // Alpha commands injected by options.
67
        extraAlphaCommands []*cobra.Command
68
        // Whether to add a completion command to the CLI.
69
        completionCommand bool
70

71
        /* Internal fields */
72

73
        // Plugin keys to scaffold with.
74
        pluginKeys []string
75
        // Project version to scaffold.
76
        projectVersion config.Version
77

78
        // A filtered set of plugins that should be used by command constructors.
79
        resolvedPlugins []plugin.Plugin
80

81
        // Root command.
82
        cmd *cobra.Command
83

84
        // Underlying fs
85
        fs machinery.Filesystem
86
}
87

88
// New creates a new CLI instance.
89
//
90
// It follows the functional options pattern in order to customize the resulting CLI.
91
//
92
// It returns an error if any of the provided options fails. As some processing needs
93
// to be done, execution errors may be found here. Instead of returning an error, this
94
// function will return a valid CLI that errors in Run so that help is provided to the
95
// user.
96
func New(options ...Option) (*CLI, error) {
11✔
97
        // Create the CLI.
11✔
98
        c, err := newCLI(options...)
11✔
99
        if err != nil {
12✔
100
                return nil, err
1✔
101
        }
1✔
102

103
        // Build the cmd tree.
104
        if err := c.buildCmd(); err != nil {
11✔
105
                c.cmd.RunE = errCmdFunc(err)
1✔
106
                return c, nil
1✔
107
        }
1✔
108

109
        // Add extra commands injected by options.
110
        if err := c.addExtraCommands(); err != nil {
10✔
111
                return nil, err
1✔
112
        }
1✔
113

114
        // Add extra alpha commands injected by options.
115
        if err := c.addExtraAlphaCommands(); err != nil {
9✔
116
                return nil, err
1✔
117
        }
1✔
118

119
        // Write deprecation notices after all commands have been constructed.
120
        c.printDeprecationWarnings()
7✔
121

7✔
122
        return c, nil
7✔
123
}
124

125
// newCLI creates a default CLI instance and applies the provided options.
126
// It is as a separate function for test purposes.
127
func newCLI(options ...Option) (*CLI, error) {
41✔
128
        // Default CLI options.
41✔
129
        c := &CLI{
41✔
130
                commandName: "kubebuilder",
41✔
131
                description: `CLI tool for building Kubernetes extensions and tools.
41✔
132
`,
41✔
133
                plugins:        make(map[string]plugin.Plugin),
41✔
134
                defaultPlugins: make(map[config.Version][]string),
41✔
135
                fs:             machinery.Filesystem{FS: afero.NewOsFs()},
41✔
136
        }
41✔
137

41✔
138
        // Apply provided options.
41✔
139
        for _, option := range options {
93✔
140
                if err := option(c); err != nil {
69✔
141
                        return nil, err
17✔
142
                }
17✔
143
        }
144

145
        return c, nil
24✔
146
}
147

148
// buildCmd creates the underlying cobra command and stores it internally.
149
func (c *CLI) buildCmd() error {
12✔
150
        c.cmd = c.newRootCmd()
12✔
151

12✔
152
        var uve config.UnsupportedVersionError
12✔
153

12✔
154
        // Get project version and plugin keys.
12✔
155
        switch err := c.getInfo(); {
12✔
156
        case err == nil:
10✔
157
        case errors.As(err, &uve) && uve.Version.Compare(config.Version{Number: 3, Stage: stage.Alpha}) == 0:
1✔
158
                // Check if the corresponding stable version exists, set c.projectVersion and break
1✔
159
                stableVersion := config.Version{
1✔
160
                        Number: uve.Version.Number,
1✔
161
                }
1✔
162
                if config.IsRegistered(stableVersion) {
2✔
163
                        // Use the stableVersion
1✔
164
                        c.projectVersion = stableVersion
1✔
165
                } else {
1✔
166
                        // stable version not registered, let's bail out
×
167
                        return err
×
168
                }
×
169
        default:
1✔
170
                return err
1✔
171
        }
172

173
        // Resolve plugins for project version and plugin keys.
174
        if err := c.resolvePlugins(); err != nil {
12✔
175
                return err
1✔
176
        }
1✔
177

178
        // Add the subcommands
179
        c.addSubcommands()
10✔
180

10✔
181
        return nil
10✔
182
}
183

184
// getInfo obtains the plugin keys and project version resolving conflicts between the project config file and flags.
185
func (c *CLI) getInfo() error {
12✔
186
        // Get plugin keys and project version from project configuration file
12✔
187
        // We discard the error if file doesn't exist because not being able to read a project configuration
12✔
188
        // file is not fatal for some commands. The ones that require it need to check its existence later.
12✔
189
        hasConfigFile := true
12✔
190
        if err := c.getInfoFromConfigFile(); errors.Is(err, os.ErrNotExist) {
22✔
191
                hasConfigFile = false
10✔
192
        } else if err != nil {
14✔
193
                return err
2✔
194
        }
2✔
195

196
        // We can't early return here in case a project configuration file was found because
197
        // this command call may override the project plugins.
198

199
        // Get project version and plugin info from flags
200
        if err := c.getInfoFromFlags(hasConfigFile); err != nil {
10✔
201
                return err
×
202
        }
×
203

204
        // Get project version and plugin info from defaults
205
        c.getInfoFromDefaults()
10✔
206

10✔
207
        return nil
10✔
208
}
209

210
// getInfoFromConfigFile obtains the project version and plugin keys from the project config file.
211
func (c *CLI) getInfoFromConfigFile() error {
12✔
212
        // Read the project configuration file
12✔
213
        cfg := yamlstore.New(c.fs)
12✔
214

12✔
215
        // Workaround for https://github.com/kubernetes-sigs/kubebuilder/issues/4433
12✔
216
        //
12✔
217
        // This allows the `kubebuilder alpha generate` command to work with old projects
12✔
218
        // that use plugin versions no longer supported (like go.kubebuilder.io/v3).
12✔
219
        //
12✔
220
        // We read the PROJECT file into memory and update the plugin version (e.g. from v3 to v4)
12✔
221
        // before the CLI tries to load it. This avoids errors during config loading
12✔
222
        // and lets users migrate their project layout from go/v3 to go/v4.
12✔
223

12✔
224
        if isAlphaGenerateCommand(os.Args[1:]) {
12✔
225
                // Patch raw file bytes before unmarshalling
×
226
                if err := patchProjectFileInMemoryIfNeeded(c.fs.FS, yamlstore.DefaultPath); err != nil {
×
227
                        return err
×
228
                }
×
229
        }
230

231
        if err := cfg.Load(); err != nil {
24✔
232
                return fmt.Errorf("error loading configuration: %w", err)
12✔
233
        }
12✔
234

235
        return c.getInfoFromConfig(cfg.Config())
×
236
}
237

238
// isAlphaGenerateCommand checks if the command invocation is `kubebuilder alpha generate`
239
// by scanning os.Args (excluding global flags). It returns true if "alpha" is followed by "generate".
240
func isAlphaGenerateCommand(args []string) bool {
12✔
241
        positional := []string{}
12✔
242
        skip := false
12✔
243

12✔
244
        for i := range args {
87✔
245
                arg := args[i]
75✔
246

75✔
247
                // Skip flags and their values
75✔
248
                if strings.HasPrefix(arg, "-") {
148✔
249
                        // If the flag is in --flag=value format, skip only this one
73✔
250
                        if strings.Contains(arg, "=") {
133✔
251
                                continue
60✔
252
                        }
253
                        // If it's --flag value format, skip next one too
254
                        if i+1 < len(args) && !strings.HasPrefix(args[i+1], "-") {
14✔
255
                                skip = true
1✔
256
                        }
1✔
257
                        continue
13✔
258
                }
259
                if skip {
3✔
260
                        skip = false
1✔
261
                        continue
1✔
262
                }
263
                positional = append(positional, arg)
1✔
264
        }
265

266
        // Check for `alpha generate` in positional arguments
267
        for i := 0; i < len(positional)-1; i++ {
12✔
268
                if positional[i] == "alpha" && positional[i+1] == "generate" {
×
269
                        return true
×
270
                }
×
271
        }
272

273
        return false
12✔
274
}
275

276
// patchProjectFileInMemoryIfNeeded updates deprecated plugin keys in the PROJECT file in place,
277
// so that users can run `kubebuilder alpha generate` even with older plugin layouts.
278
//
279
// See: https://github.com/kubernetes-sigs/kubebuilder/issues/4433
280
//
281
// This ensures the CLI can successfully load the config without failing on unsupported plugin versions.
282
func patchProjectFileInMemoryIfNeeded(fs afero.Fs, path string) error {
×
283
        type pluginReplacement struct {
×
284
                Old string
×
285
                New string
×
286
        }
×
287

×
288
        replacements := []pluginReplacement{
×
289
                {"go.kubebuilder.io/v2", "go.kubebuilder.io/v4"},
×
290
                {"go.kubebuilder.io/v3", "go.kubebuilder.io/v4"},
×
291
                {"go.kubebuilder.io/v3-alpha", "go.kubebuilder.io/v4"},
×
292
        }
×
293

×
294
        content, err := afero.ReadFile(fs, path)
×
295
        if err != nil {
×
296
                return nil
×
297
        }
×
298

299
        original := string(content)
×
300
        modified := original
×
301

×
302
        for _, rep := range replacements {
×
303
                if strings.Contains(modified, rep.Old) {
×
304
                        modified = strings.ReplaceAll(modified, rep.Old, rep.New)
×
305
                        log.Warn("Project is using an old and unsupported plugin layout",
×
306
                                "old_layout", rep.Old,
×
307
                                "new_layout", rep.New,
×
308
                                "note", "Replace in memory to allow `alpha generate` to work.",
×
309
                        )
×
310
                }
×
311
        }
312

313
        if modified != original {
×
314
                err := afero.WriteFile(fs, path, []byte(modified), 0o755)
×
315
                if err != nil {
×
316
                        return fmt.Errorf("failed to write patched PROJECT file: %w", err)
×
317
                }
×
318
        }
319

320
        return nil
×
321
}
322

323
// getInfoFromConfig obtains the project version and plugin keys from the project config.
324
// It is extracted from getInfoFromConfigFile for testing purposes.
325
func (c *CLI) getInfoFromConfig(projectConfig config.Config) error {
3✔
326
        c.pluginKeys = projectConfig.GetPluginChain()
3✔
327
        c.projectVersion = projectConfig.GetVersion()
3✔
328

3✔
329
        for _, pluginKey := range c.pluginKeys {
7✔
330
                if err := plugin.ValidateKey(pluginKey); err != nil {
5✔
331
                        return fmt.Errorf("invalid plugin key found in project configuration file: %w", err)
1✔
332
                }
1✔
333
        }
334

335
        return nil
2✔
336
}
337

338
// getInfoFromFlags obtains the project version and plugin keys from flags.
339
func (c *CLI) getInfoFromFlags(hasConfigFile bool) error {
22✔
340
        // Partially parse the command line arguments
22✔
341
        fs := pflag.NewFlagSet("base", pflag.ContinueOnError)
22✔
342

22✔
343
        // Load the base command global flags
22✔
344
        fs.AddFlagSet(c.cmd.PersistentFlags())
22✔
345

22✔
346
        // If we were unable to load the project configuration, we should also accept the project version flag
22✔
347
        var projectVersionStr string
22✔
348
        if !hasConfigFile {
44✔
349
                fs.StringVar(&projectVersionStr, projectVersionFlag, "", "project version")
22✔
350
        }
22✔
351

352
        // FlagSet special cases --help and -h, so we need to create a dummy flag with these 2 values to prevent the default
353
        // behavior (printing the usage of this FlagSet) as we want to print the usage message of the underlying command.
354
        fs.BoolP("help", "h", false, fmt.Sprintf("help for %s", c.commandName))
22✔
355

22✔
356
        // Omit unknown flags to avoid parsing errors
22✔
357
        fs.ParseErrorsAllowlist = pflag.ParseErrorsAllowlist{UnknownFlags: true}
22✔
358

22✔
359
        // Parse the arguments
22✔
360
        if err := fs.Parse(os.Args[1:]); err != nil {
22✔
361
                return fmt.Errorf("could not parse flags: %w", err)
×
362
        }
×
363

364
        // If any plugin key was provided, replace those from the project configuration file
365
        // Exception: for delete commands, APPEND to layout plugins instead of replacing
366
        if pluginKeys, err := fs.GetStringSlice(pluginsFlag); err != nil {
22✔
367
                return fmt.Errorf("invalid flag %q: %w", pluginsFlag, err)
×
368
        } else if len(pluginKeys) != 0 {
30✔
369
                // Remove leading and trailing spaces and validate the plugin keys
8✔
370
                for i, key := range pluginKeys {
24✔
371
                        pluginKeys[i] = strings.TrimSpace(key)
16✔
372
                        if err := plugin.ValidateKey(pluginKeys[i]); err != nil {
17✔
373
                                return fmt.Errorf("invalid plugin %q found in flags: %w", pluginKeys[i], err)
1✔
374
                        }
1✔
375
                }
376

377
                // For delete commands, if we have layout plugins from PROJECT, keep them
378
                // and append the user-specified plugins (avoiding duplicates)
379
                isDeleteCommand := len(os.Args) > 1 && os.Args[1] == "delete"
7✔
380

7✔
381
                if isDeleteCommand && len(c.pluginKeys) > 0 {
7✔
NEW
382
                        // Append user plugins to layout plugins (deduplicate)
×
NEW
383
                        combined := append([]string{}, c.pluginKeys...)
×
NEW
384
                        for _, userPlugin := range pluginKeys {
×
NEW
385
                                found := slices.Contains(combined, userPlugin)
×
NEW
386
                                if !found {
×
NEW
387
                                        combined = append(combined, userPlugin)
×
NEW
388
                                }
×
389
                        }
NEW
390
                        c.pluginKeys = combined
×
391
                } else {
7✔
392
                        c.pluginKeys = pluginKeys
7✔
393
                }
7✔
394
        }
395

396
        // If the project version flag was accepted but not provided keep the empty version and try to resolve it later,
397
        // else validate the provided project version
398
        if projectVersionStr != "" {
26✔
399
                if err := c.projectVersion.Parse(projectVersionStr); err != nil {
6✔
400
                        return fmt.Errorf("invalid project version flag: %w", err)
1✔
401
                }
1✔
402
        }
403

404
        return nil
20✔
405
}
406

407
// getInfoFromDefaults obtains the plugin keys, and maybe the project version from the default values
408
func (c *CLI) getInfoFromDefaults() {
14✔
409
        // Should not use default values if a plugin was already set
14✔
410
        // This checks includes the case where a project configuration file was found,
14✔
411
        // as it will always have at least one plugin key set by now
14✔
412
        if len(c.pluginKeys) != 0 {
16✔
413
                // We don't assign a default value for project version here because we may be able to
2✔
414
                // resolve the project version after resolving the plugins.
2✔
415
                return
2✔
416
        }
2✔
417

418
        // If the user provided a project version, use the default plugins for that project version
419
        if c.projectVersion.Validate() == nil {
13✔
420
                c.pluginKeys = c.defaultPlugins[c.projectVersion]
1✔
421
                return
1✔
422
        }
1✔
423

424
        // Else try to use the default plugins for the default project version
425
        if c.defaultProjectVersion.Validate() == nil {
13✔
426
                var found bool
2✔
427
                if c.pluginKeys, found = c.defaultPlugins[c.defaultProjectVersion]; found {
4✔
428
                        c.projectVersion = c.defaultProjectVersion
2✔
429
                        return
2✔
430
                }
2✔
431
        }
432

433
        // Else check if only default plugins for a project version were provided
434
        if len(c.defaultPlugins) == 1 {
16✔
435
                for projectVersion, defaultPlugins := range c.defaultPlugins {
14✔
436
                        c.pluginKeys = defaultPlugins
7✔
437
                        c.projectVersion = projectVersion
7✔
438
                        return
7✔
439
                }
7✔
440
        }
441
}
442

443
const unstablePluginMsg = " (plugin version is unstable, there may be an upgrade available: " +
444
        "https://kubebuilder.io/plugins/plugins-versioning)"
445

446
// resolvePlugins selects from the available plugins those that match the project version and plugin keys provided.
447
func (c *CLI) resolvePlugins() error {
27✔
448
        knownProjectVersion := c.projectVersion.Validate() == nil
27✔
449

27✔
450
        for _, pluginKey := range c.pluginKeys {
55✔
451
                var extraErrMsg string
28✔
452

28✔
453
                plugins := make([]plugin.Plugin, 0, len(c.plugins))
28✔
454
                for _, p := range c.plugins {
295✔
455
                        plugins = append(plugins, p)
267✔
456
                }
267✔
457
                // We can omit the error because plugin keys have already been validated
458
                plugins, _ = plugin.FilterPluginsByKey(plugins, pluginKey)
28✔
459
                if knownProjectVersion {
47✔
460
                        plugins = plugin.FilterPluginsByProjectVersion(plugins, c.projectVersion)
19✔
461
                        extraErrMsg += fmt.Sprintf(" for project version %q", c.projectVersion)
19✔
462
                }
19✔
463

464
                // Plugins are often released as "unstable" (alpha/beta) versions, then upgraded to "stable".
465
                // This upgrade effectively removes a plugin, which is fine because unstable plugins are
466
                // under no support contract. However users should be notified _why_ their plugin cannot be found.
467
                if _, version := plugin.SplitKey(pluginKey); version != "" {
42✔
468
                        var ver plugin.Version
14✔
469
                        if err := ver.Parse(version); err != nil {
14✔
470
                                return fmt.Errorf("error parsing input plugin version from key %q: %w", pluginKey, err)
×
471
                        }
×
472
                        if !ver.IsStable() {
14✔
473
                                extraErrMsg += unstablePluginMsg
×
474
                        }
×
475
                }
476

477
                // Only 1 plugin can match
478
                switch len(plugins) {
28✔
479
                case 1:
19✔
480
                        c.resolvedPlugins = append(c.resolvedPlugins, plugins[0])
19✔
481
                case 0:
6✔
482
                        return fmt.Errorf("no plugin could be resolved with key %q%s", pluginKey, extraErrMsg)
6✔
483
                default:
3✔
484
                        return fmt.Errorf("ambiguous plugin %q%s", pluginKey, extraErrMsg)
3✔
485
                }
486
        }
487

488
        // Now we can try to resolve the project version if not known by this point
489
        if !knownProjectVersion && len(c.resolvedPlugins) > 0 {
22✔
490
                // Extract the common supported project versions
4✔
491
                supportedProjectVersions := plugin.CommonSupportedProjectVersions(c.resolvedPlugins...)
4✔
492

4✔
493
                // If there is only one common supported project version, resolve to it
4✔
494
        ProjectNumberVersionSwitch:
4✔
495
                switch len(supportedProjectVersions) {
4✔
496
                case 1:
1✔
497
                        c.projectVersion = supportedProjectVersions[0]
1✔
498
                case 0:
1✔
499
                        return fmt.Errorf("no project version supported by all the resolved plugins")
1✔
500
                default:
2✔
501
                        supportedProjectVersionStrings := make([]string, 0, len(supportedProjectVersions))
2✔
502
                        for _, supportedProjectVersion := range supportedProjectVersions {
6✔
503
                                // In case one of the multiple supported versions is the default one, choose that and exit the switch
4✔
504
                                if supportedProjectVersion.Compare(c.defaultProjectVersion) == 0 {
5✔
505
                                        c.projectVersion = c.defaultProjectVersion
1✔
506
                                        break ProjectNumberVersionSwitch
1✔
507
                                }
508
                                supportedProjectVersionStrings = append(supportedProjectVersionStrings,
3✔
509
                                        fmt.Sprintf("%q", supportedProjectVersion))
3✔
510
                        }
511
                        return fmt.Errorf("ambiguous project version, resolved plugins support the following project versions: %s",
1✔
512
                                strings.Join(supportedProjectVersionStrings, ", "))
1✔
513
                }
514
        }
515

516
        return nil
16✔
517
}
518

519
// addSubcommands returns a root command with a subcommand tree reflecting the
520
// current project's state.
521
func (c *CLI) addSubcommands() {
10✔
522
        // add the alpha command if it has any subcommands enabled
10✔
523
        c.addAlphaCmd()
10✔
524

10✔
525
        // kubebuilder completion
10✔
526
        // Only add completion if requested
10✔
527
        if c.completionCommand {
11✔
528
                c.cmd.AddCommand(c.newCompletionCmd())
1✔
529
        }
1✔
530

531
        // kubebuilder create
532
        createCmd := c.newCreateCmd()
10✔
533
        // kubebuilder create api
10✔
534
        createCmd.AddCommand(c.newCreateAPICmd())
10✔
535
        createCmd.AddCommand(c.newCreateWebhookCmd())
10✔
536
        if createCmd.HasSubCommands() {
20✔
537
                c.cmd.AddCommand(createCmd)
10✔
538
        }
10✔
539

540
        // kubebuilder delete
541
        deleteCmd := c.newDeleteCmd()
10✔
542
        // kubebuilder delete api
10✔
543
        deleteCmd.AddCommand(c.newDeleteAPICmd())
10✔
544
        deleteCmd.AddCommand(c.newDeleteWebhookCmd())
10✔
545
        if deleteCmd.HasSubCommands() {
20✔
546
                c.cmd.AddCommand(deleteCmd)
10✔
547
        }
10✔
548

549
        // kubebuilder edit
550
        c.cmd.AddCommand(c.newEditCmd())
10✔
551

10✔
552
        // kubebuilder init
10✔
553
        c.cmd.AddCommand(c.newInitCmd())
10✔
554

10✔
555
        // kubebuilder version
10✔
556
        // Only add version if a version string was provided
10✔
557
        if c.version != "" {
11✔
558
                c.cmd.AddCommand(c.newVersionCmd())
1✔
559
        }
1✔
560
}
561

562
// addExtraCommands adds the additional commands.
563
func (c *CLI) addExtraCommands() error {
9✔
564
        for _, cmd := range c.extraCommands {
11✔
565
                for _, subCmd := range c.cmd.Commands() {
12✔
566
                        if cmd.Name() == subCmd.Name() {
11✔
567
                                return fmt.Errorf("command %q already exists", cmd.Name())
1✔
568
                        }
1✔
569
                }
570
                c.cmd.AddCommand(cmd)
1✔
571
        }
572
        return nil
8✔
573
}
574

575
// printDeprecationWarnings prints the deprecation warnings of the resolved plugins.
576
func (c CLI) printDeprecationWarnings() {
7✔
577
        for _, p := range c.resolvedPlugins {
12✔
578
                if p != nil && p.(plugin.Deprecated) != nil && len(p.(plugin.Deprecated).DeprecationWarning()) > 0 {
6✔
579
                        _, _ = fmt.Fprintf(os.Stderr, noticeColor, fmt.Sprintf(deprecationFmt, p.(plugin.Deprecated).DeprecationWarning()))
1✔
580
                }
1✔
581
        }
582
}
583

584
// metadata returns CLI's metadata.
585
func (c CLI) metadata() plugin.CLIMetadata {
38✔
586
        return plugin.CLIMetadata{
38✔
587
                CommandName: c.commandName,
38✔
588
        }
38✔
589
}
38✔
590

591
// Run executes the CLI utility.
592
//
593
// If an error is found, command help and examples will be printed.
594
func (c CLI) Run() error {
1✔
595
        if err := c.cmd.Execute(); err != nil {
2✔
596
                return fmt.Errorf("error executing command: %w", err)
1✔
597
        }
1✔
598

599
        return nil
×
600
}
601

602
// Command returns the underlying root command.
603
func (c CLI) Command() *cobra.Command {
2✔
604
        return c.cmd
2✔
605
}
2✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc