Skip to content

Commit

Permalink
feat: adjust help when used as kubectl plugin
Browse files Browse the repository at this point in the history
Change the usage message in help output to include the binary name the
command was invoked as, and also adjust it to match kubectl plugin
conventions when the binary name starts with `kubectl-`.

closes #187
  • Loading branch information
mig4 committed Oct 20, 2019
1 parent 8d013ec commit e473b38
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
8 changes: 8 additions & 0 deletions acceptance.bats
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,11 @@
[ "$status" -eq 0 ]
[ "$output" = "PASS - fixtures/valid.yaml contains a valid ReplicationController" ]
}

@test "Adjusts help string when invoked as a kubectl plugin" {
ln -s kubeval bin/kubectl-kubeval

run bin/kubectl-kubeval --help
[ "$status" -eq 0 ]
[[ ${lines[2]} == " kubectl kubeval <file>"* ]]
}
6 changes: 5 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ var (

// RootCmd represents the the command to run when kubeval is run
var RootCmd = &cobra.Command{
Use: "kubeval <file> [file...]",
Short: "Validate a Kubernetes YAML file against the relevant schema",
Long: `Validate a Kubernetes YAML file against the relevant schema`,
Version: fmt.Sprintf("Version: %s\nCommit: %s\nDate: %s\n", version, commit, date),
Expand Down Expand Up @@ -196,6 +195,11 @@ func Execute() {
}

func init() {
rootCmdName := filepath.Base(os.Args[0])
if strings.HasPrefix(rootCmdName, "kubectl-") {
rootCmdName = strings.Replace(rootCmdName, "-", " ", 1)
}
RootCmd.Use = fmt.Sprintf("%s <file> [file...]", rootCmdName)
kubeval.AddKubevalFlags(RootCmd, config)
RootCmd.Flags().BoolVarP(&forceColor, "force-color", "", false, "Force colored output even if stdout is not a TTY")
RootCmd.SetVersionTemplate(`{{.Version}}`)
Expand Down

0 comments on commit e473b38

Please sign in to comment.