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 (
|
||||
"fmt"
|
||||
"os"
|
||||
"sync"
|
||||
|
||||
"github.com/docker/cli/cli/command"
|
||||
"github.com/spf13/cobra"
|
||||
|
@ -31,13 +32,17 @@ const (
|
|||
CommandAnnotationPluginInvalid = "com.docker.cli.plugin-invalid"
|
||||
)
|
||||
|
||||
var pluginCommandStubsOnce sync.Once
|
||||
|
||||
// AddPluginCommandStubs adds a stub cobra.Commands for each valid and invalid
|
||||
// plugin. The command stubs will have several annotations added, see
|
||||
// `CommandAnnotationPlugin*`.
|
||||
func AddPluginCommandStubs(dockerCli command.Cli, rootCmd *cobra.Command) error {
|
||||
plugins, err := ListPlugins(dockerCli, rootCmd)
|
||||
func AddPluginCommandStubs(dockerCli command.Cli, rootCmd *cobra.Command) (err error) {
|
||||
pluginCommandStubsOnce.Do(func() {
|
||||
var plugins []Plugin
|
||||
plugins, err = ListPlugins(dockerCli, rootCmd)
|
||||
if err != nil {
|
||||
return err
|
||||
return
|
||||
}
|
||||
for _, p := range plugins {
|
||||
p := p
|
||||
|
@ -62,8 +67,8 @@ func AddPluginCommandStubs(dockerCli command.Cli, rootCmd *cobra.Command) error
|
|||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
flags := rootCmd.PersistentFlags()
|
||||
flags.SetOutput(nil)
|
||||
err := flags.Parse(args)
|
||||
if err != nil {
|
||||
perr := flags.Parse(args)
|
||||
if perr != nil {
|
||||
return err
|
||||
}
|
||||
if flags.Changed("help") {
|
||||
|
@ -78,17 +83,18 @@ func AddPluginCommandStubs(dockerCli command.Cli, rootCmd *cobra.Command) error
|
|||
cargs = append(cargs, args...)
|
||||
cargs = append(cargs, toComplete)
|
||||
os.Args = cargs
|
||||
runCommand, err := PluginRunCommand(dockerCli, p.Name, cmd)
|
||||
if err != nil {
|
||||
runCommand, runErr := PluginRunCommand(dockerCli, p.Name, cmd)
|
||||
if runErr != nil {
|
||||
return nil, cobra.ShellCompDirectiveError
|
||||
}
|
||||
err = runCommand.Run()
|
||||
if err == nil {
|
||||
runErr = runCommand.Run()
|
||||
if runErr == nil {
|
||||
os.Exit(0) // plugin already rendered complete data
|
||||
}
|
||||
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{
|
||||
Use: "help [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) {
|
||||
defaultHelpFunc := cmd.HelpFunc()
|
||||
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)
|
||||
if err == nil {
|
||||
return
|
||||
}
|
||||
if !pluginmanager.IsNotFound(err) {
|
||||
ccmd.Println(err)
|
||||
}
|
||||
cmd.PrintErrf("unknown help topic: %v\n", ccmd.Name())
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if err := isSupported(ccmd, dockerCli); err != nil {
|
||||
ccmd.Println(err)
|
||||
|
@ -227,10 +234,15 @@ func runDocker(dockerCli *command.DockerCli) error {
|
|||
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)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if len(args) > 0 {
|
||||
ccmd, _, err := cmd.Find(args)
|
||||
|
|
|
@ -83,7 +83,7 @@ func TestHelpBad(t *testing.T) {
|
|||
|
||||
res := icmd.RunCmd(run("help", "badmeta"))
|
||||
res.Assert(t, icmd.Expected{
|
||||
ExitCode: 0,
|
||||
ExitCode: 1,
|
||||
Out: icmd.None,
|
||||
})
|
||||
golden.Assert(t, res.Stderr(), "docker-help-badmeta-err.golden")
|
||||
|
@ -110,8 +110,8 @@ func TestBadHelp(t *testing.T) {
|
|||
res.Assert(t, icmd.Expected{
|
||||
ExitCode: 0,
|
||||
// This should be identical to the --help case above
|
||||
Out: usage,
|
||||
Err: shortHFlagDeprecated,
|
||||
Out: shortHFlagDeprecated + usage,
|
||||
Err: icmd.None,
|
||||
})
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue