Skip to content

Commit e558b91

Browse files
authored
Merge pull request #5892 from thaJeztah/pluginmanager_smaller_interface
cli-plugins/manager: use shallower interface
2 parents a9e5309 + 4be2dde commit e558b91

File tree

5 files changed

+33
-22
lines changed

5 files changed

+33
-22
lines changed

cli-plugins/manager/cobra.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"os"
66
"sync"
77

8-
"github.com/docker/cli/cli/command"
8+
"github.com/docker/cli/cli/config"
99
"github.com/spf13/cobra"
1010
)
1111

@@ -41,10 +41,10 @@ var pluginCommandStubsOnce sync.Once
4141
// AddPluginCommandStubs adds a stub cobra.Commands for each valid and invalid
4242
// plugin. The command stubs will have several annotations added, see
4343
// `CommandAnnotationPlugin*`.
44-
func AddPluginCommandStubs(dockerCli command.Cli, rootCmd *cobra.Command) (err error) {
44+
func AddPluginCommandStubs(dockerCLI config.Provider, rootCmd *cobra.Command) (err error) {
4545
pluginCommandStubsOnce.Do(func() {
4646
var plugins []Plugin
47-
plugins, err = ListPlugins(dockerCli, rootCmd)
47+
plugins, err = ListPlugins(dockerCLI, rootCmd)
4848
if err != nil {
4949
return
5050
}
@@ -86,7 +86,7 @@ func AddPluginCommandStubs(dockerCli command.Cli, rootCmd *cobra.Command) (err e
8686
cargs = append(cargs, args...)
8787
cargs = append(cargs, toComplete)
8888
os.Args = cargs
89-
runCommand, runErr := PluginRunCommand(dockerCli, p.Name, cmd)
89+
runCommand, runErr := PluginRunCommand(dockerCLI, p.Name, cmd)
9090
if runErr != nil {
9191
return nil, cobra.ShellCompDirectiveError
9292
}

cli-plugins/manager/hooks.go

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import (
66
"strings"
77

88
"github.com/docker/cli/cli-plugins/hooks"
9-
"github.com/docker/cli/cli/command"
9+
"github.com/docker/cli/cli/config"
10+
"github.com/docker/cli/cli/config/configfile"
1011
"github.com/sirupsen/logrus"
1112
"github.com/spf13/cobra"
1213
"github.com/spf13/pflag"
@@ -29,49 +30,52 @@ type HookPluginData struct {
2930
// a main CLI command was executed. It calls the hook subcommand for all
3031
// present CLI plugins that declare support for hooks in their metadata and
3132
// parses/prints their responses.
32-
func RunCLICommandHooks(ctx context.Context, dockerCli command.Cli, rootCmd, subCommand *cobra.Command, cmdErrorMessage string) {
33+
func RunCLICommandHooks(ctx context.Context, dockerCLI config.Provider, rootCmd, subCommand *cobra.Command, cmdErrorMessage string) {
3334
commandName := strings.TrimPrefix(subCommand.CommandPath(), rootCmd.Name()+" ")
3435
flags := getCommandFlags(subCommand)
3536

36-
runHooks(ctx, dockerCli, rootCmd, subCommand, commandName, flags, cmdErrorMessage)
37+
runHooks(ctx, dockerCLI.ConfigFile(), rootCmd, subCommand, commandName, flags, cmdErrorMessage)
3738
}
3839

3940
// RunPluginHooks is the entrypoint for the hooks execution flow
4041
// after a plugin command was just executed by the CLI.
41-
func RunPluginHooks(ctx context.Context, dockerCli command.Cli, rootCmd, subCommand *cobra.Command, args []string) {
42+
func RunPluginHooks(ctx context.Context, dockerCLI config.Provider, rootCmd, subCommand *cobra.Command, args []string) {
4243
commandName := strings.Join(args, " ")
4344
flags := getNaiveFlags(args)
4445

45-
runHooks(ctx, dockerCli, rootCmd, subCommand, commandName, flags, "")
46+
runHooks(ctx, dockerCLI.ConfigFile(), rootCmd, subCommand, commandName, flags, "")
4647
}
4748

48-
func runHooks(ctx context.Context, dockerCli command.Cli, rootCmd, subCommand *cobra.Command, invokedCommand string, flags map[string]string, cmdErrorMessage string) {
49-
nextSteps := invokeAndCollectHooks(ctx, dockerCli, rootCmd, subCommand, invokedCommand, flags, cmdErrorMessage)
50-
51-
hooks.PrintNextSteps(dockerCli.Err(), nextSteps)
49+
func runHooks(ctx context.Context, cfg *configfile.ConfigFile, rootCmd, subCommand *cobra.Command, invokedCommand string, flags map[string]string, cmdErrorMessage string) {
50+
nextSteps := invokeAndCollectHooks(ctx, cfg, rootCmd, subCommand, invokedCommand, flags, cmdErrorMessage)
51+
hooks.PrintNextSteps(subCommand.ErrOrStderr(), nextSteps)
5252
}
5353

54-
func invokeAndCollectHooks(ctx context.Context, dockerCli command.Cli, rootCmd, subCmd *cobra.Command, subCmdStr string, flags map[string]string, cmdErrorMessage string) []string {
54+
func invokeAndCollectHooks(ctx context.Context, cfg *configfile.ConfigFile, rootCmd, subCmd *cobra.Command, subCmdStr string, flags map[string]string, cmdErrorMessage string) []string {
5555
// check if the context was cancelled before invoking hooks
5656
select {
5757
case <-ctx.Done():
5858
return nil
5959
default:
6060
}
6161

62-
pluginsCfg := dockerCli.ConfigFile().Plugins
62+
pluginsCfg := cfg.Plugins
6363
if pluginsCfg == nil {
6464
return nil
6565
}
6666

67+
pluginDirs, err := getPluginDirs(cfg)
68+
if err != nil {
69+
return nil
70+
}
6771
nextSteps := make([]string, 0, len(pluginsCfg))
6872
for pluginName, cfg := range pluginsCfg {
6973
match, ok := pluginMatch(cfg, subCmdStr)
7074
if !ok {
7175
continue
7276
}
7377

74-
p, err := GetPlugin(pluginName, dockerCli, rootCmd)
78+
p, err := getPlugin(pluginName, pluginDirs, rootCmd)
7579
if err != nil {
7680
continue
7781
}

cli-plugins/manager/manager.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"strings"
1010
"sync"
1111

12-
"github.com/docker/cli/cli/command"
1312
"github.com/docker/cli/cli/config"
1413
"github.com/docker/cli/cli/config/configfile"
1514
"github.com/fvbommel/sortorder"
@@ -115,12 +114,15 @@ func listPluginCandidates(dirs []string) map[string][]string {
115114
}
116115

117116
// GetPlugin returns a plugin on the system by its name
118-
func GetPlugin(name string, dockerCli command.Cli, rootcmd *cobra.Command) (*Plugin, error) {
119-
pluginDirs, err := getPluginDirs(dockerCli.ConfigFile())
117+
func GetPlugin(name string, dockerCLI config.Provider, rootcmd *cobra.Command) (*Plugin, error) {
118+
pluginDirs, err := getPluginDirs(dockerCLI.ConfigFile())
120119
if err != nil {
121120
return nil, err
122121
}
122+
return getPlugin(name, pluginDirs, rootcmd)
123+
}
123124

125+
func getPlugin(name string, pluginDirs []string, rootcmd *cobra.Command) (*Plugin, error) {
124126
candidates := listPluginCandidates(pluginDirs)
125127
if paths, ok := candidates[name]; ok {
126128
if len(paths) == 0 {
@@ -141,7 +143,7 @@ func GetPlugin(name string, dockerCli command.Cli, rootcmd *cobra.Command) (*Plu
141143
}
142144

143145
// ListPlugins produces a list of the plugins available on the system
144-
func ListPlugins(dockerCli command.Cli, rootcmd *cobra.Command) ([]Plugin, error) {
146+
func ListPlugins(dockerCli config.Provider, rootcmd *cobra.Command) ([]Plugin, error) {
145147
pluginDirs, err := getPluginDirs(dockerCli.ConfigFile())
146148
if err != nil {
147149
return nil, err
@@ -188,7 +190,7 @@ func ListPlugins(dockerCli command.Cli, rootcmd *cobra.Command) ([]Plugin, error
188190
// PluginRunCommand returns an "os/exec".Cmd which when .Run() will execute the named plugin.
189191
// The rootcmd argument is referenced to determine the set of builtin commands in order to detect conficts.
190192
// The error returned satisfies the IsNotFound() predicate if no plugin was found or if the first candidate plugin was invalid somehow.
191-
func PluginRunCommand(dockerCli command.Cli, name string, rootcmd *cobra.Command) (*exec.Cmd, error) {
193+
func PluginRunCommand(dockerCli config.Provider, name string, rootcmd *cobra.Command) (*exec.Cmd, error) {
192194
// This uses the full original args, not the args which may
193195
// have been provided by cobra to our caller. This is because
194196
// they lack e.g. global options which we must propagate here.

cli/command/cli.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ type Cli interface {
4646
Streams
4747
SetIn(in *streams.In)
4848
Apply(ops ...CLIOption) error
49-
ConfigFile() *configfile.ConfigFile
49+
config.Provider
5050
ServerInfo() ServerInfo
5151
DefaultVersion() string
5252
CurrentVersion() string

cli/config/config.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ func getHomeDir() string {
6969
return home
7070
}
7171

72+
// Provider defines an interface for providing the CLI config.
73+
type Provider interface {
74+
ConfigFile() *configfile.ConfigFile
75+
}
76+
7277
// Dir returns the directory the configuration file is stored in
7378
func Dir() string {
7479
initConfigDir.Do(func() {

0 commit comments

Comments
 (0)