Skip to content

Commit

Permalink
Add custom usage template for cobra command so that usage will displa…
Browse files Browse the repository at this point in the history
…y 'kubectl krew' instead of just 'krew'
  • Loading branch information
brianpursley committed Mar 11, 2020
1 parent e4b2528 commit 9df7ad7
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Files generated by JetBrains IDEs, e.g. IntelliJ IDEA
.idea/
*.iml

out/
build/
coverage.txt
9 changes: 9 additions & 0 deletions cmd/krew/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"fmt"
"math/rand"
"os"
"strings"
"time"

"github.com/fatih/color"
Expand Down Expand Up @@ -98,6 +99,14 @@ func init() {
}

paths = environment.MustGetKrewPaths()

// Cobra doesn't have a way to specify a two word command (ie. "kubectl krew"), so set a custom usage template
// with kubectl in it. Cobra will use this template for the root and all child commands.
// See issue #521 for more details on the reason for this: https://github.com/kubernetes-sigs/krew/issues/521
customUsageTemplate := rootCmd.UsageTemplate()
customUsageTemplate = strings.ReplaceAll(customUsageTemplate, "{{.UseLine}}", "kubectl {{.UseLine}}")
customUsageTemplate = strings.ReplaceAll(customUsageTemplate, "{{.CommandPath}}", "kubectl {{.CommandPath}}")
rootCmd.SetUsageTemplate(customUsageTemplate)
}

func preRun(cmd *cobra.Command, _ []string) error {
Expand Down

0 comments on commit 9df7ad7

Please sign in to comment.