mirror of https://github.com/docker/cli.git
load plugin command stubs when required
We are currently loading plugin command stubs for every invocation which still has a significant performance hit. With this change we are doing this operation only if cobra completion arg request is found. - 20.10.23: `docker --version` takes ~15ms - 23.0.1: `docker --version` takes ~93ms With this change `docker --version` takes ~9ms Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
This commit is contained in:
parent
62f2358b99
commit
c39c711a18
|
@ -3,6 +3,7 @@ package manager
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"sync"
|
||||||
|
|
||||||
"github.com/docker/cli/cli/command"
|
"github.com/docker/cli/cli/command"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
@ -31,13 +32,17 @@ const (
|
||||||
CommandAnnotationPluginInvalid = "com.docker.cli.plugin-invalid"
|
CommandAnnotationPluginInvalid = "com.docker.cli.plugin-invalid"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var pluginCommandStubsOnce sync.Once
|
||||||
|
|
||||||
// AddPluginCommandStubs adds a stub cobra.Commands for each valid and invalid
|
// AddPluginCommandStubs adds a stub cobra.Commands for each valid and invalid
|
||||||
// plugin. The command stubs will have several annotations added, see
|
// plugin. The command stubs will have several annotations added, see
|
||||||
// `CommandAnnotationPlugin*`.
|
// `CommandAnnotationPlugin*`.
|
||||||
func AddPluginCommandStubs(dockerCli command.Cli, rootCmd *cobra.Command) error {
|
func AddPluginCommandStubs(dockerCli command.Cli, rootCmd *cobra.Command) (err error) {
|
||||||
plugins, err := ListPlugins(dockerCli, rootCmd)
|
pluginCommandStubsOnce.Do(func() {
|
||||||
|
var plugins []Plugin
|
||||||
|
plugins, err = ListPlugins(dockerCli, rootCmd)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return
|
||||||
}
|
}
|
||||||
for _, p := range plugins {
|
for _, p := range plugins {
|
||||||
p := p
|
p := p
|
||||||
|
@ -62,8 +67,8 @@ func AddPluginCommandStubs(dockerCli command.Cli, rootCmd *cobra.Command) error
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
flags := rootCmd.PersistentFlags()
|
flags := rootCmd.PersistentFlags()
|
||||||
flags.SetOutput(nil)
|
flags.SetOutput(nil)
|
||||||
err := flags.Parse(args)
|
perr := flags.Parse(args)
|
||||||
if err != nil {
|
if perr != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if flags.Changed("help") {
|
if flags.Changed("help") {
|
||||||
|
@ -78,17 +83,18 @@ func AddPluginCommandStubs(dockerCli command.Cli, rootCmd *cobra.Command) error
|
||||||
cargs = append(cargs, args...)
|
cargs = append(cargs, args...)
|
||||||
cargs = append(cargs, toComplete)
|
cargs = append(cargs, toComplete)
|
||||||
os.Args = cargs
|
os.Args = cargs
|
||||||
runCommand, err := PluginRunCommand(dockerCli, p.Name, cmd)
|
runCommand, runErr := PluginRunCommand(dockerCli, p.Name, cmd)
|
||||||
if err != nil {
|
if runErr != nil {
|
||||||
return nil, cobra.ShellCompDirectiveError
|
return nil, cobra.ShellCompDirectiveError
|
||||||
}
|
}
|
||||||
err = runCommand.Run()
|
runErr = runCommand.Run()
|
||||||
if err == nil {
|
if runErr == nil {
|
||||||
os.Exit(0) // plugin already rendered complete data
|
os.Exit(0) // plugin already rendered complete data
|
||||||
}
|
}
|
||||||
return nil, cobra.ShellCompDirectiveError
|
return nil, cobra.ShellCompDirectiveError
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
return nil
|
})
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
10
cli/cobra.go
10
cli/cobra.go
|
@ -204,6 +204,16 @@ func DisableFlagsInUseLine(cmd *cobra.Command) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// HasCompletionArg returns true if a cobra completion arg request is found.
|
||||||
|
func HasCompletionArg(args []string) bool {
|
||||||
|
for _, arg := range args {
|
||||||
|
if arg == cobra.ShellCompRequestCmd || arg == cobra.ShellCompNoDescRequestCmd {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
var helpCommand = &cobra.Command{
|
var helpCommand = &cobra.Command{
|
||||||
Use: "help [command]",
|
Use: "help [command]",
|
||||||
Short: "Help about the command",
|
Short: "Help about the command",
|
||||||
|
|
|
@ -133,14 +133,21 @@ func tryRunPluginHelp(dockerCli command.Cli, ccmd *cobra.Command, cargs []string
|
||||||
func setHelpFunc(dockerCli command.Cli, cmd *cobra.Command) {
|
func setHelpFunc(dockerCli command.Cli, cmd *cobra.Command) {
|
||||||
defaultHelpFunc := cmd.HelpFunc()
|
defaultHelpFunc := cmd.HelpFunc()
|
||||||
cmd.SetHelpFunc(func(ccmd *cobra.Command, args []string) {
|
cmd.SetHelpFunc(func(ccmd *cobra.Command, args []string) {
|
||||||
if pluginmanager.IsPluginCommand(ccmd) {
|
if err := pluginmanager.AddPluginCommandStubs(dockerCli, ccmd.Root()); err != nil {
|
||||||
|
ccmd.Println(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(args) >= 1 {
|
||||||
err := tryRunPluginHelp(dockerCli, ccmd, args)
|
err := tryRunPluginHelp(dockerCli, ccmd, args)
|
||||||
|
if err == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
if !pluginmanager.IsNotFound(err) {
|
if !pluginmanager.IsNotFound(err) {
|
||||||
ccmd.Println(err)
|
ccmd.Println(err)
|
||||||
}
|
|
||||||
cmd.PrintErrf("unknown help topic: %v\n", ccmd.Name())
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if err := isSupported(ccmd, dockerCli); err != nil {
|
if err := isSupported(ccmd, dockerCli); err != nil {
|
||||||
ccmd.Println(err)
|
ccmd.Println(err)
|
||||||
|
@ -227,10 +234,15 @@ func runDocker(dockerCli *command.DockerCli) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if cli.HasCompletionArg(args) {
|
||||||
|
// We add plugin command stubs early only for completion. We don't
|
||||||
|
// want to add them for normal command execution as it would cause
|
||||||
|
// a significant performance hit.
|
||||||
err = pluginmanager.AddPluginCommandStubs(dockerCli, cmd)
|
err = pluginmanager.AddPluginCommandStubs(dockerCli, cmd)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if len(args) > 0 {
|
if len(args) > 0 {
|
||||||
ccmd, _, err := cmd.Find(args)
|
ccmd, _, err := cmd.Find(args)
|
||||||
|
|
|
@ -83,7 +83,7 @@ func TestHelpBad(t *testing.T) {
|
||||||
|
|
||||||
res := icmd.RunCmd(run("help", "badmeta"))
|
res := icmd.RunCmd(run("help", "badmeta"))
|
||||||
res.Assert(t, icmd.Expected{
|
res.Assert(t, icmd.Expected{
|
||||||
ExitCode: 0,
|
ExitCode: 1,
|
||||||
Out: icmd.None,
|
Out: icmd.None,
|
||||||
})
|
})
|
||||||
golden.Assert(t, res.Stderr(), "docker-help-badmeta-err.golden")
|
golden.Assert(t, res.Stderr(), "docker-help-badmeta-err.golden")
|
||||||
|
@ -110,8 +110,8 @@ func TestBadHelp(t *testing.T) {
|
||||||
res.Assert(t, icmd.Expected{
|
res.Assert(t, icmd.Expected{
|
||||||
ExitCode: 0,
|
ExitCode: 0,
|
||||||
// This should be identical to the --help case above
|
// This should be identical to the --help case above
|
||||||
Out: usage,
|
Out: shortHFlagDeprecated + usage,
|
||||||
Err: shortHFlagDeprecated,
|
Err: icmd.None,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue