-
Notifications
You must be signed in to change notification settings - Fork 14
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
Update help text template to fix target usage #94
Conversation
571ad89
to
b38be7f
Compare
There was a problem hiding this 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
Signed-off-by: Marc Khouzam <[email protected]>
I've been wondering if there was a way to get rid of the go templating completely. 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. |
There was a problem hiding this 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") |
There was a problem hiding this comment.
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()
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moved
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 ") { |
There was a problem hiding this comment.
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 . }}` |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
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?
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: |
RunE: func(cmd *cobra.Command, args []string) error { | ||
fmt.Println("fetch") | ||
return nil | ||
}, |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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") | ||
|
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
365090d
to
5c0e51d
Compare
@vuil Updated the PR description with Before and After usage texts |
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. |
f88def7
to
697910b
Compare
There was a problem hiding this 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.
There was a problem hiding this 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!
* 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)
* 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)
What this PR does / why we need it
Which issue(s) this PR fixes
Fixes #
Describe testing done for PR
Manual Testing
Global Test Plugin
Before
Usage text doesn't show the tanzu prefix
After
Kubernetes Test Plugin
Before
Usage text commands are not properly indented
After
Mission Control Test Plugin
Release note
Additional information
Special notes for your reviewer