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

Basic framework for writing and running CLI plugins #1564

Merged
merged 23 commits into from
Jan 31, 2019

Commits on Jan 29, 2019

  1. Fold dockerPreRun into DockerCli.Initialize

    All of the current callers follow the pattern:
    
        dockerPreRun(opts)
        err := dockerCli.Initialize(opts) ...
    
    So there is no semantic change into merging the content of `dockerPreRun` into the head of `Initialize`.
    
    I'm about to add a new caller outside of the `cmd/docker` package and this
    seems preferable exporting `DockerPreRun`.
    
    Signed-off-by: Ian Campbell <ijc@docker.com>
    Ian Campbell committed Jan 29, 2019
    Configuration menu
    Copy the full SHA
    158a766 View commit details
    Browse the repository at this point in the history
  2. Move disableFlagsInUseLine from main into our cli library

    ... and expose. I would like to use this from another site.
    
    This implies also moving (and exposing) the `visitAll` helper.
    
    Unit test them while I'm here.
    
    Signed-off-by: Ian Campbell <ijc@docker.com>
    Ian Campbell committed Jan 29, 2019
    Configuration menu
    Copy the full SHA
    ccef159 View commit details
    Browse the repository at this point in the history
  3. Refactor common bits of SetupRootCommand

    I'm shortly going to add a second user to setup plugins, which will want to
    reuse the common bits.
    
    Signed-off-by: Ian Campbell <ijc@docker.com>
    Ian Campbell committed Jan 29, 2019
    Configuration menu
    Copy the full SHA
    38645ca View commit details
    Browse the repository at this point in the history
  4. Push setup of opts and default flagset into SetupRootCommand

    I'm shortly going to add a second user (plugins) which want to share some
    behaviour.
    
    Signed-off-by: Ian Campbell <ijc@docker.com>
    Ian Campbell committed Jan 29, 2019
    Configuration menu
    Copy the full SHA
    c516811 View commit details
    Browse the repository at this point in the history
  5. Move versioning variables to a separate package.

    This helps to avoid circular includes, by separating the pure data out from the
    actual functionality in the cli subpackage, allowing other code which is
    imported to access the data.
    
    Signed-off-by: Ian Campbell <ijc@docker.com>
    Ian Campbell committed Jan 29, 2019
    Configuration menu
    Copy the full SHA
    20c1983 View commit details
    Browse the repository at this point in the history
  6. cli/config: Add a helper to resolve a file within the config dir

    Signed-off-by: Ian Campbell <ijc@docker.com>
    Ian Campbell committed Jan 29, 2019
    Configuration menu
    Copy the full SHA
    eab40a5 View commit details
    Browse the repository at this point in the history
  7. Unit test for WithContentTrustFromEnv

    I authored this for `contentTrustEnabled` prior to 7f207f3, so this now
    tests the funcation argument version.
    
    Signed-off-by: Ian Campbell <ijc@docker.com>
    Ian Campbell committed Jan 29, 2019
    Configuration menu
    Copy the full SHA
    8cf946d View commit details
    Browse the repository at this point in the history
  8. Add basic framework for writing a CLI plugin

    That is, the helper to be used from the plugin's `main`.
    
    Also add a `helloworld` plugin example and build integration.
    
    Signed-off-by: Ian Campbell <ijc@docker.com>
    Ian Campbell committed Jan 29, 2019
    Configuration menu
    Copy the full SHA
    e962404 View commit details
    Browse the repository at this point in the history

Commits on Jan 30, 2019

  1. Add support for running a CLI plugin

    Also includes the  scaffolding for finding a validating plugin candidates.
    
    Argument validation is moved to RunE to support this, so `noArgs` is removed.
    
    Signed-off-by: Ian Campbell <ijc@docker.com>
    Ian Campbell committed Jan 30, 2019
    Configuration menu
    Copy the full SHA
    f1f31ab View commit details
    Browse the repository at this point in the history
  2. Add some simple e2e tests for executing CLI plugins

    To help with this add a bad plugin which produces invalid metadata and arrange
    for it to be built in the e2e container.
    
    Signed-off-by: Ian Campbell <ijc@docker.com>
    Ian Campbell committed Jan 30, 2019
    Configuration menu
    Copy the full SHA
    5db3367 View commit details
    Browse the repository at this point in the history
  3. Integrate CLI plugins into docker help output.

    To do this we add a stub `cobra.Command` for each installed plugin (only when
    invoking `help`, not for normal running).
    
    This requires a function to list all available plugins so that is added here.
    
    Signed-off-by: Ian Campbell <ijc@docker.com>
    Ian Campbell committed Jan 30, 2019
    Configuration menu
    Copy the full SHA
    f912b55 View commit details
    Browse the repository at this point in the history
  4. Add stubs when calling help due to no arguments

    e.g. the `docker` case which should act as `docker help`.
    
    Signed-off-by: Ian Campbell <ijc@docker.com>
    Ian Campbell committed Jan 30, 2019
    Configuration menu
    Copy the full SHA
    c43da09 View commit details
    Browse the repository at this point in the history
  5. Integrate CLI plugins with docker help «foo»

    Signed-off-by: Ian Campbell <ijc@docker.com>
    Ian Campbell committed Jan 30, 2019
    Configuration menu
    Copy the full SHA
    20a2847 View commit details
    Browse the repository at this point in the history
  6. Integrate CLI plugins with docker «plugin» --help.

    To achieve this we hook in at the beginning of our custom `HelpFunc` and detect
    the plugin case by adding stub commands.
    
    Signed-off-by: Ian Campbell <ijc@docker.com>
    Ian Campbell committed Jan 30, 2019
    Configuration menu
    Copy the full SHA
    53f0181 View commit details
    Browse the repository at this point in the history
  7. Allow plugins to make use of the cobra PersistentPreRun hooks.

    Previously a plugin which used these hooks would overwrite the top-level plugin
    command's use of this hook, resulting in the dockerCli object not being fully
    initialised.
    
    Provide a function which plugins can use to chain to the required behaviour.
    This required some fairly ugly arrangements to preserve state (which was
    previously in-scope in `newPluginCOmmand`) to be used by the new function.
    
    Signed-off-by: Ian Campbell <ijc@docker.com>
    Ian Campbell committed Jan 30, 2019
    Configuration menu
    Copy the full SHA
    e5e578a View commit details
    Browse the repository at this point in the history
  8. Output broken CLI plugins in help output.

    Signed-off-by: Ian Campbell <ijc@docker.com>
    Ian Campbell committed Jan 30, 2019
    Configuration menu
    Copy the full SHA
    0ab8ec0 View commit details
    Browse the repository at this point in the history
  9. Integrate CLI plugins into docker info

    Fairly straight forward. It became necessary to wrap `Plugin.Err` with a type
    which implements `encoding.MarshalText` in order to have that field rendered
    properly in the `docker info -f '{{json}}'` output.
    
    Since I changed the type somewhat I also added a unit test for `formatInfo`.
    
    Signed-off-by: Ian Campbell <ijc@docker.com>
    Ian Campbell committed Jan 30, 2019
    Configuration menu
    Copy the full SHA
    1c576e9 View commit details
    Browse the repository at this point in the history
  10. Documentation on writing a plugin

    Signed-off-by: Ian Campbell <ijc@docker.com>
    Ian Campbell committed Jan 30, 2019
    Configuration menu
    Copy the full SHA
    609dcb9 View commit details
    Browse the repository at this point in the history
  11. Refactor code which deals with Windows' .exe suffix

    Signed-off-by: Ian Campbell <ijc@docker.com>
    Ian Campbell committed Jan 30, 2019
    Configuration menu
    Copy the full SHA
    63f3ad1 View commit details
    Browse the repository at this point in the history
  12. Check for .exe case insensitively

    On Windows `foo.exe`, `foo.eXe` and `foo.EXE` are equally executable.
    
    Signed-off-by: Ian Campbell <ijc@docker.com>
    Ian Campbell committed Jan 30, 2019
    Configuration menu
    Copy the full SHA
    1337895 View commit details
    Browse the repository at this point in the history
  13. Ignore unknown arguments on the top-level command.

    This allows passing argument to plugins, otherwise they are caught by the parse
    loop, since cobra does not know about each plugin at this stage (to avoid
    having to always scan for all plugins) this means that e.g. `docker plugin
    --foo` would accumulate `plugin` as an arg to the `docker` command, then choke
    on the unknown `--foo`.
    
    This allows unknown global args only, unknown arguments on subcommands (e.g.
    `docker ps --foo`) are still correctly caught.
    
    Add an e2e test covering this case.
    
    Signed-off-by: Ian Campbell <ijc@docker.com>
    Ian Campbell committed Jan 30, 2019
    Configuration menu
    Copy the full SHA
    935d47b View commit details
    Browse the repository at this point in the history
  14. Ensure plugins default search path obeys --config

    A static global initialiser happens before the arguments are parsed, so we need
    to calculate the path later.
    
    Signed-off-by: Ian Campbell <ijc@docker.com>
    Ian Campbell committed Jan 30, 2019
    Configuration menu
    Copy the full SHA
    0a89eb5 View commit details
    Browse the repository at this point in the history
  15. Ensure that plugins are only listed once in help outputs.

    They were listed twice in `docker --help` (but not `docker help`), since the
    stubs were added in both `tryRunPluginHelp` and the `setHelpFunc` closure.
    
    Calling `AddPluginStubCommands` earlier in `setHelpFunc` before the call to
    `tryRunPluginHelp` is sufficient. Also it is no longer necessary to add just
    valid plugins (`tryRunPluginHelp` handles invalid plugins correctly) so remove
    that logic (which was in any case broken for e.g. `docker --help`).
    
    Update the e2e test to check for duplicate entries and also to test `docker
    --help` which was previously missed.
    
    Signed-off-by: Ian Campbell <ijc@docker.com>
    Ian Campbell committed Jan 30, 2019
    Configuration menu
    Copy the full SHA
    baabf6e View commit details
    Browse the repository at this point in the history