Merge pull request #5892 from thaJeztah/pluginmanager_smaller_interface
cli-plugins/manager: use shallower interface
This commit is contained in:
commit
e558b915c2
@ -5,7 +5,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/docker/cli/cli/command"
|
"github.com/docker/cli/cli/config"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -41,10 +41,10 @@ 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) (err error) {
|
func AddPluginCommandStubs(dockerCLI config.Provider, rootCmd *cobra.Command) (err error) {
|
||||||
pluginCommandStubsOnce.Do(func() {
|
pluginCommandStubsOnce.Do(func() {
|
||||||
var plugins []Plugin
|
var plugins []Plugin
|
||||||
plugins, err = ListPlugins(dockerCli, rootCmd)
|
plugins, err = ListPlugins(dockerCLI, rootCmd)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -86,7 +86,7 @@ func AddPluginCommandStubs(dockerCli command.Cli, rootCmd *cobra.Command) (err e
|
|||||||
cargs = append(cargs, args...)
|
cargs = append(cargs, args...)
|
||||||
cargs = append(cargs, toComplete)
|
cargs = append(cargs, toComplete)
|
||||||
os.Args = cargs
|
os.Args = cargs
|
||||||
runCommand, runErr := PluginRunCommand(dockerCli, p.Name, cmd)
|
runCommand, runErr := PluginRunCommand(dockerCLI, p.Name, cmd)
|
||||||
if runErr != nil {
|
if runErr != nil {
|
||||||
return nil, cobra.ShellCompDirectiveError
|
return nil, cobra.ShellCompDirectiveError
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,8 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/docker/cli/cli-plugins/hooks"
|
"github.com/docker/cli/cli-plugins/hooks"
|
||||||
"github.com/docker/cli/cli/command"
|
"github.com/docker/cli/cli/config"
|
||||||
|
"github.com/docker/cli/cli/config/configfile"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"github.com/spf13/pflag"
|
"github.com/spf13/pflag"
|
||||||
@ -29,29 +30,28 @@ type HookPluginData struct {
|
|||||||
// a main CLI command was executed. It calls the hook subcommand for all
|
// a main CLI command was executed. It calls the hook subcommand for all
|
||||||
// present CLI plugins that declare support for hooks in their metadata and
|
// present CLI plugins that declare support for hooks in their metadata and
|
||||||
// parses/prints their responses.
|
// parses/prints their responses.
|
||||||
func RunCLICommandHooks(ctx context.Context, dockerCli command.Cli, rootCmd, subCommand *cobra.Command, cmdErrorMessage string) {
|
func RunCLICommandHooks(ctx context.Context, dockerCLI config.Provider, rootCmd, subCommand *cobra.Command, cmdErrorMessage string) {
|
||||||
commandName := strings.TrimPrefix(subCommand.CommandPath(), rootCmd.Name()+" ")
|
commandName := strings.TrimPrefix(subCommand.CommandPath(), rootCmd.Name()+" ")
|
||||||
flags := getCommandFlags(subCommand)
|
flags := getCommandFlags(subCommand)
|
||||||
|
|
||||||
runHooks(ctx, dockerCli, rootCmd, subCommand, commandName, flags, cmdErrorMessage)
|
runHooks(ctx, dockerCLI.ConfigFile(), rootCmd, subCommand, commandName, flags, cmdErrorMessage)
|
||||||
}
|
}
|
||||||
|
|
||||||
// RunPluginHooks is the entrypoint for the hooks execution flow
|
// RunPluginHooks is the entrypoint for the hooks execution flow
|
||||||
// after a plugin command was just executed by the CLI.
|
// after a plugin command was just executed by the CLI.
|
||||||
func RunPluginHooks(ctx context.Context, dockerCli command.Cli, rootCmd, subCommand *cobra.Command, args []string) {
|
func RunPluginHooks(ctx context.Context, dockerCLI config.Provider, rootCmd, subCommand *cobra.Command, args []string) {
|
||||||
commandName := strings.Join(args, " ")
|
commandName := strings.Join(args, " ")
|
||||||
flags := getNaiveFlags(args)
|
flags := getNaiveFlags(args)
|
||||||
|
|
||||||
runHooks(ctx, dockerCli, rootCmd, subCommand, commandName, flags, "")
|
runHooks(ctx, dockerCLI.ConfigFile(), rootCmd, subCommand, commandName, flags, "")
|
||||||
}
|
}
|
||||||
|
|
||||||
func runHooks(ctx context.Context, dockerCli command.Cli, rootCmd, subCommand *cobra.Command, invokedCommand string, flags map[string]string, cmdErrorMessage string) {
|
func runHooks(ctx context.Context, cfg *configfile.ConfigFile, rootCmd, subCommand *cobra.Command, invokedCommand string, flags map[string]string, cmdErrorMessage string) {
|
||||||
nextSteps := invokeAndCollectHooks(ctx, dockerCli, rootCmd, subCommand, invokedCommand, flags, cmdErrorMessage)
|
nextSteps := invokeAndCollectHooks(ctx, cfg, rootCmd, subCommand, invokedCommand, flags, cmdErrorMessage)
|
||||||
|
hooks.PrintNextSteps(subCommand.ErrOrStderr(), nextSteps)
|
||||||
hooks.PrintNextSteps(dockerCli.Err(), nextSteps)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func invokeAndCollectHooks(ctx context.Context, dockerCli command.Cli, rootCmd, subCmd *cobra.Command, subCmdStr string, flags map[string]string, cmdErrorMessage string) []string {
|
func invokeAndCollectHooks(ctx context.Context, cfg *configfile.ConfigFile, rootCmd, subCmd *cobra.Command, subCmdStr string, flags map[string]string, cmdErrorMessage string) []string {
|
||||||
// check if the context was cancelled before invoking hooks
|
// check if the context was cancelled before invoking hooks
|
||||||
select {
|
select {
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
@ -59,11 +59,15 @@ func invokeAndCollectHooks(ctx context.Context, dockerCli command.Cli, rootCmd,
|
|||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
|
|
||||||
pluginsCfg := dockerCli.ConfigFile().Plugins
|
pluginsCfg := cfg.Plugins
|
||||||
if pluginsCfg == nil {
|
if pluginsCfg == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pluginDirs, err := getPluginDirs(cfg)
|
||||||
|
if err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
nextSteps := make([]string, 0, len(pluginsCfg))
|
nextSteps := make([]string, 0, len(pluginsCfg))
|
||||||
for pluginName, cfg := range pluginsCfg {
|
for pluginName, cfg := range pluginsCfg {
|
||||||
match, ok := pluginMatch(cfg, subCmdStr)
|
match, ok := pluginMatch(cfg, subCmdStr)
|
||||||
@ -71,7 +75,7 @@ func invokeAndCollectHooks(ctx context.Context, dockerCli command.Cli, rootCmd,
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
p, err := GetPlugin(pluginName, dockerCli, rootCmd)
|
p, err := getPlugin(pluginName, pluginDirs, rootCmd)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
@ -9,7 +9,6 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/docker/cli/cli/command"
|
|
||||||
"github.com/docker/cli/cli/config"
|
"github.com/docker/cli/cli/config"
|
||||||
"github.com/docker/cli/cli/config/configfile"
|
"github.com/docker/cli/cli/config/configfile"
|
||||||
"github.com/fvbommel/sortorder"
|
"github.com/fvbommel/sortorder"
|
||||||
@ -115,12 +114,15 @@ func listPluginCandidates(dirs []string) map[string][]string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// GetPlugin returns a plugin on the system by its name
|
// GetPlugin returns a plugin on the system by its name
|
||||||
func GetPlugin(name string, dockerCli command.Cli, rootcmd *cobra.Command) (*Plugin, error) {
|
func GetPlugin(name string, dockerCLI config.Provider, rootcmd *cobra.Command) (*Plugin, error) {
|
||||||
pluginDirs, err := getPluginDirs(dockerCli.ConfigFile())
|
pluginDirs, err := getPluginDirs(dockerCLI.ConfigFile())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
return getPlugin(name, pluginDirs, rootcmd)
|
||||||
|
}
|
||||||
|
|
||||||
|
func getPlugin(name string, pluginDirs []string, rootcmd *cobra.Command) (*Plugin, error) {
|
||||||
candidates := listPluginCandidates(pluginDirs)
|
candidates := listPluginCandidates(pluginDirs)
|
||||||
if paths, ok := candidates[name]; ok {
|
if paths, ok := candidates[name]; ok {
|
||||||
if len(paths) == 0 {
|
if len(paths) == 0 {
|
||||||
@ -141,7 +143,7 @@ func GetPlugin(name string, dockerCli command.Cli, rootcmd *cobra.Command) (*Plu
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ListPlugins produces a list of the plugins available on the system
|
// ListPlugins produces a list of the plugins available on the system
|
||||||
func ListPlugins(dockerCli command.Cli, rootcmd *cobra.Command) ([]Plugin, error) {
|
func ListPlugins(dockerCli config.Provider, rootcmd *cobra.Command) ([]Plugin, error) {
|
||||||
pluginDirs, err := getPluginDirs(dockerCli.ConfigFile())
|
pluginDirs, err := getPluginDirs(dockerCli.ConfigFile())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -188,7 +190,7 @@ func ListPlugins(dockerCli command.Cli, rootcmd *cobra.Command) ([]Plugin, error
|
|||||||
// PluginRunCommand returns an "os/exec".Cmd which when .Run() will execute the named plugin.
|
// PluginRunCommand returns an "os/exec".Cmd which when .Run() will execute the named plugin.
|
||||||
// The rootcmd argument is referenced to determine the set of builtin commands in order to detect conficts.
|
// The rootcmd argument is referenced to determine the set of builtin commands in order to detect conficts.
|
||||||
// The error returned satisfies the IsNotFound() predicate if no plugin was found or if the first candidate plugin was invalid somehow.
|
// The error returned satisfies the IsNotFound() predicate if no plugin was found or if the first candidate plugin was invalid somehow.
|
||||||
func PluginRunCommand(dockerCli command.Cli, name string, rootcmd *cobra.Command) (*exec.Cmd, error) {
|
func PluginRunCommand(dockerCli config.Provider, name string, rootcmd *cobra.Command) (*exec.Cmd, error) {
|
||||||
// This uses the full original args, not the args which may
|
// This uses the full original args, not the args which may
|
||||||
// have been provided by cobra to our caller. This is because
|
// have been provided by cobra to our caller. This is because
|
||||||
// they lack e.g. global options which we must propagate here.
|
// they lack e.g. global options which we must propagate here.
|
||||||
|
@ -46,7 +46,7 @@ type Cli interface {
|
|||||||
Streams
|
Streams
|
||||||
SetIn(in *streams.In)
|
SetIn(in *streams.In)
|
||||||
Apply(ops ...CLIOption) error
|
Apply(ops ...CLIOption) error
|
||||||
ConfigFile() *configfile.ConfigFile
|
config.Provider
|
||||||
ServerInfo() ServerInfo
|
ServerInfo() ServerInfo
|
||||||
DefaultVersion() string
|
DefaultVersion() string
|
||||||
CurrentVersion() string
|
CurrentVersion() string
|
||||||
|
@ -69,6 +69,11 @@ func getHomeDir() string {
|
|||||||
return home
|
return home
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Provider defines an interface for providing the CLI config.
|
||||||
|
type Provider interface {
|
||||||
|
ConfigFile() *configfile.ConfigFile
|
||||||
|
}
|
||||||
|
|
||||||
// Dir returns the directory the configuration file is stored in
|
// Dir returns the directory the configuration file is stored in
|
||||||
func Dir() string {
|
func Dir() string {
|
||||||
initConfigDir.Do(func() {
|
initConfigDir.Do(func() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user