Skip to content

Commit 402b1db

Browse files
committed
fix: restore os.Args after plugin completion and fix error return
Signed-off-by: luojiyin <luojiyin@hotmail.com>
1 parent 419e5d1 commit 402b1db

File tree

2 files changed

+59
-2
lines changed

2 files changed

+59
-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: 54 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,53 @@ 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+
originalArgs := []string{"docker", "image", "ls"}
45+
os.Args = append([]string(nil), originalArgs...)
46+
47+
_, directive := cmd.ValidArgsFunction(cmd, []string{"--all"}, "alp")
48+
assert.Equal(t, directive, cobra.ShellCompDirectiveError)
49+
assert.DeepEqual(t, os.Args, originalArgs)
50+
}
51+
52+
func preparePluginStubCommand(t *testing.T) (*cobra.Command, error) {
53+
t.Helper()
54+
pluginCommandStubsOnce = sync.Once{}
55+
56+
tmpDir := t.TempDir()
57+
const cliPlugin = "#!/bin/sh\nprintf '%s' '{\"SchemaVersion\":\"0.1.0\"}'\n"
58+
if err := os.WriteFile(filepath.Join(tmpDir, "docker-testplugin"), []byte(cliPlugin), 0o777); err != nil {
59+
return nil, err
60+
}
61+
62+
cli := test.NewFakeCli(nil)
63+
cli.ConfigFile().CLIPluginsExtraDirs = []string{tmpDir}
64+
65+
root := &cobra.Command{Use: "docker"}
66+
root.PersistentFlags().Bool("debug", false, "")
67+
68+
if err := AddPluginCommandStubs(cli, root); err != nil {
69+
return nil, err
70+
}
71+
72+
cmd, _, err := root.Find([]string{"testplugin"})
73+
if err != nil {
74+
return nil, err
75+
}
76+
if cmd == nil {
77+
return nil, os.ErrNotExist
78+
}
79+
return cmd, nil
80+
}

0 commit comments

Comments
 (0)