From 3e8564be8072383df9f9e8c2197cc0d386773ffa Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Tue, 4 Mar 2025 23:33:21 +0100 Subject: [PATCH] move cli-plugins annotation consts to a separate package Signed-off-by: Sebastiaan van Stijn --- cli-plugins/manager/annotations.go | 30 ++++++++++++++++++ cli-plugins/manager/cobra.go | 36 +++------------------- cli-plugins/manager/manager.go | 2 +- cli-plugins/plugin/metadata/annotations.go | 28 +++++++++++++++++ cli/cobra.go | 10 +++--- cli/cobra_test.go | 14 ++++----- 6 files changed, 76 insertions(+), 44 deletions(-) create mode 100644 cli-plugins/manager/annotations.go create mode 100644 cli-plugins/plugin/metadata/annotations.go diff --git a/cli-plugins/manager/annotations.go b/cli-plugins/manager/annotations.go new file mode 100644 index 000000000000..4e3041ae8d79 --- /dev/null +++ b/cli-plugins/manager/annotations.go @@ -0,0 +1,30 @@ +package manager + +import "github.com/docker/cli/cli-plugins/plugin/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 +) diff --git a/cli-plugins/manager/cobra.go b/cli-plugins/manager/cobra.go index 3e2566fc59e8..2304d75e5272 100644 --- a/cli-plugins/manager/cobra.go +++ b/cli-plugins/manager/cobra.go @@ -5,36 +5,10 @@ import ( "os" "sync" + "github.com/docker/cli/cli-plugins/plugin/metadata" "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 @@ -53,12 +27,12 @@ func AddPluginCommandStubs(dockerCLI ConfigProvider, rootCmd *cobra.Command) (er 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, diff --git a/cli-plugins/manager/manager.go b/cli-plugins/manager/manager.go index 7cca31c1ed28..08c9708cf039 100644 --- a/cli-plugins/manager/manager.go +++ b/cli-plugins/manager/manager.go @@ -250,5 +250,5 @@ func PluginRunCommand(dockerCli ConfigProvider, name string, rootcmd *cobra.Comm // 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" } diff --git a/cli-plugins/plugin/metadata/annotations.go b/cli-plugins/plugin/metadata/annotations.go new file mode 100644 index 000000000000..1c7c6d18747b --- /dev/null +++ b/cli-plugins/plugin/metadata/annotations.go @@ -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" +) diff --git a/cli/cobra.go b/cli/cobra.go index bf0b88e2b8ad..10fb3d30a634 100644 --- a/cli/cobra.go +++ b/cli/cobra.go @@ -6,7 +6,7 @@ import ( "sort" "strings" - pluginmanager "github.com/docker/cli/cli-plugins/manager" + "github.com/docker/cli/cli-plugins/plugin/metadata" "github.com/docker/cli/cli/command" cliflags "github.com/docker/cli/cli/flags" "github.com/fvbommel/sortorder" @@ -251,7 +251,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 { @@ -355,9 +355,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) @@ -416,7 +416,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: diff --git a/cli/cobra_test.go b/cli/cobra_test.go index 4e0ea8e3e42e..cd2de4dbd8d8 100644 --- a/cli/cobra_test.go +++ b/cli/cobra_test.go @@ -3,7 +3,7 @@ package cli import ( "testing" - pluginmanager "github.com/docker/cli/cli-plugins/manager" + "github.com/docker/cli/cli-plugins/plugin/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*") }