Skip to content

Commit

Permalink
Deprecate CmdTemplate and introduce private cmdTemplate variable and …
Browse files Browse the repository at this point in the history
…update all usages
  • Loading branch information
mpanchajanya committed Sep 14, 2023
1 parent b8f20cb commit 64f846b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
2 changes: 1 addition & 1 deletion plugin/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func newRootCmd(descriptor *PluginDescriptor) *cobra.Command {
},
}
cobra.AddTemplateFuncs(TemplateFuncs)
cmd.SetUsageTemplate(CmdTemplate)
cmd.SetUsageTemplate(cmdTemplate)

cmd.AddCommand(
newDescribeCmd(descriptor.Description),
Expand Down
30 changes: 28 additions & 2 deletions plugin/usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,41 @@ import (

// UsageFunc is the usage func for a plugin.
var UsageFunc = func(c *cobra.Command) error {
t, err := template.New("usage").Funcs(TemplateFuncs).Parse(CmdTemplate)
t, err := template.New("usage").Funcs(TemplateFuncs).Parse(cmdTemplate)
if err != nil {
return err
}
return t.Execute(os.Stdout, c)
}

// CmdTemplate is the template for plugin commands.
const CmdTemplate = `{{ printHelp . }}`
// Deprecated: This variable is deprecated.
const CmdTemplate = `{{ bold "Usage:" }}
{{if .Runnable}}{{ $target := index .Annotations "target" }}{{ if or (eq $target "kubernetes") (eq $target "k8s") }}tanzu {{.UseLine}}{{ end }}{{ if and (ne $target "global") (ne $target "") }}tanzu {{ $target }} {{ else }} {{ end }}{{.UseLine}}{{end}}{{if .HasAvailableSubCommands}}{{ $target := index .Annotations "target" }}{{ if or (eq $target "kubernetes") (eq $target "k8s") }}tanzu {{.CommandPath}} [command]{{end}}{{ if and (ne $target "global") (ne $target "") }}tanzu {{ $target }} {{ else }} {{ end }}{{.CommandPath}} [command]{{end}}{{if gt (len .Aliases) 0}}
{{ bold "Aliases:" }}
{{.NameAndAliases}}{{end}}{{if .HasExample}}
{{ bold "Examples:" }}
{{.Example}}{{end}}{{if .HasAvailableSubCommands}}
{{ bold "Available Commands:" }}{{range .Commands}}{{if .IsAvailableCommand }}
{{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableLocalFlags}}
{{ bold "Flags:" }}
{{.LocalFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasAvailableInheritedFlags}}
{{ bold "Global Flags:" }}
{{.InheritedFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasHelpSubCommands}}
{{ bold "Additional help topics:" }}{{range .Commands}}{{if .IsAdditionalHelpTopicCommand}}
{{rpad .CommandPath .CommandPathPadding}} {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableSubCommands}}
{{ $target := index .Annotations "target" }}{{ if or (eq $target "kubernetes") (eq $target "k8s") }}Use "{{if beginsWith .CommandPath "tanzu "}}{{.CommandPath}}{{else}}tanzu {{.CommandPath}}{{end}} [command] --help" for more information about a command.{{end}}Use "{{if beginsWith .CommandPath "tanzu "}}{{.CommandPath}}{{else}}tanzu{{ $target := index .Annotations "target" }}{{ if and (ne $target "global") (ne $target "") }} {{ $target }} {{ else }} {{ end }}{{.CommandPath}}{{end}} [command] --help" for more information about a command.{{end}}
`

// cmdTemplate is the template for plugin commands.
const cmdTemplate = `{{ printHelp . }}`

// Constants for help text labels
const (
Expand Down

0 comments on commit 64f846b

Please sign in to comment.