Skip to content

Commit

Permalink
Update help text template to fix target usage
Browse files Browse the repository at this point in the history
  • Loading branch information
mpanchajanya committed Sep 13, 2023
1 parent 15f5bef commit b38be7f
Show file tree
Hide file tree
Showing 2 changed files with 250 additions and 16 deletions.
86 changes: 75 additions & 11 deletions plugin/usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,92 @@ var UsageFunc = func(c *cobra.Command) error {
}

// CmdTemplate is the template for plugin commands.
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}}
const CmdTemplate = `
{{- bold "Usage:" -}}
{{- if .Runnable -}}
{{- $target := index .Annotations "target" -}}
{{- /* For kubernetes, k8s, global, or no target display tanzu command path without target*/ -}}
{{- if or (eq $target "kubernetes") (eq $target "k8s") (eq $target "global") (eq $target "") }}
tanzu {{.UseLine}}
{{- end -}}
{{- /* For non global, or no target display tanzu command path with target*/ -}}
{{- if and (ne $target "global") (ne $target "") }}
tanzu {{ $target }} {{.UseLine}}
{{- end -}}
{{- print "\n" -}}
{{- end -}}
{{- if .HasAvailableSubCommands -}}
{{- $target := index .Annotations "target" -}}
{{- /* For kubernetes, k8s, global, or no target display tanzu command path without target*/ -}}
{{- if or (eq $target "kubernetes") (eq $target "k8s") (eq $target "global") (eq $target "") }}
tanzu {{.CommandPath}} [command]
{{- end -}}
{{- /* For non global, or no target display tanzu command path with target*/ -}}
{{- if and (ne $target "global") (ne $target "") }}
tanzu {{ $target }} {{.CommandPath}} [command]
{{- end -}}
{{- print "\n" -}}
{{- end -}}
{{- /* Display Aliases for the plugin if specified*/ -}}
{{ if gt (len .Aliases) 0 }}
{{ bold "Aliases:" }}
{{.NameAndAliases}}{{end}}{{if .HasExample}}
{{.NameAndAliases}}
{{- print "\n" -}}
{{- end -}}
{{- /* Display Examples for the plugin if specified*/ -}}
{{ if .HasExample }}
{{ bold "Examples:" }}
{{.Example}}{{end}}{{if .HasAvailableSubCommands}}
{{.Example}}
{{- print "\n" -}}
{{- end -}}
{{ bold "Available Commands:" }}{{range .Commands}}{{if .IsAvailableCommand }}
{{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableLocalFlags}}
{{- /* Display Available Commands for the plugin*/ -}}
{{ if .HasAvailableSubCommands }}
{{ bold "Available Commands:" }}{{range .Commands}}
{{- if .IsAvailableCommand }}
{{rpad .Name .NamePadding }} {{.Short}}
{{- end -}}
{{- end -}}
{{- print "\n" -}}
{{- end -}}
{{- /* Display Flags of the plugin*/ -}}
{{ if .HasAvailableLocalFlags}}
{{ bold "Flags:" }}
{{.LocalFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasAvailableInheritedFlags}}
{{.LocalFlags.FlagUsages | trimTrailingWhitespaces}}
{{- print "\n" -}}
{{- end -}}
{{- /* Display Global Flags of the plugin*/ -}}
{{ if .HasAvailableInheritedFlags}}
{{ bold "Global Flags:" }}
{{.InheritedFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasHelpSubCommands}}
{{.InheritedFlags.FlagUsages | trimTrailingWhitespaces}}
{{- print "\n" -}}
{{- end -}}
{{ bold "Additional help topics:" }}{{range .Commands}}{{if .IsAdditionalHelpTopicCommand}}
{{rpad .CommandPath .CommandPathPadding}} {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableSubCommands}}
{{- /* Display Additional help topics of the plugin*/ -}}
{{ if .HasHelpSubCommands}}
{{ bold "Additional help topics:" }}{{range .Commands}}
{{- if .IsAdditionalHelpTopicCommand}}
{{rpad .CommandPath .CommandPathPadding}} {{.Short}}
{{- end -}}
{{- end -}}
{{- print "\n" -}}
{{- end -}}
{{ $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}}
{{ if .HasAvailableSubCommands}}
{{- $target := index .Annotations "target" }}
{{- if or (eq $target "kubernetes") (eq $target "k8s") (eq $target "global") (eq $target "") }}
Use "{{- if beginsWith .CommandPath "tanzu "}}{{.CommandPath}}{{- else}}tanzu {{.CommandPath}}{{- end}} [command] --help" for more information about a command.
{{- end -}}
{{- if and (ne $target "global") (ne $target "") }}
Use "{{- if beginsWith .CommandPath "tanzu "}}{{.CommandPath}}{{- else}}tanzu {{ $target }} {{.CommandPath}}{{- end}} [command] --help" for more information about a command.
{{- end -}}
{{- end }}
`

// TemplateFuncs are the template usage funcs.
Expand Down
180 changes: 175 additions & 5 deletions plugin/usage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,69 @@
package plugin

import (
"fmt"
"os"
"testing"

"github.com/spf13/cobra"

"github.com/stretchr/testify/assert"

"github.com/vmware-tanzu/tanzu-plugin-runtime/config/types"
)

func TestUsageFunc(t *testing.T) {
assert := assert.New(t)
func SampleTestCommand(t *testing.T, target types.Target) *cobra.Command {
var pluginsCmd = &cobra.Command{
Use: "plugin",
Short: "Plugin tests",
}

var fetchCmd = &cobra.Command{
Use: "fetch",
Short: "Fetch the plugin tests",
RunE: func(cmd *cobra.Command, args []string) error {
fmt.Println("fetch")
return nil
},
}

var pushCmd = &cobra.Command{
Use: "push",
Short: "Push the plugin tests",
RunE: func(cmd *cobra.Command, args []string) error {
fmt.Println("push")
return nil
},
}

var descriptor = PluginDescriptor{
Name: "test",
Target: target,
Aliases: []string{"tt", "ttt"},
Description: "Test the CLI",
Group: AdminCmdGroup,
Version: "v1.1.0",
BuildSHA: "1234567",
}

var local string

fetchCmd.Flags().StringVarP(&local, "local", "l", "", "path to local repository")
_ = fetchCmd.MarkFlagRequired("local")

p, err := NewPlugin(&descriptor)
assert.Nil(t, err)

p.AddCommands(
fetchCmd,
pushCmd,
pluginsCmd,
)

return p.Cmd
}

func TestUsageFunc(t *testing.T) {
r, w, err := os.Pipe()
if err != nil {
t.Error(err)
Expand All @@ -36,9 +89,126 @@ func TestUsageFunc(t *testing.T) {
Short: "Sub1 description",
}
err = UsageFunc(cmd)
w.Close()
assert.Nil(err)
assert.Nil(t, err)

err = w.Close()
assert.Nil(t, err)

got := <-c
assert.Contains(string(got), "Usage:")
assert.Contains(t, string(got), "Usage:")
}

func TestUsageFuncWithKubernetesTargetPlugin(t *testing.T) {
r, w, err := os.Pipe()
if err != nil {
t.Error(err)
}
c := make(chan []byte)
go readOutput(t, r, c)

// Set up for our test
stdout := os.Stdout
stderr := os.Stderr
defer func() {
os.Stdout = stdout
os.Stderr = stderr
}()
os.Stdout = w
os.Stderr = w

cmd := SampleTestCommand(t, types.TargetK8s)
err = UsageFunc(cmd)
assert.Nil(t, err)
err = w.Close()
assert.Nil(t, err)

got := string(<-c)

// Check for various segments in the output
assert.Contains(t, got, "Usage:")
assert.Contains(t, got, "tanzu test [command]")
assert.Contains(t, got, "tanzu kubernetes test [command]")
assert.Contains(t, got, "Available Commands:")
assert.Contains(t, got, "fetch Fetch the plugin tests")
assert.Contains(t, got, "push Push the plugin tests")
assert.Contains(t, got, "Additional help topics:")
assert.Contains(t, got, "test plugin Plugin tests")
assert.Contains(t, got, `Use "tanzu test [command] --help" for more information about a command.`)
assert.Contains(t, got, `Use "tanzu kubernetes test [command] --help" for more information about a command.`)
}

func TestUsageFuncWithGlobalTargetPlugin(t *testing.T) {
r, w, err := os.Pipe()
if err != nil {
t.Error(err)
}
c := make(chan []byte)
go readOutput(t, r, c)

// Set up for our test
stdout := os.Stdout
stderr := os.Stderr
defer func() {
os.Stdout = stdout
os.Stderr = stderr
}()
os.Stdout = w
os.Stderr = w

cmd := SampleTestCommand(t, types.TargetGlobal)
err = UsageFunc(cmd)
assert.Nil(t, err)

err = w.Close()
assert.Nil(t, err)

got := string(<-c)

// Check for various segments in the output
assert.Contains(t, got, "Usage:")
assert.Contains(t, got, "tanzu test [command]")
assert.Contains(t, got, "Available Commands:")
assert.Contains(t, got, "fetch Fetch the plugin tests")
assert.Contains(t, got, "push Push the plugin tests")
assert.Contains(t, got, "Additional help topics:")
assert.Contains(t, got, "test plugin Plugin tests")
assert.Contains(t, got, `Use "tanzu test [command] --help" for more information about a command.`)
}

func TestUsageFuncWithTMCTargetPlugin(t *testing.T) {
r, w, err := os.Pipe()
if err != nil {
t.Error(err)
}
c := make(chan []byte)
go readOutput(t, r, c)

// Set up for our test
stdout := os.Stdout
stderr := os.Stderr
defer func() {
os.Stdout = stdout
os.Stderr = stderr
}()
os.Stdout = w
os.Stderr = w

cmd := SampleTestCommand(t, types.TargetTMC)
err = UsageFunc(cmd)
assert.Nil(t, err)

err = w.Close()
assert.Nil(t, err)

got := string(<-c)

// Check for various segments in the output
assert.Contains(t, got, "Usage:")
assert.Contains(t, got, "tanzu mission-control test [command]")
assert.Contains(t, got, "Available Commands:")
assert.Contains(t, got, "fetch Fetch the plugin tests")
assert.Contains(t, got, "push Push the plugin tests")
assert.Contains(t, got, "Additional help topics:")
assert.Contains(t, got, "test plugin Plugin tests")
assert.Contains(t, got, `Use "tanzu mission-control test [command] --help" for more information about a command.`)
}

0 comments on commit b38be7f

Please sign in to comment.