From 292713c887b17339106e9f7f3647ed50bebc675d 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/manager/telemetry.go | 3 ++- cli-plugins/metadata/annotations.go | 28 ++++++++++++++++++++++ cli/cobra.go | 10 ++++---- cli/cobra_test.go | 14 +++++------ cmd/docker/builder.go | 3 ++- 8 files changed, 80 insertions(+), 46 deletions(-) create mode 100644 cli-plugins/manager/annotations.go create mode 100644 cli-plugins/metadata/annotations.go diff --git a/cli-plugins/manager/annotations.go b/cli-plugins/manager/annotations.go new file mode 100644 index 0000000000..8fc76b73c9 --- /dev/null +++ b/cli-plugins/manager/annotations.go @@ -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 +) diff --git a/cli-plugins/manager/cobra.go b/cli-plugins/manager/cobra.go index 756ede944d..ddf067be1d 100644 --- a/cli-plugins/manager/cobra.go +++ b/cli-plugins/manager/cobra.go @@ -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, diff --git a/cli-plugins/manager/manager.go b/cli-plugins/manager/manager.go index 8de4710779..4b553eae79 100644 --- a/cli-plugins/manager/manager.go +++ b/cli-plugins/manager/manager.go @@ -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" } diff --git a/cli-plugins/manager/telemetry.go b/cli-plugins/manager/telemetry.go index 3382d4d8d2..ab3dace414 100644 --- a/cli-plugins/manager/telemetry.go +++ b/cli-plugins/manager/telemetry.go @@ -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) } diff --git a/cli-plugins/metadata/annotations.go b/cli-plugins/metadata/annotations.go new file mode 100644 index 0000000000..1c7c6d1874 --- /dev/null +++ b/cli-plugins/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 aa7dbef44c..9cfcc616d6 100644 --- a/cli/cobra.go +++ b/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: diff --git a/cli/cobra_test.go b/cli/cobra_test.go index 4e0ea8e3e4..e1d4bcb2d2 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/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*") } diff --git a/cmd/docker/builder.go b/cmd/docker/builder.go index ecb73264e0..bf1ed5e23a 100644 --- a/cmd/docker/builder.go +++ b/cmd/docker/builder.go @@ -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 }