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'

See issue #521 for more details
  • Loading branch information
brianpursley committed Mar 13, 2020
1 parent e4b2528 commit b5e8a11
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
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: 8 additions & 1 deletion 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 @@ -58,7 +59,7 @@ var (

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "krew",
Use: "krew", // This is prefixed by kubectl in the custom usage template
Short: "krew is the kubectl plugin manager",
Long: `krew is the kubectl plugin manager.
You can invoke krew through kubectl: "kubectl krew [command]..."`,
Expand Down Expand Up @@ -98,6 +99,12 @@ 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.
rootCmd.SetUsageTemplate(strings.NewReplacer(
"{{.UseLine}}", "kubectl {{.UseLine}}",
"{{.CommandPath}}", "kubectl {{.CommandPath}}").Replace(rootCmd.UsageTemplate()))
}

func preRun(cmd *cobra.Command, _ []string) error {
Expand Down
35 changes: 35 additions & 0 deletions cmd/krew/cmd/root_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright 2020 The Kubernetes Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd

import (
"strings"
"testing"
)

func TestUsageTemplateContainsReplacements(t *testing.T) {
usageTemplate := rootCmd.UsageTemplate()

expectedStrings := []string{
"kubectl {{.CommandPath}}",
"kubectl {{.UseLine}}",
}

for _, expectedString := range expectedStrings {
if !strings.Contains(usageTemplate, expectedString) {
t.Errorf("expected usage template to contain '%s' but it did not:\n%v", expectedString, usageTemplate)
}
}
}

0 comments on commit b5e8a11

Please sign in to comment.