Skip to content

Commit

Permalink
cli-plugins/manager: add IsPluginCommand(() utility
Browse files Browse the repository at this point in the history
This makes it more convenient to check if a command is a plugin-stub

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
  • Loading branch information
thaJeztah committed Sep 30, 2022
1 parent a496a7d commit 90f1238
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 6 deletions.
5 changes: 5 additions & 0 deletions cli-plugins/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,3 +224,8 @@ func PluginRunCommand(dockerCli command.Cli, name string, rootcmd *cobra.Command
}
return nil, errPluginNotFound(name)
}

// IsPluginCommand checks if the given cmd is a plugin-stub.
func IsPluginCommand(cmd *cobra.Command) bool {
return cmd.Annotations[CommandAnnotationPlugin] == "true"
}
2 changes: 1 addition & 1 deletion cli-plugins/manager/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func newPlugin(c Candidate, rootcmd *cobra.Command) (Plugin, error) {
// Ignore conflicts with commands which are
// just plugin stubs (i.e. from a previous
// call to AddPluginCommandStubs).
if p := cmd.Annotations[CommandAnnotationPlugin]; p == "true" {
if IsPluginCommand(cmd) {
continue
}
if cmd.Name() == p.Name {
Expand Down
2 changes: 1 addition & 1 deletion cli/cobra.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ func hasAdditionalHelp(cmd *cobra.Command) bool {
}

func isPlugin(cmd *cobra.Command) bool {
return cmd.Annotations[pluginmanager.CommandAnnotationPlugin] == "true"
return pluginmanager.IsPluginCommand(cmd)
}

func hasAliases(cmd *cobra.Command) bool {
Expand Down
2 changes: 1 addition & 1 deletion cmd/docker/aliases.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func processAliases(dockerCli command.Cli, cmd *cobra.Command, args, osArgs []st
return args, osArgs, errors.Errorf("not allowed to alias %q (allowed: %#v)", k, allowedAliases)
}
if c, _, err := cmd.Find(strings.Split(v, " ")); err == nil {
if c.Annotations[pluginmanager.CommandAnnotationPlugin] != "true" {
if !pluginmanager.IsPluginCommand(c) {
return args, osArgs, errors.Errorf("not allowed to alias with builtin %q as target", v)
}
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ 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 ccmd.Annotations[pluginmanager.CommandAnnotationPlugin] == "true" {
if pluginmanager.IsPluginCommand(ccmd) {
err := tryRunPluginHelp(dockerCli, ccmd, args)
if !pluginmanager.IsNotFound(err) {
ccmd.Println(err)
Expand Down Expand Up @@ -229,8 +229,8 @@ func runDocker(dockerCli *command.DockerCli) error {
}

if len(args) > 0 {
command, _, err := cmd.Find(args)
if err != nil || command.Annotations[pluginmanager.CommandAnnotationPlugin] == "true" {
ccmd, _, err := cmd.Find(args)
if err != nil || pluginmanager.IsPluginCommand(ccmd) {
err := tryPluginRun(dockerCli, cmd, args[0])
if !pluginmanager.IsNotFound(err) {
return err
Expand Down

0 comments on commit 90f1238

Please sign in to comment.