Merge pull request #3796 from thaJeztah/add_is_plugincommand_utility

cli-plugins/manager: add IsPluginCommand(() utility
This commit is contained in:
Sebastiaan van Stijn 2022-09-30 11:58:54 +02:00 committed by GitHub
commit fed93645f1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 11 additions and 6 deletions

View File

@ -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"
}

View File

@ -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 {

View File

@ -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 {

View File

@ -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)
}
}

View File

@ -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)
@ -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