Skip to content

Commit

Permalink
Fix kubectl-ns plugin help text
Browse files Browse the repository at this point in the history
The plugin help text was not consistent - using `ns` in some cases and
`kubectl ns` in others:

    $ ./kubectl-ns -h
    ...
    Usage:
      ns [new-namespace] [flags]

    Examples:

            # view the current namespace in your KUBECONFIG
            kubectl ns

            ...

    Flags:
      -h, --help                           help for ns
      ...

With cobra 1.8.1 we can fix the issue by annotating the command so the
help text matches the way we use the command. With this change we can
use the executable name (kubectl-ns) as the `Use` argument, and have
`kubectl ns` in the help text.

Issues:
- Requires Cobra release including spf13/cobra#2070
- Tested using local cobra with the require fix.
  • Loading branch information
nirs committed Nov 12, 2023
1 parent a95a79c commit ec1c21e
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 17 deletions.
3 changes: 2 additions & 1 deletion staging/src/k8s.io/sample-cli-plugin/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module k8s.io/sample-cli-plugin
go 1.21.3

require (
github.com/spf13/cobra v1.7.0
github.com/spf13/cobra v1.8.0
github.com/spf13/pflag v1.0.5
k8s.io/cli-runtime v0.0.0
k8s.io/client-go v0.0.0
Expand Down Expand Up @@ -69,6 +69,7 @@ require (
)

replace (
github.com/spf13/cobra => /home/nsoffer/src/cobra
k8s.io/api => ../api
k8s.io/apimachinery => ../apimachinery
k8s.io/cli-runtime => ../cli-runtime
Expand Down
19 changes: 3 additions & 16 deletions staging/src/k8s.io/sample-cli-plugin/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions staging/src/k8s.io/sample-cli-plugin/pkg/cmd/ns.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ func NewCmdNamespace(streams genericiooptions.IOStreams) *cobra.Command {
Short: "View or set the current namespace",
Example: fmt.Sprintf(namespaceExample, "kubectl"),
SilenceUsage: true,
Annotations: map[string]string{
cobra.CommandDisplayNameAnnotation: "kubectl ns",
},
RunE: func(c *cobra.Command, args []string) error {
if err := o.Complete(c, args); err != nil {
return err
Expand Down

0 comments on commit ec1c21e

Please sign in to comment.