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

Update help text template to fix target usage #94

Merged
merged 13 commits into from
Sep 15, 2023

Conversation

mpanchajanya
Copy link
Contributor

@mpanchajanya mpanchajanya commented Sep 13, 2023

What this PR does / why we need it

Which issue(s) this PR fixes

Fixes #

Describe testing done for PR

  • Added unit tests for each plugin target i.e global, k8s, tmc

Manual Testing

Global Test Plugin

Before
Usage text doesn't show the tanzu prefix

mpanchajanya@mpanchajanF09MK ~/v/r/tanzu-cli (main)> tanzu test -h
Test the CLI

Usage:
   test [command]

Aliases:
  test, t

Available Commands:
  fetch         Fetch the plugin tests
  push          Push the plugin tests

Flags:
  -h, --help   help for test

Additional help topics:
  test plugin        Plugin tests

Use "tanzu test [command] --help" for more information about a command.
mpanchajanya@mpanchajanF09MK ~/v/r/tanzu-cli (main)> tanzu test fetch -h
Fetch the plugin tests

Usage:
   test fetch [flags]

Flags:
  -h, --help           help for fetch
  -l, --local string   path to local repository
mpanchajanya@mpanchajanF09MK ~/v/r/tanzu-cli (main)>

After

mpanchajanya@mpanchajanF09MK ~/v/r/tanzu-cli (main)> tanzu plugin list
Standalone Plugins
  NAME  DESCRIPTION   TARGET  VERSION     STATUS
  test  Test the CLI  global  v1.1.0-dev  installed
mpanchajanya@mpanchajanF09MK ~/v/r/tanzu-cli (main)> tanzu test -h
Test the CLI

Usage:
  tanzu test [command]

Aliases:
  test, t

Available Commands:
  fetch         Fetch the plugin tests
  push          Push the plugin tests

Flags:
  -h, --help   help for test

Additional help topics:
  test plugin        Plugin tests

Use "tanzu test [command] --help" for more information about a command.
mpanchajanya@mpanchajanF09MK ~/v/r/tanzu-cli (main)> tanzu test fetch -h
Fetch the plugin tests

Usage:
  tanzu test fetch [flags]

Flags:
  -h, --help           help for fetch
  -l, --local string   path to local repository
mpanchajanya@mpanchajanF09MK ~/v/r/tanzu-cli (main)>

Kubernetes Test Plugin

Before

Usage text commands are not properly indented

mpanchajanya@mpanchajanF09MK ~/v/r/tanzu-cli (main) [1]> tanzu test -h
Test the CLI

Usage:
  tanzu test [command]tanzu kubernetes test [command]

Aliases:
  test, t

Available Commands:
  fetch         Fetch the plugin tests
  push          Push the plugin tests

Flags:
  -h, --help   help for test

Additional help topics:
  test plugin        Plugin tests

Use "tanzu test [command] --help" for more information about a command.Use "tanzu kubernetes test [command] --help" for more information about a command.
mpanchajanya@mpanchajanF09MK ~/v/r/tanzu-cli (main)> tanzu test fetch -h
Fetch the plugin tests

Usage:
  tanzu test fetch [flags]tanzu kubernetes test fetch [flags]

Flags:
  -h, --help           help for fetch
  -l, --local string   path to local repository
mpanchajanya@mpanchajanF09MK ~/v/r/tanzu-cli (main)> tanzu k8s test -h
Test the CLI

Usage:
  tanzu test [command]tanzu kubernetes test [command]

Aliases:
  test, t

Available Commands:
  fetch         Fetch the plugin tests
  push          Push the plugin tests

Flags:
  -h, --help   help for test

Additional help topics:
  test plugin        Plugin tests

Use "tanzu test [command] --help" for more information about a command.Use "tanzu kubernetes test [command] --help" for more information about a command.
mpanchajanya@mpanchajanF09MK ~/v/r/tanzu-cli (main)> tanzu k8s test fetch -h
Fetch the plugin tests

Usage:
  tanzu test fetch [flags]tanzu kubernetes test fetch [flags]

Flags:
  -h, --help           help for fetch
  -l, --local string   path to local repository
mpanchajanya@mpanchajanF09MK ~/v/r/tanzu-cli (main)>

After

mpanchajanya@mpanchajanF09MK ~/v/r/tanzu-cli (main)> tanzu plugin list
Standalone Plugins
  NAME  DESCRIPTION   TARGET      VERSION     STATUS
  test  Test the CLI  kubernetes  v1.1.0-dev  installed
mpanchajanya@mpanchajanF09MK ~/v/r/tanzu-cli (main)> tanzu test -h
Test the CLI

Usage:
  tanzu test [command]
  tanzu kubernetes test [command]

Aliases:
  test, t

Available Commands:
  fetch         Fetch the plugin tests
  push          Push the plugin tests

Flags:
  -h, --help   help for test

Additional help topics:
  test plugin        Plugin tests

Use "tanzu test [command] --help" for more information about a command.
Use "tanzu kubernetes test [command] --help" for more information about a command.
mpanchajanya@mpanchajanF09MK ~/v/r/tanzu-cli (main)> tanzu test fetch -h
Fetch the plugin tests

Usage:
  tanzu test fetch [flags]
  tanzu kubernetes test fetch [flags]

Flags:
  -h, --help           help for fetch
  -l, --local string   path to local repository
mpanchajanya@mpanchajanF09MK ~/v/r/tanzu-cli (main)> tanzu k8s test -h
Test the CLI

Usage:
  tanzu test [command]
  tanzu kubernetes test [command]

Aliases:
  test, t

Available Commands:
  fetch         Fetch the plugin tests
  push          Push the plugin tests

Flags:
  -h, --help   help for test

Additional help topics:
  test plugin        Plugin tests

Use "tanzu test [command] --help" for more information about a command.
Use "tanzu kubernetes test [command] --help" for more information about a command.
mpanchajanya@mpanchajanF09MK ~/v/r/tanzu-cli (main)> tanzu k8s test fetch -h
Fetch the plugin tests

Usage:
  tanzu test fetch [flags]
  tanzu kubernetes test fetch [flags]

Flags:
  -h, --help           help for fetch
  -l, --local string   path to local repository
mpanchajanya@mpanchajanF09MK ~/v/r/tanzu-cli (main)>

Mission Control Test Plugin

mpanchajanya@mpanchajanF09MK ~/v/r/tanzu-cli (main)> tanzu plugin list
Standalone Plugins
  NAME  DESCRIPTION   TARGET           VERSION     STATUS
  test  Test the CLI  mission-control  v1.1.0-dev  installed
mpanchajanya@mpanchajanF09MK ~/v/r/tanzu-cli (main) [1]> tanzu tmc test -h
Test the CLI

Usage:
  tanzu mission-control test [command]

Aliases:
  test, t

Available Commands:
  fetch         Fetch the plugin tests
  push          Push the plugin tests

Flags:
  -h, --help   help for test

Additional help topics:
  test plugin        Plugin tests

Use "tanzu mission-control test [command] --help" for more information about a command.
mpanchajanya@mpanchajanF09MK ~/v/r/tanzu-cli (main)> tanzu tmc test fetch -h
Fetch the plugin tests

Usage:
  tanzu mission-control test fetch [flags]

Flags:
  -h, --help           help for fetch
  -l, --local string   path to local repository
mpanchajanya@mpanchajanF09MK ~/v/r/tanzu-cli (main)>

Release note

Fix plugin help text for global, kubernetes, mission-control targets

Additional information

Special notes for your reviewer

@mpanchajanya mpanchajanya marked this pull request as ready for review September 13, 2023 02:53
@mpanchajanya mpanchajanya requested a review from a team as a code owner September 13, 2023 02:53
Copy link
Contributor

@marckhouzam marckhouzam left a comment

Choose a reason for hiding this comment

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

This is much more readable! Nice use of {{- -}} 👏

There is an extra empty line at the very end of the help for commands that are runnable:

$ tz test fetch -h
Fetch the plugin tests

Usage:
  tanzu test fetch [flags]

Flags:
  -h, --help           help for fetch
  -l, --local string   path to local repository

$ # Notice the new extra line before this new prompt

plugin/usage.go Outdated Show resolved Hide resolved
plugin/usage.go Outdated Show resolved Hide resolved
plugin/usage.go Outdated Show resolved Hide resolved
plugin/usage.go Outdated Show resolved Hide resolved
@marckhouzam
Copy link
Contributor

I've been wondering if there was a way to get rid of the go templating completely.
After some digging I came up with a way to stop using go templates but instead use plain Go code to generate the help.

After discussing it with @mpanchajanya we decided to add this change on top of this PR.

@mpanchajanya
Copy link
Contributor Author

I've been wondering if there was a way to get rid of the go templating completely. After some digging I came up with a way to stop using go templates but instead use plain Go code to generate the help.

After discussing it with @mpanchajanya we decided to add this change on top of this PR.

Thanks @marckhouzam for the go implementation alternate to string template. This is way better.
I have refactored the printHelp to be more efficient and avoid lint issues.

Copy link
Contributor

@marckhouzam marckhouzam left a comment

Choose a reason for hiding this comment

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

Looks super nice!

plugin/usage.go Outdated

{{ bold "Available Commands:" }}{{range .Commands}}{{if .IsAvailableCommand }}
{{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableLocalFlags}}
output.WriteString(component.Bold(usageStr) + "\n")
Copy link
Contributor

Choose a reason for hiding this comment

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

Since you've done such a nice encapsulation job, making this function nicely readable, how about moving this line to the formatUsageHelpSection()?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Moved

plugin/usage.go Show resolved Hide resolved
plugin/usage.go Outdated
// For kubernetes, k8s, global, or no target display tanzu command path without target
if target == types.TargetK8s || target == types.TargetGlobal || target == types.TargetUnknown {
footer.WriteString(`Use "`)
if !strings.HasSuffix(cmd.CommandPath(), "tanzu ") {
Copy link
Contributor

Choose a reason for hiding this comment

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

Woops, this is a typo from my own commit. HasSuffix() should actually be HasPrefix() here and a couple of lines below.

plugin/usage.go Outdated
@@ -22,32 +24,142 @@ 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 = `{{ printHelp . }}`
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't feel like this change has any backwards-compatibility issues, but I'm not sure.
Any opinions here?

Copy link
Contributor Author

@mpanchajanya mpanchajanya Sep 13, 2023

Choose a reason for hiding this comment

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

I feel like this change is backward compatible since we are using the same constant and instead of string template we refactored the template generation into a go func that returns template.

@vuil @anujc25 can add any opinions here

Copy link
Contributor

Choose a reason for hiding this comment

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

@marckhouzam you mean as in the fact that the output has changed in general, or something more specific?

@vuil
Copy link
Contributor

vuil commented Sep 13, 2023

Nit: @mpanchajanya it is really hard to tell after the changes how much/little of the output has changed. It would help the review if:
in the PR description's command help outputs, either highlight what is added/deleted/modified or just include a diff of the outputs before vs after the changes.

RunE: func(cmd *cobra.Command, args []string) error {
fmt.Println("fetch")
return nil
},
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you add Examples here and then test using fetch -h?
It will test the examples printout but also the case where the command is Runnable, which I don't believe we test

Copy link
Contributor Author

@mpanchajanya mpanchajanya Sep 13, 2023

Choose a reason for hiding this comment

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

added tests to validate fetch --help cmd.


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

Copy link
Contributor

Choose a reason for hiding this comment

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

Could you add a global flag by adding a persistent flag to the top command so we can test that case

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added tests with global flags

@mpanchajanya
Copy link
Contributor Author

Nit: @mpanchajanya it is really hard to tell after the changes how much/little of the output has changed. It would help the review if: in the PR description's command help outputs, either highlight what is added/deleted/modified or just include a diff of the outputs before vs after the changes.

@vuil Updated the PR description with Before and After usage texts

@vuil
Copy link
Contributor

vuil commented Sep 13, 2023

Nit: @mpanchajanya it is really hard to tell after the changes how much/little of the output has changed. It would help the review if: in the PR description's command help outputs, either highlight what is added/deleted/modified or just include a diff of the outputs before vs after the changes.

The other related thing I wanted to add but forgot is: If addressing an issue involves essentially porting from one implementation to another (template-based logic vs straight golang code are two quite different things), I would strongly encourage we break up the change into a straight port, have existing (or even new tests) tests confirms that there is no observable behavior change, then proceed with another commit to apply the actual fix.

@mpanchajanya
Copy link
Contributor Author

Nit: @mpanchajanya it is really hard to tell after the changes how much/little of the output has changed. It would help the review if: in the PR description's command help outputs, either highlight what is added/deleted/modified or just include a diff of the outputs before vs after the changes.

The other related thing I wanted to add but forgot is: If addressing an issue involves essentially porting from one implementation to another (template-based logic vs straight golang code are two quite different things), I would strongly encourage we break up the change into a straight port, have existing (or even new tests) tests confirms that there is no observable behavior change, then proceed with another commit to apply the actual fix.

There are no automated tests earlier to confirm the actual change. Its was all manual tests.
With the new tests added yes I have validated the previous and new help text outputs

Copy link
Contributor

@marckhouzam marckhouzam left a comment

Choose a reason for hiding this comment

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

LGTM
Thanks for the great tests and working out all the kinks.

Copy link
Contributor

@vuil vuil left a comment

Choose a reason for hiding this comment

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

LGTM, thanks for the fixes and tests!

@mpanchajanya mpanchajanya merged commit 3923e33 into vmware-tanzu:main Sep 15, 2023
4 checks passed
mpanchajanya added a commit to mpanchajanya/tanzu-plugin-runtime that referenced this pull request Sep 15, 2023
* Update help text template to fix target usage

* Add two spaces for tanzu command

* Convert the help template to go code

Signed-off-by: Marc Khouzam <[email protected]>

* testing the lint and tests

* refactor the print help

* refactor the indentation

* Update check to HasPrefix from HasSuffix

* Update tests to include global flags and fetch --help command

* Update additional help topics section with tanzu and target prefix

* Revert additional help topics targeted prefix

* Deprecate CmdTemplate and introduce private cmdTemplate variable and update all usages

* Fix sub command unit tests by executing plugin instead of command

* Fix spacing in examples section and update tests

---------

Signed-off-by: Marc Khouzam <[email protected]>
Co-authored-by: Marc Khouzam <[email protected]>
(cherry picked from commit 3923e33)
mpanchajanya added a commit that referenced this pull request Sep 15, 2023
* Update help text template to fix target usage

* Add two spaces for tanzu command

* Convert the help template to go code

Signed-off-by: Marc Khouzam <[email protected]>

* testing the lint and tests

* refactor the print help

* refactor the indentation

* Update check to HasPrefix from HasSuffix

* Update tests to include global flags and fetch --help command

* Update additional help topics section with tanzu and target prefix

* Revert additional help topics targeted prefix

* Deprecate CmdTemplate and introduce private cmdTemplate variable and update all usages

* Fix sub command unit tests by executing plugin instead of command

* Fix spacing in examples section and update tests

---------

Signed-off-by: Marc Khouzam <[email protected]>
Co-authored-by: Marc Khouzam <[email protected]>
(cherry picked from commit 3923e33)
@marckhouzam marckhouzam added this to the v1.0.2 milestone Oct 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants