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 72494a0
Show file tree
Hide file tree
Showing 2 changed files with 12 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
8 changes: 8 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,13 @@ 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.
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 72494a0

Please sign in to comment.