Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 6 additions & 9 deletions cmd/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,14 +229,11 @@ func setupHelpCommand(dockerCli command.Cli, rootCmd, helpCmd *cobra.Command) {
}
}

func tryRunPluginHelp(dockerCli command.Cli, ccmd *cobra.Command, cargs []string) error {
root := ccmd.Root()

cmd, _, err := root.Traverse(cargs)
if err != nil {
return err
func tryRunPluginHelp(dockerCli command.Cli, ccmd *cobra.Command) error {
if !pluginmanager.IsPluginCommand(ccmd) {
return errdefs.ErrNotFound
}
helpcmd, err := pluginmanager.PluginRunCommand(dockerCli, cmd.Name(), root)
helpcmd, err := pluginmanager.PluginRunCommand(dockerCli, ccmd.Name(), ccmd.Root())
if err != nil {
return err
}
Expand All @@ -251,8 +248,8 @@ func setHelpFunc(dockerCli command.Cli, cmd *cobra.Command) {
return
}

if len(args) >= 1 {
err := tryRunPluginHelp(dockerCli, ccmd, args)
if pluginmanager.IsPluginCommand(ccmd) {
err := tryRunPluginHelp(dockerCli, ccmd)
if err == nil {
return
}
Expand Down
38 changes: 38 additions & 0 deletions cmd/docker/docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"fmt"
"io"
"os"
"path/filepath"
"strings"
"syscall"
"testing"
"time"
Expand Down Expand Up @@ -205,3 +207,39 @@ func TestVisitAll(t *testing.T) {
expected := []string{"sub1sub1", "sub1sub2", "sub1", "sub2", "root"}
assert.DeepEqual(t, expected, visited)
}

func TestSwarmInitHelpNotDelegatedToInitPlugin(t *testing.T) {
tmpDir := t.TempDir()
pluginScript := `#!/bin/sh
if [ "$1" = "--help" ] || [ "$1" = "-h" ]; then
echo "PLUGIN INIT HELP"
exit 0
fi
printf '%s' '{"SchemaVersion":"0.1.0","ShortDescription":"Docker Init"}'

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Vendor is required field but is missing so the plugin is considered invalid and not loaded.

`
pluginPath := filepath.Join(tmpDir, "docker-init")
err := os.WriteFile(pluginPath, []byte(pluginScript), 0o755)
assert.NilError(t, err)

var b bytes.Buffer
ctx := context.Background()
cli, err := command.NewDockerCli(
command.WithBaseContext(ctx),
command.WithCombinedStreams(&b),
)
assert.NilError(t, err)
cli.ConfigFile().CLIPluginsExtraDirs = []string{tmpDir}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

CLIPluginsExtraDirs is assigned before tcmd.Initialize() which reloads the CLI configuration and discards it.


tcmd := newDockerCommand(cli)
tcmd.SetArgs([]string{"swarm", "init", "--help"})
cmd, args, err := tcmd.HandleGlobalFlags()
assert.NilError(t, err)
assert.NilError(t, tcmd.Initialize())
cmd.SetArgs(args)
err = cmd.Execute()
assert.NilError(t, err)

out := b.String()
assert.Assert(t, is.Contains(out, "Initialize a swarm"))
assert.Assert(t, !strings.Contains(out, "PLUGIN INIT HELP"), "output: %s", out)
}
Loading