2018-12-11 09:50:04 -05:00
|
|
|
package manager
|
|
|
|
|
|
|
|
import (
|
2022-03-30 09:27:25 -04:00
|
|
|
"fmt"
|
|
|
|
"os"
|
2023-03-27 22:08:07 -04:00
|
|
|
"sync"
|
2022-03-30 09:27:25 -04:00
|
|
|
|
2018-12-11 09:50:04 -05:00
|
|
|
"github.com/docker/cli/cli/command"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// CommandAnnotationPlugin is added to every stub command added by
|
|
|
|
// AddPluginCommandStubs with the value "true" and so can be
|
|
|
|
// used to distinguish plugin stubs from regular commands.
|
|
|
|
CommandAnnotationPlugin = "com.docker.cli.plugin"
|
|
|
|
|
|
|
|
// CommandAnnotationPluginVendor is added to every stub command
|
|
|
|
// added by AddPluginCommandStubs and contains the vendor of
|
|
|
|
// that plugin.
|
|
|
|
CommandAnnotationPluginVendor = "com.docker.cli.plugin.vendor"
|
|
|
|
|
2019-02-15 11:27:22 -05:00
|
|
|
// CommandAnnotationPluginVersion is added to every stub command
|
|
|
|
// added by AddPluginCommandStubs and contains the version of
|
|
|
|
// that plugin.
|
|
|
|
CommandAnnotationPluginVersion = "com.docker.cli.plugin.version"
|
|
|
|
|
2018-12-11 09:50:04 -05:00
|
|
|
// CommandAnnotationPluginInvalid is added to any stub command
|
|
|
|
// added by AddPluginCommandStubs for an invalid command (that
|
|
|
|
// is, one which failed it's candidate test) and contains the
|
|
|
|
// reason for the failure.
|
|
|
|
CommandAnnotationPluginInvalid = "com.docker.cli.plugin-invalid"
|
|
|
|
)
|
|
|
|
|
2023-03-27 22:08:07 -04:00
|
|
|
var pluginCommandStubsOnce sync.Once
|
|
|
|
|
2019-01-30 05:46:31 -05:00
|
|
|
// AddPluginCommandStubs adds a stub cobra.Commands for each valid and invalid
|
|
|
|
// plugin. The command stubs will have several annotations added, see
|
|
|
|
// `CommandAnnotationPlugin*`.
|
2023-03-27 22:08:07 -04:00
|
|
|
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
|
2018-12-11 09:50:04 -05:00
|
|
|
}
|
2023-03-27 22:08:07 -04:00
|
|
|
for _, p := range plugins {
|
|
|
|
p := p
|
|
|
|
vendor := p.Vendor
|
|
|
|
if vendor == "" {
|
|
|
|
vendor = "unknown"
|
|
|
|
}
|
|
|
|
annotations := map[string]string{
|
|
|
|
CommandAnnotationPlugin: "true",
|
|
|
|
CommandAnnotationPluginVendor: vendor,
|
|
|
|
CommandAnnotationPluginVersion: p.Version,
|
|
|
|
}
|
|
|
|
if p.Err != nil {
|
|
|
|
annotations[CommandAnnotationPluginInvalid] = p.Err.Error()
|
|
|
|
}
|
|
|
|
rootCmd.AddCommand(&cobra.Command{
|
|
|
|
Use: p.Name,
|
|
|
|
Short: p.ShortDescription,
|
|
|
|
Run: func(_ *cobra.Command, _ []string) {},
|
|
|
|
Annotations: annotations,
|
|
|
|
DisableFlagParsing: true,
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
flags := rootCmd.PersistentFlags()
|
|
|
|
flags.SetOutput(nil)
|
|
|
|
perr := flags.Parse(args)
|
|
|
|
if perr != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if flags.Changed("help") {
|
|
|
|
cmd.HelpFunc()(rootCmd, args)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return fmt.Errorf("docker: '%s' is not a docker command.\nSee 'docker --help'", cmd.Name())
|
|
|
|
},
|
|
|
|
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
|
|
|
// 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
|
|
|
|
}
|
2022-03-30 09:27:25 -04:00
|
|
|
return nil, cobra.ShellCompDirectiveError
|
2023-03-27 22:08:07 -04:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return err
|
2018-12-11 09:50:04 -05:00
|
|
|
}
|