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

kubernetes-sigs / kubebuilder / 25603346581

09 May 2026 02:20PM UTC coverage: 82.316% (+0.003%) from 82.313%
25603346581

Pull #5676

github

dongjiang1989
fix golangci lint check

Signed-off-by: dongjiang <dongjiang1989@126.com>
Pull Request #5676: ✨ (go/v4): upgrade golangci-lint to v2.12.1

8 of 13 new or added lines in 6 files covered. (61.54%)

1 existing line in 1 file now uncovered.

7704 of 9359 relevant lines covered (82.32%)

73.64 hits per line

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

82.78
/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
        "strings"
25

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

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

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

41
        pluginsFlag        = "plugins"
42
        projectVersionFlag = "project-version"
43
        goPluginV4         = "go.kubebuilder.io/v4"
44
        goPluginV3         = "go.kubebuilder.io/v3"
45
        goPluginV3Alpha    = "go.kubebuilder.io/v3-alpha"
46
        goPluginV2         = "go.kubebuilder.io/v2"
47
)
48

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

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

74
        /* Internal fields */
75

76
        // Plugin keys to scaffold with.
77
        pluginKeys []string
78
        // Project version to scaffold.
79
        projectVersion config.Version
80

81
        // A filtered set of plugins that should be used by command constructors.
82
        resolvedPlugins []plugin.Plugin
83

84
        // Root command.
85
        cmd *cobra.Command
86

87
        // Underlying fs
88
        fs machinery.Filesystem
89
}
90

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

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

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

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

122
        // Write deprecation notices after all commands have been constructed.
123
        c.printDeprecationWarnings()
7✔
124

7✔
125
        return c, nil
7✔
126
}
127

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

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

148
        return c, nil
24✔
149
}
150

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

12✔
155
        var uve config.UnsupportedVersionError
12✔
156

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

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

181
        // Add the subcommands
182
        c.addSubcommands()
10✔
183

10✔
184
        return nil
10✔
185
}
186

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

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

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

207
        // Get project version and plugin info from defaults
208
        c.getInfoFromDefaults()
10✔
209

10✔
210
        return nil
10✔
211
}
212

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

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

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

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

238
        return c.getInfoFromConfig(cfg.Config())
×
239
}
240

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

12✔
247
        for i := range args {
87✔
248
                arg := args[i]
75✔
249

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

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

276
        return false
12✔
277
}
278

279
// patchProjectFileInMemoryIfNeeded updates deprecated plugin keys in the PROJECT file in place,
280
// so that users can run `kubebuilder alpha generate` even with older plugin layouts.
281
//
282
// See: https://github.com/kubernetes-sigs/kubebuilder/issues/4433
283
//
284
// This ensures the CLI can successfully load the config without failing on unsupported plugin versions.
285
func patchProjectFileInMemoryIfNeeded(fs afero.Fs, path string) error {
×
286
        type pluginReplacement struct {
×
287
                Old string
×
288
                New string
×
289
        }
×
290
        replacements := []pluginReplacement{
×
NEW
291
                {goPluginV2, goPluginV4},
×
NEW
292
                {goPluginV3, goPluginV4},
×
NEW
293
                {goPluginV3Alpha, goPluginV4},
×
294
        }
×
295

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

301
        original := string(content)
×
302
        modified := original
×
303

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

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

322
        return nil
×
323
}
324

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

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

337
        return nil
2✔
338
}
339

340
// getInfoFromFlags obtains the project version and plugin keys from flags.
341
func (c *CLI) getInfoFromFlags(hasConfigFile bool) error {
24✔
342
        // Check if --plugins is followed by --help or -h to avoid parsing help as a plugin value
24✔
343
        // This fixes: kubebuilder init --plugins --help
24✔
344
        for i := 0; i < len(os.Args)-1; i++ {
218✔
345
                if os.Args[i] == "--plugins" || os.Args[i] == "--plugins=" {
204✔
346
                        nextArg := os.Args[i+1]
10✔
347
                        if isHelpFlag(nextArg) {
12✔
348
                                // Help was requested, return early to let Cobra handle it
2✔
349
                                return nil
2✔
350
                        }
2✔
351
                }
352
        }
353

354
        // Partially parse the command line arguments
355
        fs := pflag.NewFlagSet("base", pflag.ContinueOnError)
22✔
356

22✔
357
        // Load the base command global flags
22✔
358
        fs.AddFlagSet(c.cmd.PersistentFlags())
22✔
359

22✔
360
        // If we were unable to load the project configuration, we should also accept the project version flag
22✔
361
        var projectVersionStr string
22✔
362
        if !hasConfigFile {
44✔
363
                fs.StringVar(&projectVersionStr, projectVersionFlag, "", "project version")
22✔
364
        }
22✔
365

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

22✔
370
        // Omit unknown flags to avoid parsing errors
22✔
371
        fs.ParseErrorsAllowlist = pflag.ParseErrorsAllowlist{UnknownFlags: true}
22✔
372

22✔
373
        // Parse the arguments
22✔
374
        if err := fs.Parse(os.Args[1:]); err != nil {
22✔
375
                return fmt.Errorf("could not parse flags: %w", err)
×
376
        }
×
377

378
        // If any plugin key was provided, replace those from the project configuration file
379
        if pluginKeys, err := fs.GetStringSlice(pluginsFlag); err != nil {
22✔
380
                return fmt.Errorf("invalid flag %q: %w", pluginsFlag, err)
×
381
        } else if len(pluginKeys) != 0 {
30✔
382
                // Filter out help flags that may have been incorrectly parsed as plugin values
8✔
383
                // This fixes the issue where "kubebuilder edit --plugins --help" treats --help as a plugin
8✔
384
                validPluginKeys := make([]string, 0, len(pluginKeys))
8✔
385
                helpRequested := false
8✔
386
                for _, key := range pluginKeys {
24✔
387
                        key = strings.TrimSpace(key)
16✔
388
                        // Skip help flags
16✔
389
                        if isHelpFlag(key) {
16✔
390
                                helpRequested = true
×
391
                                continue
×
392
                        }
393
                        validPluginKeys = append(validPluginKeys, key)
16✔
394
                }
395

396
                // If help was requested via --plugins flag, set the help flag to trigger Cobra's help display
397
                // This prevents command execution and shows help instead
398
                if helpRequested {
8✔
399
                        if err := fs.Set("help", "true"); err == nil {
×
400
                                return nil
×
401
                        }
×
402
                        // If setting help flag fails, still return nil to avoid validation errors
403
                        return nil
×
404
                }
405

406
                // Validate the remaining plugin keys
407
                for i, key := range validPluginKeys {
24✔
408
                        if err := plugin.ValidateKey(key); err != nil {
17✔
409
                                return fmt.Errorf("invalid plugin %q found in flags: %w", validPluginKeys[i], err)
1✔
410
                        }
1✔
411
                }
412

413
                c.pluginKeys = validPluginKeys
7✔
414
        }
415

416
        // If the project version flag was accepted but not provided keep the empty version and try to resolve it later,
417
        // else validate the provided project version
418
        if projectVersionStr != "" {
26✔
419
                if err := c.projectVersion.Parse(projectVersionStr); err != nil {
6✔
420
                        return fmt.Errorf("invalid project version flag: %w", err)
1✔
421
                }
1✔
422
        }
423

424
        return nil
20✔
425
}
426

427
// getInfoFromDefaults obtains the plugin keys, and maybe the project version from the default values
428
func (c *CLI) getInfoFromDefaults() {
14✔
429
        // Should not use default values if a plugin was already set
14✔
430
        // This checks includes the case where a project configuration file was found,
14✔
431
        // as it will always have at least one plugin key set by now
14✔
432
        if len(c.pluginKeys) != 0 {
16✔
433
                // We don't assign a default value for project version here because we may be able to
2✔
434
                // resolve the project version after resolving the plugins.
2✔
435
                return
2✔
436
        }
2✔
437

438
        // If the user provided a project version, use the default plugins for that project version
439
        if c.projectVersion.Validate() == nil {
13✔
440
                c.pluginKeys = c.defaultPlugins[c.projectVersion]
1✔
441
                return
1✔
442
        }
1✔
443

444
        // Else try to use the default plugins for the default project version
445
        if c.defaultProjectVersion.Validate() == nil {
13✔
446
                var found bool
2✔
447
                if c.pluginKeys, found = c.defaultPlugins[c.defaultProjectVersion]; found {
4✔
448
                        c.projectVersion = c.defaultProjectVersion
2✔
449
                        return
2✔
450
                }
2✔
451
        }
452

453
        // Else check if only default plugins for a project version were provided
454
        if len(c.defaultPlugins) == 1 {
16✔
455
                for projectVersion, defaultPlugins := range c.defaultPlugins {
14✔
456
                        c.pluginKeys = defaultPlugins
7✔
457
                        c.projectVersion = projectVersion
7✔
458
                        return
7✔
459
                }
7✔
460
        }
461
}
462

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

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

27✔
470
        for _, pluginKey := range c.pluginKeys {
55✔
471
                var extraErrMsg string
28✔
472

28✔
473
                plugins := make([]plugin.Plugin, 0, len(c.plugins))
28✔
474
                for _, p := range c.plugins {
295✔
475
                        plugins = append(plugins, p)
267✔
476
                }
267✔
477
                // We can omit the error because plugin keys have already been validated
478
                plugins, _ = plugin.FilterPluginsByKey(plugins, pluginKey)
28✔
479
                if knownProjectVersion {
47✔
480
                        plugins = plugin.FilterPluginsByProjectVersion(plugins, c.projectVersion)
19✔
481
                        extraErrMsg += fmt.Sprintf(" for project version %q", c.projectVersion)
19✔
482
                }
19✔
483

484
                // Plugins are often released as "unstable" (alpha/beta) versions, then upgraded to "stable".
485
                // This upgrade effectively removes a plugin, which is fine because unstable plugins are
486
                // under no support contract. However users should be notified _why_ their plugin cannot be found.
487
                if _, version := plugin.SplitKey(pluginKey); version != "" {
42✔
488
                        var ver plugin.Version
14✔
489
                        if err := ver.Parse(version); err != nil {
14✔
490
                                return fmt.Errorf("error parsing input plugin version from key %q: %w", pluginKey, err)
×
491
                        }
×
492
                        if !ver.IsStable() {
14✔
493
                                extraErrMsg += unstablePluginMsg
×
494
                        }
×
495
                }
496

497
                // Only 1 plugin can match
498
                switch len(plugins) {
28✔
499
                case 1:
19✔
500
                        c.resolvedPlugins = append(c.resolvedPlugins, plugins[0])
19✔
501
                case 0:
6✔
502
                        return fmt.Errorf("no plugin could be resolved with key %q%s", pluginKey, extraErrMsg)
6✔
503
                default:
3✔
504
                        return fmt.Errorf("ambiguous plugin %q%s", pluginKey, extraErrMsg)
3✔
505
                }
506
        }
507

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

4✔
513
                // If there is only one common supported project version, resolve to it
4✔
514
        ProjectNumberVersionSwitch:
4✔
515
                switch len(supportedProjectVersions) {
4✔
516
                case 1:
1✔
517
                        c.projectVersion = supportedProjectVersions[0]
1✔
518
                case 0:
1✔
519
                        return fmt.Errorf("no project version supported by all the resolved plugins")
1✔
520
                default:
2✔
521
                        supportedProjectVersionStrings := make([]string, 0, len(supportedProjectVersions))
2✔
522
                        for _, supportedProjectVersion := range supportedProjectVersions {
6✔
523
                                // In case one of the multiple supported versions is the default one, choose that and exit the switch
4✔
524
                                if supportedProjectVersion.Compare(c.defaultProjectVersion) == 0 {
5✔
525
                                        c.projectVersion = c.defaultProjectVersion
1✔
526
                                        break ProjectNumberVersionSwitch
1✔
527
                                }
528
                                supportedProjectVersionStrings = append(supportedProjectVersionStrings,
3✔
529
                                        fmt.Sprintf("%q", supportedProjectVersion))
3✔
530
                        }
531
                        return fmt.Errorf("ambiguous project version, resolved plugins support the following project versions: %s",
1✔
532
                                strings.Join(supportedProjectVersionStrings, ", "))
1✔
533
                }
534
        }
535

536
        return nil
16✔
537
}
538

539
// addSubcommands returns a root command with a subcommand tree reflecting the
540
// current project's state.
541
func (c *CLI) addSubcommands() {
10✔
542
        // add the alpha command if it has any subcommands enabled
10✔
543
        c.addAlphaCmd()
10✔
544

10✔
545
        // kubebuilder completion
10✔
546
        // Only add completion if requested
10✔
547
        if c.completionCommand {
11✔
548
                c.cmd.AddCommand(c.newCompletionCmd())
1✔
549
        }
1✔
550

551
        // kubebuilder create
552
        createCmd := c.newCreateCmd()
10✔
553
        // kubebuilder create api
10✔
554
        createCmd.AddCommand(c.newCreateAPICmd())
10✔
555
        createCmd.AddCommand(c.newCreateWebhookCmd())
10✔
556
        if createCmd.HasSubCommands() {
20✔
557
                c.cmd.AddCommand(createCmd)
10✔
558
        }
10✔
559

560
        // kubebuilder edit
561
        c.cmd.AddCommand(c.newEditCmd())
10✔
562

10✔
563
        // kubebuilder init
10✔
564
        c.cmd.AddCommand(c.newInitCmd())
10✔
565

10✔
566
        // kubebuilder version
10✔
567
        // Only add version if a version string was provided
10✔
568
        if c.version != "" {
11✔
569
                c.cmd.AddCommand(c.newVersionCmd())
1✔
570
        }
1✔
571
}
572

573
// addExtraCommands adds the additional commands.
574
func (c *CLI) addExtraCommands() error {
9✔
575
        for _, cmd := range c.extraCommands {
11✔
576
                for _, subCmd := range c.cmd.Commands() {
10✔
577
                        if cmd.Name() == subCmd.Name() {
9✔
578
                                return fmt.Errorf("command %q already exists", cmd.Name())
1✔
579
                        }
1✔
580
                }
581
                c.cmd.AddCommand(cmd)
1✔
582
        }
583
        return nil
8✔
584
}
585

586
// printDeprecationWarnings prints the deprecation warnings of the resolved plugins.
587
func (c CLI) printDeprecationWarnings() {
7✔
588
        for _, p := range c.resolvedPlugins {
12✔
589
                if p != nil && p.(plugin.Deprecated) != nil && len(p.(plugin.Deprecated).DeprecationWarning()) > 0 {
6✔
590
                        _, _ = fmt.Fprintf(os.Stderr, noticeColor, fmt.Sprintf(deprecationFmt, p.(plugin.Deprecated).DeprecationWarning()))
1✔
591
                }
1✔
592
        }
593
}
594

595
// metadata returns CLI's metadata.
596
func (c CLI) metadata() plugin.CLIMetadata {
26✔
597
        return plugin.CLIMetadata{
26✔
598
                CommandName: c.commandName,
26✔
599
        }
26✔
600
}
26✔
601

602
// Run executes the CLI utility.
603
//
604
// If an error is found, command help and examples will be printed.
605
func (c CLI) Run() error {
1✔
606
        if err := c.cmd.Execute(); err != nil {
2✔
607
                // Don't return error if help was displayed (from --plugins --help pattern)
1✔
608
                if err == errHelpDisplayed {
1✔
609
                        return nil
×
610
                }
×
611
                return fmt.Errorf("error executing command: %w", err)
1✔
612
        }
613

614
        return nil
×
615
}
616

617
// Command returns the underlying root command.
618
func (c CLI) Command() *cobra.Command {
2✔
619
        return c.cmd
2✔
620
}
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