mirror of https://github.com/docker/cli.git
				
				
				
			move cli-plugins annotation consts to a separate package
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
		
							parent
							
								
									4321293972
								
							
						
					
					
						commit
						292713c887
					
				| 
						 | 
				
			
			@ -0,0 +1,30 @@
 | 
			
		|||
package manager
 | 
			
		||||
 | 
			
		||||
import "github.com/docker/cli/cli-plugins/metadata"
 | 
			
		||||
 | 
			
		||||
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 = metadata.CommandAnnotationPlugin
 | 
			
		||||
 | 
			
		||||
	// CommandAnnotationPluginVendor is added to every stub command
 | 
			
		||||
	// added by AddPluginCommandStubs and contains the vendor of
 | 
			
		||||
	// that plugin.
 | 
			
		||||
	CommandAnnotationPluginVendor = metadata.CommandAnnotationPluginVendor
 | 
			
		||||
 | 
			
		||||
	// CommandAnnotationPluginVersion is added to every stub command
 | 
			
		||||
	// added by AddPluginCommandStubs and contains the version of
 | 
			
		||||
	// that plugin.
 | 
			
		||||
	CommandAnnotationPluginVersion = metadata.CommandAnnotationPluginVersion
 | 
			
		||||
 | 
			
		||||
	// 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 = metadata.CommandAnnotationPluginInvalid
 | 
			
		||||
 | 
			
		||||
	// CommandAnnotationPluginCommandPath is added to overwrite the
 | 
			
		||||
	// command path for a plugin invocation.
 | 
			
		||||
	CommandAnnotationPluginCommandPath = metadata.CommandAnnotationPluginCommandPath
 | 
			
		||||
)
 | 
			
		||||
| 
						 | 
				
			
			@ -5,37 +5,11 @@ import (
 | 
			
		|||
	"os"
 | 
			
		||||
	"sync"
 | 
			
		||||
 | 
			
		||||
	"github.com/docker/cli/cli-plugins/metadata"
 | 
			
		||||
	"github.com/docker/cli/cli/config"
 | 
			
		||||
	"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"
 | 
			
		||||
 | 
			
		||||
	// CommandAnnotationPluginVersion is added to every stub command
 | 
			
		||||
	// added by AddPluginCommandStubs and contains the version of
 | 
			
		||||
	// that plugin.
 | 
			
		||||
	CommandAnnotationPluginVersion = "com.docker.cli.plugin.version"
 | 
			
		||||
 | 
			
		||||
	// 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"
 | 
			
		||||
 | 
			
		||||
	// CommandAnnotationPluginCommandPath is added to overwrite the
 | 
			
		||||
	// command path for a plugin invocation.
 | 
			
		||||
	CommandAnnotationPluginCommandPath = "com.docker.cli.plugin.command_path"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
var pluginCommandStubsOnce sync.Once
 | 
			
		||||
 | 
			
		||||
// AddPluginCommandStubs adds a stub cobra.Commands for each valid and invalid
 | 
			
		||||
| 
						 | 
				
			
			@ -54,12 +28,12 @@ func AddPluginCommandStubs(dockerCLI config.Provider, rootCmd *cobra.Command) (e
 | 
			
		|||
				vendor = "unknown"
 | 
			
		||||
			}
 | 
			
		||||
			annotations := map[string]string{
 | 
			
		||||
				CommandAnnotationPlugin:        "true",
 | 
			
		||||
				CommandAnnotationPluginVendor:  vendor,
 | 
			
		||||
				CommandAnnotationPluginVersion: p.Version,
 | 
			
		||||
				metadata.CommandAnnotationPlugin:        "true",
 | 
			
		||||
				metadata.CommandAnnotationPluginVendor:  vendor,
 | 
			
		||||
				metadata.CommandAnnotationPluginVersion: p.Version,
 | 
			
		||||
			}
 | 
			
		||||
			if p.Err != nil {
 | 
			
		||||
				annotations[CommandAnnotationPluginInvalid] = p.Err.Error()
 | 
			
		||||
				annotations[metadata.CommandAnnotationPluginInvalid] = p.Err.Error()
 | 
			
		||||
			}
 | 
			
		||||
			rootCmd.AddCommand(&cobra.Command{
 | 
			
		||||
				Use:                p.Name,
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -248,5 +248,5 @@ func PluginRunCommand(dockerCli config.Provider, name string, rootcmd *cobra.Com
 | 
			
		|||
 | 
			
		||||
// IsPluginCommand checks if the given cmd is a plugin-stub.
 | 
			
		||||
func IsPluginCommand(cmd *cobra.Command) bool {
 | 
			
		||||
	return cmd.Annotations[CommandAnnotationPlugin] == "true"
 | 
			
		||||
	return cmd.Annotations[metadata.CommandAnnotationPlugin] == "true"
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -5,6 +5,7 @@ import (
 | 
			
		|||
	"os"
 | 
			
		||||
	"strings"
 | 
			
		||||
 | 
			
		||||
	"github.com/docker/cli/cli-plugins/metadata"
 | 
			
		||||
	"github.com/spf13/cobra"
 | 
			
		||||
	"go.opentelemetry.io/otel"
 | 
			
		||||
	"go.opentelemetry.io/otel/attribute"
 | 
			
		||||
| 
						 | 
				
			
			@ -26,7 +27,7 @@ const (
 | 
			
		|||
)
 | 
			
		||||
 | 
			
		||||
func getPluginResourceAttributes(cmd *cobra.Command, plugin Plugin) attribute.Set {
 | 
			
		||||
	commandPath := cmd.Annotations[CommandAnnotationPluginCommandPath]
 | 
			
		||||
	commandPath := cmd.Annotations[metadata.CommandAnnotationPluginCommandPath]
 | 
			
		||||
	if commandPath == "" {
 | 
			
		||||
		commandPath = fmt.Sprintf("%s %s", cmd.CommandPath(), plugin.Name)
 | 
			
		||||
	}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -0,0 +1,28 @@
 | 
			
		|||
package metadata
 | 
			
		||||
 | 
			
		||||
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"
 | 
			
		||||
 | 
			
		||||
	// CommandAnnotationPluginVersion is added to every stub command
 | 
			
		||||
	// added by AddPluginCommandStubs and contains the version of
 | 
			
		||||
	// that plugin.
 | 
			
		||||
	CommandAnnotationPluginVersion = "com.docker.cli.plugin.version"
 | 
			
		||||
 | 
			
		||||
	// 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"
 | 
			
		||||
 | 
			
		||||
	// CommandAnnotationPluginCommandPath is added to overwrite the
 | 
			
		||||
	// command path for a plugin invocation.
 | 
			
		||||
	CommandAnnotationPluginCommandPath = "com.docker.cli.plugin.command_path"
 | 
			
		||||
)
 | 
			
		||||
							
								
								
									
										10
									
								
								cli/cobra.go
								
								
								
								
							
							
						
						
									
										10
									
								
								cli/cobra.go
								
								
								
								
							| 
						 | 
				
			
			@ -7,7 +7,7 @@ import (
 | 
			
		|||
	"sort"
 | 
			
		||||
	"strings"
 | 
			
		||||
 | 
			
		||||
	pluginmanager "github.com/docker/cli/cli-plugins/manager"
 | 
			
		||||
	"github.com/docker/cli/cli-plugins/metadata"
 | 
			
		||||
	"github.com/docker/cli/cli/command"
 | 
			
		||||
	cliflags "github.com/docker/cli/cli/flags"
 | 
			
		||||
	"github.com/docker/docker/pkg/homedir"
 | 
			
		||||
| 
						 | 
				
			
			@ -252,7 +252,7 @@ func hasAdditionalHelp(cmd *cobra.Command) bool {
 | 
			
		|||
}
 | 
			
		||||
 | 
			
		||||
func isPlugin(cmd *cobra.Command) bool {
 | 
			
		||||
	return pluginmanager.IsPluginCommand(cmd)
 | 
			
		||||
	return cmd.Annotations[metadata.CommandAnnotationPlugin] == "true"
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func hasAliases(cmd *cobra.Command) bool {
 | 
			
		||||
| 
						 | 
				
			
			@ -356,9 +356,9 @@ func decoratedName(cmd *cobra.Command) string {
 | 
			
		|||
}
 | 
			
		||||
 | 
			
		||||
func vendorAndVersion(cmd *cobra.Command) string {
 | 
			
		||||
	if vendor, ok := cmd.Annotations[pluginmanager.CommandAnnotationPluginVendor]; ok && isPlugin(cmd) {
 | 
			
		||||
	if vendor, ok := cmd.Annotations[metadata.CommandAnnotationPluginVendor]; ok && isPlugin(cmd) {
 | 
			
		||||
		version := ""
 | 
			
		||||
		if v, ok := cmd.Annotations[pluginmanager.CommandAnnotationPluginVersion]; ok && v != "" {
 | 
			
		||||
		if v, ok := cmd.Annotations[metadata.CommandAnnotationPluginVersion]; ok && v != "" {
 | 
			
		||||
			version = ", " + v
 | 
			
		||||
		}
 | 
			
		||||
		return fmt.Sprintf("(%s%s)", vendor, version)
 | 
			
		||||
| 
						 | 
				
			
			@ -417,7 +417,7 @@ func invalidPlugins(cmd *cobra.Command) []*cobra.Command {
 | 
			
		|||
}
 | 
			
		||||
 | 
			
		||||
func invalidPluginReason(cmd *cobra.Command) string {
 | 
			
		||||
	return cmd.Annotations[pluginmanager.CommandAnnotationPluginInvalid]
 | 
			
		||||
	return cmd.Annotations[metadata.CommandAnnotationPluginInvalid]
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const usageTemplate = `Usage:
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -3,7 +3,7 @@ package cli
 | 
			
		|||
import (
 | 
			
		||||
	"testing"
 | 
			
		||||
 | 
			
		||||
	pluginmanager "github.com/docker/cli/cli-plugins/manager"
 | 
			
		||||
	"github.com/docker/cli/cli-plugins/metadata"
 | 
			
		||||
	"github.com/google/go-cmp/cmp/cmpopts"
 | 
			
		||||
	"github.com/spf13/cobra"
 | 
			
		||||
	"gotest.tools/v3/assert"
 | 
			
		||||
| 
						 | 
				
			
			@ -49,9 +49,9 @@ func TestVendorAndVersion(t *testing.T) {
 | 
			
		|||
			cmd := &cobra.Command{
 | 
			
		||||
				Use: "test",
 | 
			
		||||
				Annotations: map[string]string{
 | 
			
		||||
					pluginmanager.CommandAnnotationPlugin:        "true",
 | 
			
		||||
					pluginmanager.CommandAnnotationPluginVendor:  tc.vendor,
 | 
			
		||||
					pluginmanager.CommandAnnotationPluginVersion: tc.version,
 | 
			
		||||
					metadata.CommandAnnotationPlugin:        "true",
 | 
			
		||||
					metadata.CommandAnnotationPluginVendor:  tc.vendor,
 | 
			
		||||
					metadata.CommandAnnotationPluginVersion: tc.version,
 | 
			
		||||
				},
 | 
			
		||||
			}
 | 
			
		||||
			assert.Equal(t, vendorAndVersion(cmd), tc.expected)
 | 
			
		||||
| 
						 | 
				
			
			@ -69,8 +69,8 @@ func TestInvalidPlugin(t *testing.T) {
 | 
			
		|||
	assert.Assert(t, is.Len(invalidPlugins(root), 0))
 | 
			
		||||
 | 
			
		||||
	sub1.Annotations = map[string]string{
 | 
			
		||||
		pluginmanager.CommandAnnotationPlugin:        "true",
 | 
			
		||||
		pluginmanager.CommandAnnotationPluginInvalid: "foo",
 | 
			
		||||
		metadata.CommandAnnotationPlugin:        "true",
 | 
			
		||||
		metadata.CommandAnnotationPluginInvalid: "foo",
 | 
			
		||||
	}
 | 
			
		||||
	root.AddCommand(sub1, sub2)
 | 
			
		||||
	sub1.AddCommand(sub1sub1, sub1sub2)
 | 
			
		||||
| 
						 | 
				
			
			@ -100,6 +100,6 @@ func TestDecoratedName(t *testing.T) {
 | 
			
		|||
	topLevelCommand := &cobra.Command{Use: "pluginTopLevelCommand"}
 | 
			
		||||
	root.AddCommand(topLevelCommand)
 | 
			
		||||
	assert.Equal(t, decoratedName(topLevelCommand), "pluginTopLevelCommand ")
 | 
			
		||||
	topLevelCommand.Annotations = map[string]string{pluginmanager.CommandAnnotationPlugin: "true"}
 | 
			
		||||
	topLevelCommand.Annotations = map[string]string{metadata.CommandAnnotationPlugin: "true"}
 | 
			
		||||
	assert.Equal(t, decoratedName(topLevelCommand), "pluginTopLevelCommand*")
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -8,6 +8,7 @@ import (
 | 
			
		|||
	"strings"
 | 
			
		||||
 | 
			
		||||
	pluginmanager "github.com/docker/cli/cli-plugins/manager"
 | 
			
		||||
	"github.com/docker/cli/cli-plugins/metadata"
 | 
			
		||||
	"github.com/docker/cli/cli/command"
 | 
			
		||||
	"github.com/docker/docker/api/types"
 | 
			
		||||
	"github.com/pkg/errors"
 | 
			
		||||
| 
						 | 
				
			
			@ -127,7 +128,7 @@ func processBuilder(dockerCli command.Cli, cmd *cobra.Command, args, osargs []st
 | 
			
		|||
	}
 | 
			
		||||
 | 
			
		||||
	// overwrite the command path for this plugin using the alias name.
 | 
			
		||||
	cmd.Annotations[pluginmanager.CommandAnnotationPluginCommandPath] = strings.Join(append([]string{cmd.CommandPath()}, fwcmdpath...), " ")
 | 
			
		||||
	cmd.Annotations[metadata.CommandAnnotationPluginCommandPath] = strings.Join(append([]string{cmd.CommandPath()}, fwcmdpath...), " ")
 | 
			
		||||
 | 
			
		||||
	return fwargs, fwosargs, envs, nil
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue