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

Plugins: Fail plugins installation on wrong args provided #71355

Merged
merged 5 commits into from Jul 12, 2023

Conversation

oshirohugo
Copy link
Contributor

@oshirohugo oshirohugo commented Jul 11, 2023

What is this feature?

Fail plugin installation when more args than needed are provided

Why do we need this feature?

This is needed because we use hierarchical flags and arguments and we need to feedback the user if an argument is being ignored.

Who is this feature for?

Fixes #70056

Special notes for your reviewer:

The command line package that we use to parse our flags and arguments is based on hierarchical flags. --pluginsDir is defined in the upper command (cli) to be availabel to all plugins subcommands. The library doesn't seem to support a global flag being passed after a subcommand. This is confirmed by this maintainer comment and by this comment on an open isse

Please check that:

  • It works as expected from a user's perspective.
  • If this is a pre-GA feature, it is behind a feature toggle.
  • The docs are updated, and if this is a notable improvement, it's added to our What's New doc.

@oshirohugo oshirohugo requested a review from a team as a code owner July 11, 2023 11:49
@oshirohugo oshirohugo requested review from sakjur, papagian and yangkb09 and removed request for a team July 11, 2023 11:49
@@ -20,6 +20,11 @@ import (
)

func validateInput(c utils.CommandLine, pluginFolder string) error {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

pluginFolder argument isn't used inside this function and could be removed as well

Copy link
Contributor

Choose a reason for hiding this comment

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

you can remove it then :)

@oshirohugo oshirohugo changed the title Log ignored arguments on plugin install Chore: Log ignored arguments on plugin install Jul 11, 2023
@sakjur sakjur requested review from a team, wbrowne and marefr and removed request for a team July 11, 2023 12:02
@oshirohugo oshirohugo added this to the 10.1.x milestone Jul 11, 2023
@oshirohugo oshirohugo added the no-backport Skip backport of PR label Jul 11, 2023
Copy link
Contributor

@andresmgot andresmgot left a comment

Choose a reason for hiding this comment

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

The lack of this functionality is surprising 😅 I checked it and if you copy the flag to the sub command (so you have the flag --pluginsDir in the install command), it no longer works if you specify it at the root level (so grafana cli --pluginsDir foo install bar would stop working).

Anyway, thanks for the investigation! I have some comments.

Comment on lines 23 to 26
if c.Args().Len() > 1 {
logger.Info("Install only supports one local argument\n")
showIgnoredArguments(c.Args().Slice()[1:])
}
Copy link
Contributor

Choose a reason for hiding this comment

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

IMO a better UX would be to error, the log line can be missed (and the plugin may be installed in a directory the user does not want to, for example).

Copy link
Contributor

Choose a reason for hiding this comment

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

Also, the length to check should be 2, not 1 (it's possible to specify the version, apart from the name). This has the caveat that if you specify ... install foo --pluginsDir=bar, the number of arguments is correct but it will fail anyway when trying to use the flag as a version.

Copy link
Contributor

@andresmgot andresmgot left a comment

Choose a reason for hiding this comment

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

LGTM

@oshirohugo oshirohugo changed the title Chore: Log ignored arguments on plugin install Chore: Fail plugins installation on wrong args provided Jul 12, 2023
@oshirohugo oshirohugo changed the title Chore: Fail plugins installation on wrong args provided Plugins: Fail plugins installation on wrong args provided Jul 12, 2023
@oshirohugo
Copy link
Contributor Author

@wbrowne could you please take a look. There was a change on the first proposal after @andresmgot suggestions and now we are failing the command.

Copy link
Member

@wbrowne wbrowne left a comment

Choose a reason for hiding this comment

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

A shame about the CLI package limitations, but definitely an improvement. Had an idea about potentially adding a log statement.

Think it would be good to follow this PR up with version field validation to avoid the possibility of using an "incorrect" single argument where version should be (like what @andresmgot mentioned in a comment).

Great work!


func validateInput(c utils.CommandLine) error {
if c.Args().Len() > installArgsSize {
return errors.New("install only supports 2 arguments: plugin and version")
Copy link
Member

Choose a reason for hiding this comment

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

Maybe before we return the error here, we should add a log with something more descriptive to hopefully help the user form the right command.

For example:

./bin/darwin-amd64/grafana cli plugins install autohome-comparequeries-datasource --debug --pluginUrl /Users/wbrowne/Downloads/autohome-comparequeries-datasource.zip                                           
Please specify the correct format. For example ./grafana cli (<command arguments>) plugins install <plugin ID> (<plugin version>)

Error: ✗ install only supports 2 arguments: plugin and version
Suggested change
return errors.New("install only supports 2 arguments: plugin and version")
logger.Info(color.RedString("Please specify the correct format. For example ./grafana cli (<command arguments>) plugins install <plugin ID> (<plugin version>)\n\n"))
return errors.New("install only supports 2 arguments: plugin and version")

Maybe @sympatheticmoose/@josmperez can weigh in here on best lingo

Copy link
Member

Choose a reason for hiding this comment

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

For reference, this would look like:
image

Copy link
Member

@wbrowne wbrowne left a comment

Choose a reason for hiding this comment

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

LGTM

@oshirohugo oshirohugo merged commit fe4a932 into main Jul 12, 2023
11 checks passed
@oshirohugo oshirohugo deleted the print-info-on-ignored-args branch July 12, 2023 11:52
polibb pushed a commit that referenced this pull request Jul 14, 2023
* Return error on plugin install extra args

* Remove unused valid argument

* Add log for wrong installation args
@ricky-undeadcoders ricky-undeadcoders modified the milestones: 10.1.x, 10.1.0 Aug 1, 2023
@oshirohugo oshirohugo self-assigned this Nov 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

Plugins: Make grafana CLI flexible in working with command arguments
5 participants