Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom usage template for cobra command so that usage will display 'kubectl krew' instead of just 'krew' #547

Merged
merged 1 commit into from
Mar 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Comment on lines +14 to +16
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@brianpursley I just noticed this.

Ideally we don't wanna have editor-specific stuff here.

You should add these to your ~/.gitignore_global file and set git global config core.excludesfile=~/.gitignore_global

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry about that. I will make a PR to clean it up :)


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)
}
}
}