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

Fix regression for CLI plugins using PersistentPreRunE #1737

Merged
merged 2 commits into from
Mar 14, 2019

Conversation

ijc
Copy link
Contributor

@ijc ijc commented Mar 14, 2019

I got a bit carried away in d4ced2e ("allow plugins to have argument which match a top-level flag.") and broke the ability of a plugin to use the PersistentPreRun(E) hook on its top-level command (by unconditionally overwriting it) and also broke the plugin framework if a plugin's subcommand used those hooks (because they would shadow the root one). This could result in either dockerCli.Client() returning nil or whatever initialisation the plugin hoped to do not occuring.

Unfortunately my carriedawayness extended to thinking the e2e test was no longer necessary (although I'm not completely sure it would have caught this case in fact). So first introduce a new (and better) e2e test case for this, then fix the issue which ends up being easier looking cleaner than the original code (so this isn't a simple partial revert).

Ian Campbell added 2 commits March 14, 2019 14:20
I regressed this in d4ced2e ("allow plugins to have argument which match a
top-level flag.") by unconditionally overwriting any `PersistentRunE` that the
user may have supplied.

We need to ensure two things:

1. That the user can use `PersistentRunE` (or `PersistentRun`) for their own
   purposes.
2. That our initialisation always runs, even if the user has used
   `PersistentRun*`, since that will shadow the root.

To do this add a `PersistentRunE` to the helloworld plugin which logs (covers 1
above) and then use it when calling the `apiversion` subcommand (which covers 2
since that uses the client)

Signed-off-by: Ian Campbell <ijc@docker.com>
I got a bit carried away in d4ced2e ("allow plugins to have argument
which match a top-level flag.") and broke the ability of a plugin to use the
`PersistentPreRun(E)` hook on its top-level command (by unconditionally
overwriting it) and also broke the plugin framework if a plugin's subcommand
used those hooks (because they would shadow the root one). This could result in
either `dockerCli.Client()` returning `nil` or whatever initialisation the
plugin hoped to do not occuring.

This change revert the relevant bits and reinstates the requirement that a
plugin calls `plugin.PersistentPreRunE` if it uses that hook itself.

It is at least a bit nicer now since we avoid the need for the global struct
since the interesting state is now encapsulated in `tcmd` (and the closure).

In principal this could be done even more simply (by calling `tcmd.Initialize`
statically between `tcmd.HandleGlobalFlags` and `cmd.Execute`) however this has
the downside of _always_ initialising the cli (and therefore dialing the
daemon) even for the `docker-cli-plugin-metadata` command but also for the
`help foo` and `foo --help` commands (Cobra short-circuits the hooks in this
case).

Signed-off-by: Ian Campbell <ijc@docker.com>
Copy link
Member

@thaJeztah thaJeztah 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!

@codecov-io
Copy link

Codecov Report

Merging #1737 into master will not change coverage.
The diff coverage is n/a.

@@           Coverage Diff           @@
##           master    #1737   +/-   ##
=======================================
  Coverage   56.04%   56.04%           
=======================================
  Files         306      306           
  Lines       21050    21050           
=======================================
  Hits        11798    11798           
  Misses       8399     8399           
  Partials      853      853

Copy link
Contributor

@silvin-lubecki silvin-lubecki left a comment

Choose a reason for hiding this comment

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

LGTM

@thaJeztah thaJeztah merged commit 7f1176b into docker:master Mar 14, 2019
@GordonTheTurtle GordonTheTurtle added this to the 19.03.0 milestone Mar 14, 2019
@ijc ijc deleted the plugins-using-PersistentPreRunE branch March 14, 2019 14:51
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.

5 participants