DockerCLI/cli
Ian Campbell d4ced2ef77 allow plugins to have argument which match a top-level flag.
The issue with plugin options clashing with globals is that when cobra is
parsing the command line and it comes across an argument which doesn't start
with a `-` it (in the absence of plugins) distinguishes between "argument to
current command" and "new subcommand" based on the list of registered sub
commands.

Plugins breaks that model. When presented with `docker -D plugin -c foo` cobra
parses up to the `plugin`, sees it isn't a registered sub-command of the
top-level docker (because it isn't, it's a plugin) so it accumulates it as an
argument to the top-level `docker` command. Then it sees the `-c`, and thinks
it is the global `-c` (for AKA `--context`) option and tries to treat it as
that, which fails.

In the specific case of the top-level `docker` subcommand we know that it has
no arguments which aren't `--flags` (or `-f` short flags) and so anything which
doesn't start with a `-` must either be a (known) subcommand or an attempt to
execute a plugin.

We could simply scan for and register all installed plugins at start of day, so
that cobra can do the right thing, but we want to avoid that since it would
involve executing each plugin to fetch the metadata, even if the command wasn't
going to end up hitting a plugin.

Instead we can parse the initial set of global arguments separately before
hitting the main cobra `Execute` path, which works here exactly because we know
that the top-level has no non-flag arguments.

One slight wrinkle is that the top-level `PersistentPreRunE` is no longer
called on the plugins path (since it no longer goes via `Execute`), so we
arrange for the initialisation done there (which has to be done after global
flags are parsed to handle e.g. `--config`) to happen explictly after the
global flags are parsed. Rather than make `newDockerCommand` return the
complicated set of results needed to make this happen, instead return a closure
which achieves this.

The new functionality is introduced via a common `TopLevelCommand` abstraction
which lets us adjust the plugin entrypoint to use the same strategy for parsing
the global arguments. This isn't strictly required (in this case the stuff in
cobra's `Execute` works fine) but doing it this way avoids the possibility of
subtle differences in behaviour.

Fixes #1699, and also, as a side-effect, the first item in #1661.

Signed-off-by: Ian Campbell <ijc@docker.com>
2019-03-13 11:28:17 +00:00
..
command Merge pull request #1718 from ijc/dial-stdio-npipe-on-windows 2019-03-13 11:49:25 +01:00
compose bump docker/docker to 8aca18d631f3f72d4c6e3dc01b6e5d468ad941b8 2019-03-02 13:41:42 +01:00
config harden config.Path() to disallow directory traversal 2019-03-07 14:40:53 +00:00
connhelper connhelper: export functions for other projects 2019-03-02 21:11:49 +09:00
context Add context store config options and expose context commands 2019-01-29 11:19:54 +01:00
debug updated vendoring 2017-09-01 19:41:06 -04:00
flags Introduce docker context store 2019-01-04 17:06:51 +01:00
manifest Fix some spelling errors 2019-01-11 15:49:02 +08:00
registry/client cli/registry: fix a Debugf statement 2018-10-11 01:13:13 -07:00
streams Extract streams helpers from command package to their own package to remove a cyclic dependency from command to internal/containerizedengine 2019-01-28 14:36:00 +01:00
trust Update tests to use gotest.tools 👼 2018-06-08 18:24:26 +02:00
version Move versioning variables to a separate package. 2019-01-29 11:26:40 +00:00
winresources Add windows resources to binary. 2017-05-15 18:03:03 -04:00
cobra.go allow plugins to have argument which match a top-level flag. 2019-03-13 11:28:17 +00:00
cobra_test.go Refactor plugins' vendor location on --help 2019-02-21 17:54:11 +01:00
error.go Import docker/docker/cli 2017-04-17 17:40:59 -04:00
required.go Singularize / pluralize "argument(s)" in error message 2017-08-12 18:25:38 +02:00
required_test.go Update tests to use gotest.tools 👼 2018-06-08 18:24:26 +02:00