fix: restore os.Args after plugin completion and fix error return#6817
fix: restore os.Args after plugin completion and fix error return#6817luojiyin1987 wants to merge 1 commit intodocker:masterfrom
Conversation
thaJeztah
left a comment
There was a problem hiding this comment.
whoops! I started reviewing, but didn't submit 🙈
| perr := flags.Parse(args) | ||
| if perr != nil { | ||
| return err | ||
| return perr |
There was a problem hiding this comment.
Nice catch; while updating; perhaps just inline it;
if err := flags.Parse(args); err != nil {
return err| assert.DeepEqual(t, os.Args, originalArgs) | ||
| } | ||
|
|
||
| func preparePluginStubCommand(t *testing.T, metadata string) *cobra.Command { |
There was a problem hiding this comment.
Looks like my IDE (and possibly linters) has trouble detecting that the returned command will never be nil (due to the asserts); it's probably OK to just return the errors instead
| dir := fs.NewDir(t, t.Name(), | ||
| fs.WithFile("docker-testplugin", "#!/bin/sh\nprintf '%s' '"+metadata+"'\n", fs.WithMode(0o777)), | ||
| ) | ||
| t.Cleanup(func() { dir.Remove() }) |
There was a problem hiding this comment.
I know we use the fs package still in some other places in this package, but I'm .. slightly trying to get rid of it (in most cases it's overkill); this one can probably be simplified using stdlib;
Also looks like we don't use the metadata anywhere for assertions, so we could just make this all "static" (until there's a specific reason to customise it);
tmpDir := t.TempDir()
const cliPlugin = `
#!/bin/sh
printf '%s' '{"SchemaVersion":"0.1.0","Vendor":"e2e-testing"}'
`
err := os.WriteFile(filepath.Join(tmpDir, "docker-testplugin"), []byte(cliPlugin), 0o777)| cli.SetConfigFile(&configfile.ConfigFile{ | ||
| CLIPluginsExtraDirs: []string{dir.Path()}, | ||
| }) |
There was a problem hiding this comment.
Probably fine here to simplify and just patch the config;
cli.ConfigFile().CLIPluginsExtraDirs = []string{tmpDir}
- What I did
Fix two issues in the plugin manager:
- How I did it
errtoperrwhen flag parsing failsos.Argsbefore modification and usedeferto restore it- Files changed:
cli-plugins/manager/cobra.gocli-plugins/manager/cobra_test.go- How to verify it
The new tests verify both scenarios.
- Description for the changelog
Fixes #6816