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

Allow plugins to have --flags which are the same as the top-level #1722

Merged
merged 5 commits into from
Mar 13, 2019

Commits on Mar 13, 2019

  1. e2e: start a new file for cli plugins arguments tests

    `TestRunGoodArgument` fits here.
    
    Signed-off-by: Ian Campbell <[email protected]>
    Ian Campbell committed Mar 13, 2019
    Configuration menu
    Copy the full SHA
    4e5f0af View commit details
    Browse the repository at this point in the history
  2. Add e2e tests for plugin vs global argument issues

    These won't pass right now due to docker#1699
    ("Plugins can't re-use the same flags as cli global flags") and the change in
    935d47b ("Ignore unknown arguments on the top-level command."), but the
    intention is to fix them now.
    
    Signed-off-by: Ian Campbell <[email protected]>
    Ian Campbell committed Mar 13, 2019
    Configuration menu
    Copy the full SHA
    8289ae0 View commit details
    Browse the repository at this point in the history
  3. 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 docker#1699, and also, as a side-effect, the first item in docker#1661.
    
    Signed-off-by: Ian Campbell <[email protected]>
    Ian Campbell committed Mar 13, 2019
    Configuration menu
    Copy the full SHA
    d4ced2e View commit details
    Browse the repository at this point in the history
  4. Add e2e test for handling of version/-v/--version with plugins

    Previous commits fixed the first issue on docker#1661, this simply adds a test for
    it. Note that this is testing the current behaviour, without regard for the
    second issue in docker#1661 which proposes a different behaviour.
    
    Signed-off-by: Ian Campbell <[email protected]>
    Ian Campbell committed Mar 13, 2019
    Configuration menu
    Copy the full SHA
    2c624e8 View commit details
    Browse the repository at this point in the history
  5. Use a copy of root flagset in HandleGlobalFlags

    This makes things more idempotent, rather than relying on undoing the
    interspersed settings.
    
    Note that the underlying `Flag`s remain shared, it's just the `FlagSet` which
    is duplicated.
    
    Signed-off-by: Ian Campbell <[email protected]>
    Ian Campbell committed Mar 13, 2019
    Configuration menu
    Copy the full SHA
    e824bc8 View commit details
    Browse the repository at this point in the history