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

kubernetes-sigs / kubebuilder / 24062067765

07 Apr 2026 02:47AM UTC coverage: 80.377% (-0.2%) from 80.62%
24062067765

Pull #5587

github

vitorfloriano
feat(cli): show hint when file completion not applicable

When hitting TAB on subcommands that don't
take files as arguments, the cli will show a
hint message (ActiveHelp) on how to list flags
instead of showing file completions.
Pull Request #5587: ✨ feat(cli): Show hint message when file completion not applicable to subcommand

8 of 33 new or added lines in 5 files covered. (24.24%)

6558 of 8159 relevant lines covered (80.38%)

45.86 hits per line

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

87.8
/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
//nolint:dupl
18
package cli
19

20
import (
21
        "fmt"
22

23
        "github.com/spf13/cobra"
24

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

28
const apiErrorMsg = "failed to create API"
29

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

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

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

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

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

73
        c.applySubcommandHooks(cmd, subcommands, apiErrorMsg, false)
6✔
74

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

81
        return cmd
6✔
82
}
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