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,64 +32,69 @@ 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() {
|
||||||
if err != nil {
|
var plugins []Plugin
|
||||||
return err
|
plugins, err = ListPlugins(dockerCli, rootCmd)
|
||||||
}
|
if err != nil {
|
||||||
for _, p := range plugins {
|
return
|
||||||
p := p
|
|
||||||
vendor := p.Vendor
|
|
||||||
if vendor == "" {
|
|
||||||
vendor = "unknown"
|
|
||||||
}
|
}
|
||||||
annotations := map[string]string{
|
for _, p := range plugins {
|
||||||
CommandAnnotationPlugin: "true",
|
p := p
|
||||||
CommandAnnotationPluginVendor: vendor,
|
vendor := p.Vendor
|
||||||
CommandAnnotationPluginVersion: p.Version,
|
if vendor == "" {
|
||||||
}
|
vendor = "unknown"
|
||||||
if p.Err != nil {
|
}
|
||||||
annotations[CommandAnnotationPluginInvalid] = p.Err.Error()
|
annotations := map[string]string{
|
||||||
}
|
CommandAnnotationPlugin: "true",
|
||||||
rootCmd.AddCommand(&cobra.Command{
|
CommandAnnotationPluginVendor: vendor,
|
||||||
Use: p.Name,
|
CommandAnnotationPluginVersion: p.Version,
|
||||||
Short: p.ShortDescription,
|
}
|
||||||
Run: func(_ *cobra.Command, _ []string) {},
|
if p.Err != nil {
|
||||||
Annotations: annotations,
|
annotations[CommandAnnotationPluginInvalid] = p.Err.Error()
|
||||||
DisableFlagParsing: true,
|
}
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
rootCmd.AddCommand(&cobra.Command{
|
||||||
flags := rootCmd.PersistentFlags()
|
Use: p.Name,
|
||||||
flags.SetOutput(nil)
|
Short: p.ShortDescription,
|
||||||
err := flags.Parse(args)
|
Run: func(_ *cobra.Command, _ []string) {},
|
||||||
if err != nil {
|
Annotations: annotations,
|
||||||
return err
|
DisableFlagParsing: true,
|
||||||
}
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
if flags.Changed("help") {
|
flags := rootCmd.PersistentFlags()
|
||||||
cmd.HelpFunc()(rootCmd, args)
|
flags.SetOutput(nil)
|
||||||
return nil
|
perr := flags.Parse(args)
|
||||||
}
|
if perr != nil {
|
||||||
return fmt.Errorf("docker: '%s' is not a docker command.\nSee 'docker --help'", cmd.Name())
|
return err
|
||||||
},
|
}
|
||||||
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
if flags.Changed("help") {
|
||||||
// Delegate completion to plugin
|
cmd.HelpFunc()(rootCmd, args)
|
||||||
cargs := []string{p.Path, cobra.ShellCompRequestCmd, p.Name}
|
return nil
|
||||||
cargs = append(cargs, args...)
|
}
|
||||||
cargs = append(cargs, toComplete)
|
return fmt.Errorf("docker: '%s' is not a docker command.\nSee 'docker --help'", cmd.Name())
|
||||||
os.Args = cargs
|
},
|
||||||
runCommand, err := PluginRunCommand(dockerCli, p.Name, cmd)
|
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
||||||
if err != nil {
|
// Delegate completion to plugin
|
||||||
|
cargs := []string{p.Path, cobra.ShellCompRequestCmd, p.Name}
|
||||||
|
cargs = append(cargs, args...)
|
||||||
|
cargs = append(cargs, toComplete)
|
||||||
|
os.Args = cargs
|
||||||
|
runCommand, runErr := PluginRunCommand(dockerCli, p.Name, cmd)
|
||||||
|
if runErr != nil {
|
||||||
|
return nil, cobra.ShellCompDirectiveError
|
||||||
|
}
|
||||||
|
runErr = runCommand.Run()
|
||||||
|
if runErr == nil {
|
||||||
|
os.Exit(0) // plugin already rendered complete data
|
||||||
|
}
|
||||||
return nil, cobra.ShellCompDirectiveError
|
return nil, cobra.ShellCompDirectiveError
|
||||||
}
|
},
|
||||||
err = runCommand.Run()
|
})
|
||||||
if err == nil {
|
}
|
||||||
os.Exit(0) // plugin already rendered complete data
|
})
|
||||||
}
|
return err
|
||||||
return nil, cobra.ShellCompDirectiveError
|
|
||||||
},
|
|
||||||
})
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
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,13 +133,20 @@ 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)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
cmd.PrintErrf("unknown help topic: %v\n", ccmd.Name())
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := isSupported(ccmd, dockerCli); err != nil {
|
if err := isSupported(ccmd, dockerCli); err != nil {
|
||||||
|
@ -227,9 +234,14 @@ func runDocker(dockerCli *command.DockerCli) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = pluginmanager.AddPluginCommandStubs(dockerCli, cmd)
|
if cli.HasCompletionArg(args) {
|
||||||
if err != nil {
|
// We add plugin command stubs early only for completion. We don't
|
||||||
return err
|
// 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 {
|
if len(args) > 0 {
|
||||||
|
|
|
@ -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