Integrate CLI plugins with `docker help «foo»`

Signed-off-by: Ian Campbell <ijc@docker.com>
This commit is contained in:
Ian Campbell 2018-12-11 14:52:59 +00:00
parent c43da09188
commit 20a284721c
6 changed files with 79 additions and 0 deletions

View File

@ -103,6 +103,18 @@ func setupHelpCommand(dockerCli *command.DockerCli, rootCmd, helpCmd *cobra.Comm
return err
}
if len(args) > 0 {
helpcmd, err := pluginmanager.PluginRunCommand(dockerCli, args[0], rootCmd)
if err == nil {
err = helpcmd.Run()
if err != nil {
return err
}
}
if !pluginmanager.IsNotFound(err) {
return err
}
}
if origRunE != nil {
return origRunE(c, args)
}

View File

@ -22,6 +22,19 @@ func TestRunNonexisting(t *testing.T) {
golden.Assert(t, res.Stderr(), "docker-nonexistent-err.golden")
}
// TestHelpNonexisting ensures correct behaviour when invoking help on a nonexistent plugin.
func TestHelpNonexisting(t *testing.T) {
run, cleanup := prepare(t)
defer cleanup()
res := icmd.RunCmd(run("help", "nonexistent"))
res.Assert(t, icmd.Expected{
ExitCode: 1,
})
assert.Assert(t, is.Equal(res.Stdout(), ""))
golden.Assert(t, res.Stderr(), "docker-help-nonexistent-err.golden")
}
// TestRunBad ensures correct behaviour when running an existent but invalid plugin
func TestRunBad(t *testing.T) {
run, cleanup := prepare(t)
@ -35,6 +48,19 @@ func TestRunBad(t *testing.T) {
golden.Assert(t, res.Stderr(), "docker-badmeta-err.golden")
}
// TestHelpBad ensures correct behaviour when invoking help on a existent but invalid plugin.
func TestHelpBad(t *testing.T) {
run, cleanup := prepare(t)
defer cleanup()
res := icmd.RunCmd(run("help", "badmeta"))
res.Assert(t, icmd.Expected{
ExitCode: 1,
})
assert.Assert(t, is.Equal(res.Stdout(), ""))
golden.Assert(t, res.Stderr(), "docker-help-badmeta-err.golden")
}
// TestRunGood ensures correct behaviour when running a valid plugin
func TestRunGood(t *testing.T) {
run, cleanup := prepare(t)
@ -47,6 +73,19 @@ func TestRunGood(t *testing.T) {
})
}
// TestHelpGood ensures correct behaviour when invoking help on a
// valid plugin. A global argument is included to ensure it does not
// interfere.
func TestHelpGood(t *testing.T) {
run, cleanup := prepare(t)
defer cleanup()
res := icmd.RunCmd(run("-D", "help", "helloworld"))
res.Assert(t, icmd.Success)
golden.Assert(t, res.Stdout(), "docker-help-helloworld.golden")
assert.Assert(t, is.Equal(res.Stderr(), ""))
}
// TestRunGoodSubcommand ensures correct behaviour when running a valid plugin with a subcommand
func TestRunGoodSubcommand(t *testing.T) {
run, cleanup := prepare(t)
@ -58,3 +97,16 @@ func TestRunGoodSubcommand(t *testing.T) {
Out: "Goodbye World!",
})
}
// TestHelpGoodSubcommand ensures correct behaviour when invoking help on a
// valid plugin subcommand. A global argument is included to ensure it does not
// interfere.
func TestHelpGoodSubcommand(t *testing.T) {
run, cleanup := prepare(t)
defer cleanup()
res := icmd.RunCmd(run("-D", "help", "helloworld", "goodbye"))
res.Assert(t, icmd.Success)
golden.Assert(t, res.Stdout(), "docker-help-helloworld-goodbye.golden")
assert.Assert(t, is.Equal(res.Stderr(), ""))
}

View File

@ -0,0 +1 @@
unknown help topic: badmeta

View File

@ -0,0 +1,4 @@
Usage: docker helloworld goodbye
Say Goodbye instead of Hello

View File

@ -0,0 +1,9 @@
Usage: docker helloworld COMMAND
A basic Hello World plugin for tests
Commands:
goodbye Say Goodbye instead of Hello
Run 'docker helloworld COMMAND --help' for more information on a command.

View File

@ -0,0 +1 @@
unknown help topic: nonexistent