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

kubernetes-sigs / kubebuilder / 24630651487

19 Apr 2026 01:49PM UTC coverage: 82.313% (-0.07%) from 82.383%
24630651487

Pull #5647

github

vitorfloriano
feat(cli): mark kind and version flags as required

The kind and version are now required.This makes
completion suggest those flags when completing
the create api and create webhooks commands.

Just a nice little improvement in UX.
Pull Request #5647: ✨ feat(cli): mark resource flags as required to improve completion

8 of 17 new or added lines in 2 files covered. (47.06%)

7707 of 9363 relevant lines covered (82.31%)

73.67 hits per line

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

88.89
/pkg/cli/api.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
        "fmt"
21

22
        "github.com/spf13/cobra"
23

24
        "sigs.k8s.io/kubebuilder/v4/pkg/plugin"
25
)
26

27
const apiErrorMsg = "failed to create API"
28

29
func (c CLI) newCreateAPICmd() *cobra.Command {
10✔
30
        cmd := &cobra.Command{
10✔
31
                Use:   "api",
10✔
32
                Short: "Scaffold a Kubernetes API",
10✔
33
                Long:  `Scaffold a Kubernetes API.`,
10✔
34
                RunE: errCmdFunc(
10✔
35
                        fmt.Errorf("api subcommand requires an existing project"),
10✔
36
                ),
10✔
37
        }
10✔
38

10✔
39
        // Show hint message on how to list flags instead of showing file completion
10✔
40
        cmd.ValidArgsFunction = func(
10✔
41
                _ *cobra.Command,
10✔
42
                args []string,
10✔
43
                toComplete string,
10✔
44
        ) ([]cobra.Completion, cobra.ShellCompDirective) {
10✔
NEW
45
                completions := []cobra.Completion{}
×
NEW
46
                if len(args) == 0 && toComplete == "" {
×
NEW
47
                        completions = cobra.AppendActiveHelp(completions, "Type '--' and press TAB to list more flags")
×
NEW
48
                }
×
NEW
49
                return completions, cobra.ShellCompDirectiveNoFileComp
×
50
        }
51

52
        // In case no plugin was resolved, instead of failing the construction of the CLI, fail the execution of
53
        // this subcommand. This allows the use of subcommands that do not require resolved plugins like help.
54
        if len(c.resolvedPlugins) == 0 {
13✔
55
                cmdErr(cmd, noResolvedPluginError{})
3✔
56
                return cmd
3✔
57
        }
3✔
58

59
        // Obtain the plugin keys and subcommands from the plugins that implement plugin.CreateAPI.
60
        subcommands := c.filterSubcommands(
7✔
61
                func(p plugin.Plugin) bool {
14✔
62
                        _, isValid := p.(plugin.CreateAPI)
7✔
63
                        return isValid
7✔
64
                },
7✔
65
                func(p plugin.Plugin) plugin.Subcommand {
6✔
66
                        return p.(plugin.CreateAPI).GetCreateAPISubcommand()
6✔
67
                },
6✔
68
        )
69

70
        // Verify that there is at least one remaining plugin.
71
        if len(subcommands) == 0 {
8✔
72
                cmdErr(cmd, noAvailablePluginError{"API creation"})
1✔
73
                return cmd
1✔
74
        }
1✔
75

76
        c.applySubcommandHooks(cmd, subcommands, apiErrorMsg, false)
6✔
77

6✔
78
        // Append plugin table after metadata updates
6✔
79
        c.appendPluginTable(cmd, func(p plugin.Plugin) bool {
12✔
80
                _, isValid := p.(plugin.CreateAPI)
6✔
81
                return isValid
6✔
82
        }, "Available plugins that support 'create api'")
6✔
83

84
        return cmd
6✔
85
}
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