Skip to content

Commit

Permalink
Fix plugin command usage when non-global plugin remaps the commands (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
anujc25 authored Jul 9, 2024
1 parent a6dcc47 commit 368b5a9
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 8 deletions.
24 changes: 16 additions & 8 deletions plugin/usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,11 +199,11 @@ func formatUsageHelpSection(cmd *cobra.Command, target types.Target) string {
base := indentStr + "tanzu"

if cmd.Runnable() {
if shouldPrintInvocationWithoutTarget(target) {
if shouldPrintInvocationWithoutTarget(target, ic) {
output.WriteString(buildInvocationString(base, useLineEx(cmd, ic)) + "\n")
}

if shouldPrintInvocationWithTarget(target) {
if shouldPrintInvocationWithTarget(target, ic) {
output.WriteString(buildInvocationString(base, string(target), useLineEx(cmd, ic)) + "\n")
}
}
Expand All @@ -215,11 +215,11 @@ func formatUsageHelpSection(cmd *cobra.Command, target types.Target) string {
output.WriteString("\n")
}

if shouldPrintInvocationWithoutTarget(target) {
if shouldPrintInvocationWithoutTarget(target, ic) {
output.WriteString(buildInvocationString(base, commandPathEx(cmd, ic), "[command]") + "\n")
}

if shouldPrintInvocationWithTarget(target) {
if shouldPrintInvocationWithTarget(target, ic) {
output.WriteString(buildInvocationString(base, string(target), commandPathEx(cmd, ic), "[command]") + "\n")
}
}
Expand All @@ -241,11 +241,11 @@ func formatHelpFooter(cmd *cobra.Command, target types.Target) string {
base = "Use \"tanzu"
}

if shouldPrintInvocationWithoutTarget(target) {
if shouldPrintInvocationWithoutTarget(target, ic) {
footer.WriteString(buildInvocationString(base, commandPathEx(cmd, ic), `[command] --help" for more information about a command.`+"\n"))
}

if shouldPrintInvocationWithTarget(target) {
if shouldPrintInvocationWithTarget(target, ic) {
footer.WriteString(buildInvocationString(base, string(target), commandPathEx(cmd, ic), `[command] --help" for more information about a command.`+"\n"))
}

Expand Down Expand Up @@ -377,12 +377,20 @@ var TemplateFuncs = template.FuncMap{
"beginsWith": component.BeginsWith,
}

func shouldPrintInvocationWithoutTarget(target types.Target) bool {
func shouldPrintInvocationWithoutTarget(target types.Target, ic *InvocationContext) bool {
// Do not show the target information if the InvocationContext is specified
if ic != nil && ic.invokedCommand != "" {
return true
}
// For kubernetes, k8s, global, or no target, display tanzu command path without target
return target == types.TargetK8s || target == types.TargetGlobal || target == types.TargetUnknown
}

func shouldPrintInvocationWithTarget(target types.Target) bool {
func shouldPrintInvocationWithTarget(target types.Target, ic *InvocationContext) bool {
// Do not show the target information if the InvocationContext is specified
if ic != nil && ic.invokedCommand != "" {
return false
}
// For non global, or no target display tanzu command path with target
// Also, for the deprecated invocation using the kubernetes target, no longer display the command path with the target
return target != types.TargetGlobal && target != types.TargetUnknown && target != types.TargetK8s
Expand Down
60 changes: 60 additions & 0 deletions plugin/usage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,66 @@ func TestGlobalTestFetchCommandHelpTextWithInvocationContext(t *testing.T) {

got := string(<-c)

//nolint:goconst
expected := `Fetch the plugin tests
Usage:
tanzu fetch [flags]
Examples:
sample example usage of the fetch command
Flags:
-h, --help help for fetch
-l, --local string path to local repository
-u, --url string url to remote repository
Global Flags:
-e, --env string env to test
`

assert.Equal(t, expected, got)
}

func TestOperationsTestFetchCommandHelpTextWithInvocationContext(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

os.Setenv("TANZU_CLI_COMMAND_MAPPED_FROM", "fetch")
os.Setenv("TANZU_CLI_INVOKED_COMMAND", "fetch")
os.Setenv("TANZU_CLI_INVOKED_GROUP", "")
defer func() {
os.Stdout = stdout
os.Stderr = stderr
os.Unsetenv("TANZU_CLI_COMMAND_MAPPED_FROM")
os.Unsetenv("TANZU_CLI_INVOKED_COMMAND")
os.Unsetenv("TANZU_CLI_INVOKED_GROUP")
}()
os.Stdout = w
os.Stderr = w

// Prepare the root command with Operations target
p := usageTestPlugin(t, types.TargetOperations)

p.Cmd.SetArgs([]string{"fetch", "--help"})

// Execute the command which will trigger the help
err = p.Execute()
assert.Nil(t, err)

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

got := string(<-c)

expected := `Fetch the plugin tests
Usage:
Expand Down

0 comments on commit 368b5a9

Please sign in to comment.