Skip to content

Commit 26d4525

Browse files
authored
Merge pull request #6817 from luojiyin1987/fix-plugin-cobra
fix: restore os.Args after plugin completion and fix error return
2 parents c3a17b9 + 6b1ba1a commit 26d4525

File tree

2 files changed

+64
-2
lines changed

2 files changed

+64
-2
lines changed

cli-plugins/manager/cobra.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,7 @@ func AddPluginCommandStubs(dockerCLI config.Provider, rootCmd *cobra.Command) (e
4545
RunE: func(cmd *cobra.Command, args []string) error {
4646
flags := rootCmd.PersistentFlags()
4747
flags.SetOutput(nil)
48-
perr := flags.Parse(args)
49-
if perr != nil {
48+
if err := flags.Parse(args); err != nil {
5049
return err
5150
}
5251
if flags.Changed("help") {
@@ -60,7 +59,11 @@ func AddPluginCommandStubs(dockerCLI config.Provider, rootCmd *cobra.Command) (e
6059
cargs := []string{p.Path, cobra.ShellCompRequestCmd, p.Name} //nolint:prealloc // no need to over-complicate things.
6160
cargs = append(cargs, args...)
6261
cargs = append(cargs, toComplete)
62+
origArgs := os.Args
6363
os.Args = cargs
64+
defer func() {
65+
os.Args = origArgs
66+
}()
6467
runCommand, runErr := PluginRunCommand(dockerCLI, p.Name, cmd)
6568
if runErr != nil {
6669
return nil, cobra.ShellCompDirectiveError

cli-plugins/manager/cobra_test.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
package manager
22

33
import (
4+
"os"
5+
"path/filepath"
6+
"sync"
47
"testing"
58

9+
"github.com/docker/cli/internal/test"
610
"github.com/spf13/cobra"
711
"gotest.tools/v3/assert"
812
)
@@ -24,3 +28,58 @@ func TestPluginResourceAttributesEnvvar(t *testing.T) {
2428
env = appendPluginResourceAttributesEnvvar(nil, cmd, Plugin{Name: "compose"})
2529
assert.DeepEqual(t, []string{"OTEL_RESOURCE_ATTRIBUTES=a.b.c=foo,docker.cli.cobra.command_path=docker%20compose"}, env)
2630
}
31+
32+
func TestPluginStubRunEReturnsParseError(t *testing.T) {
33+
cmd, err := preparePluginStubCommand(t)
34+
assert.NilError(t, err)
35+
36+
err = cmd.RunE(cmd, []string{"--definitely-not-a-real-flag"})
37+
assert.ErrorContains(t, err, "unknown flag: --definitely-not-a-real-flag")
38+
}
39+
40+
func TestPluginStubCompletionRestoresOSArgs(t *testing.T) {
41+
cmd, err := preparePluginStubCommand(t)
42+
assert.NilError(t, err)
43+
44+
savedArgs := os.Args
45+
t.Cleanup(func() { os.Args = savedArgs })
46+
47+
originalArgs := []string{"docker", "image", "ls"}
48+
os.Args = append([]string(nil), originalArgs...)
49+
50+
_, directive := cmd.ValidArgsFunction(cmd, []string{"--all"}, "alp")
51+
assert.Equal(t, directive, cobra.ShellCompDirectiveError)
52+
assert.DeepEqual(t, os.Args, originalArgs)
53+
}
54+
55+
func preparePluginStubCommand(t *testing.T) (*cobra.Command, error) {
56+
t.Helper()
57+
pluginCommandStubsOnce = sync.Once{}
58+
59+
tmpDir := t.TempDir()
60+
const cliPlugin = `#!/bin/sh
61+
printf '%s' '{"SchemaVersion":"0.1.0"}'
62+
`
63+
if err := os.WriteFile(filepath.Join(tmpDir, "docker-testplugin"), []byte(cliPlugin), 0o777); err != nil {
64+
return nil, err
65+
}
66+
67+
cli := test.NewFakeCli(nil)
68+
cli.ConfigFile().CLIPluginsExtraDirs = []string{tmpDir}
69+
70+
root := &cobra.Command{Use: "docker"}
71+
root.PersistentFlags().Bool("debug", false, "")
72+
73+
if err := AddPluginCommandStubs(cli, root); err != nil {
74+
return nil, err
75+
}
76+
77+
cmd, _, err := root.Find([]string{"testplugin"})
78+
if err != nil {
79+
return nil, err
80+
}
81+
if cmd == nil {
82+
return nil, os.ErrNotExist
83+
}
84+
return cmd, nil
85+
}

0 commit comments

Comments
 (0)