Skip to content

Commit 292713c

Browse files
committed
move cli-plugins annotation consts to a separate package
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent 4321293 commit 292713c

File tree

8 files changed

+80
-46
lines changed

8 files changed

+80
-46
lines changed

‎cli-plugins/manager/annotations.go‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package manager
2+
3+
import "github.com/docker/cli/cli-plugins/metadata"
4+
5+
const (
6+
// CommandAnnotationPlugin is added to every stub command added by
7+
// AddPluginCommandStubs with the value "true" and so can be
8+
// used to distinguish plugin stubs from regular commands.
9+
CommandAnnotationPlugin = metadata.CommandAnnotationPlugin
10+
11+
// CommandAnnotationPluginVendor is added to every stub command
12+
// added by AddPluginCommandStubs and contains the vendor of
13+
// that plugin.
14+
CommandAnnotationPluginVendor = metadata.CommandAnnotationPluginVendor
15+
16+
// CommandAnnotationPluginVersion is added to every stub command
17+
// added by AddPluginCommandStubs and contains the version of
18+
// that plugin.
19+
CommandAnnotationPluginVersion = metadata.CommandAnnotationPluginVersion
20+
21+
// CommandAnnotationPluginInvalid is added to any stub command
22+
// added by AddPluginCommandStubs for an invalid command (that
23+
// is, one which failed it's candidate test) and contains the
24+
// reason for the failure.
25+
CommandAnnotationPluginInvalid = metadata.CommandAnnotationPluginInvalid
26+
27+
// CommandAnnotationPluginCommandPath is added to overwrite the
28+
// command path for a plugin invocation.
29+
CommandAnnotationPluginCommandPath = metadata.CommandAnnotationPluginCommandPath
30+
)

‎cli-plugins/manager/cobra.go‎

Lines changed: 5 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,37 +5,11 @@ import (
55
"os"
66
"sync"
77

8+
"github.com/docker/cli/cli-plugins/metadata"
89
"github.com/docker/cli/cli/config"
910
"github.com/spf13/cobra"
1011
)
1112

12-
const (
13-
// CommandAnnotationPlugin is added to every stub command added by
14-
// AddPluginCommandStubs with the value "true" and so can be
15-
// used to distinguish plugin stubs from regular commands.
16-
CommandAnnotationPlugin = "com.docker.cli.plugin"
17-
18-
// CommandAnnotationPluginVendor is added to every stub command
19-
// added by AddPluginCommandStubs and contains the vendor of
20-
// that plugin.
21-
CommandAnnotationPluginVendor = "com.docker.cli.plugin.vendor"
22-
23-
// CommandAnnotationPluginVersion is added to every stub command
24-
// added by AddPluginCommandStubs and contains the version of
25-
// that plugin.
26-
CommandAnnotationPluginVersion = "com.docker.cli.plugin.version"
27-
28-
// CommandAnnotationPluginInvalid is added to any stub command
29-
// added by AddPluginCommandStubs for an invalid command (that
30-
// is, one which failed it's candidate test) and contains the
31-
// reason for the failure.
32-
CommandAnnotationPluginInvalid = "com.docker.cli.plugin-invalid"
33-
34-
// CommandAnnotationPluginCommandPath is added to overwrite the
35-
// command path for a plugin invocation.
36-
CommandAnnotationPluginCommandPath = "com.docker.cli.plugin.command_path"
37-
)
38-
3913
var pluginCommandStubsOnce sync.Once
4014

4115
// AddPluginCommandStubs adds a stub cobra.Commands for each valid and invalid
@@ -54,12 +28,12 @@ func AddPluginCommandStubs(dockerCLI config.Provider, rootCmd *cobra.Command) (e
5428
vendor = "unknown"
5529
}
5630
annotations := map[string]string{
57-
CommandAnnotationPlugin: "true",
58-
CommandAnnotationPluginVendor: vendor,
59-
CommandAnnotationPluginVersion: p.Version,
31+
metadata.CommandAnnotationPlugin: "true",
32+
metadata.CommandAnnotationPluginVendor: vendor,
33+
metadata.CommandAnnotationPluginVersion: p.Version,
6034
}
6135
if p.Err != nil {
62-
annotations[CommandAnnotationPluginInvalid] = p.Err.Error()
36+
annotations[metadata.CommandAnnotationPluginInvalid] = p.Err.Error()
6337
}
6438
rootCmd.AddCommand(&cobra.Command{
6539
Use: p.Name,

‎cli-plugins/manager/manager.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,5 +248,5 @@ func PluginRunCommand(dockerCli config.Provider, name string, rootcmd *cobra.Com
248248

249249
// IsPluginCommand checks if the given cmd is a plugin-stub.
250250
func IsPluginCommand(cmd *cobra.Command) bool {
251-
return cmd.Annotations[CommandAnnotationPlugin] == "true"
251+
return cmd.Annotations[metadata.CommandAnnotationPlugin] == "true"
252252
}

‎cli-plugins/manager/telemetry.go‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"os"
66
"strings"
77

8+
"github.com/docker/cli/cli-plugins/metadata"
89
"github.com/spf13/cobra"
910
"go.opentelemetry.io/otel"
1011
"go.opentelemetry.io/otel/attribute"
@@ -26,7 +27,7 @@ const (
2627
)
2728

2829
func getPluginResourceAttributes(cmd *cobra.Command, plugin Plugin) attribute.Set {
29-
commandPath := cmd.Annotations[CommandAnnotationPluginCommandPath]
30+
commandPath := cmd.Annotations[metadata.CommandAnnotationPluginCommandPath]
3031
if commandPath == "" {
3132
commandPath = fmt.Sprintf("%s %s", cmd.CommandPath(), plugin.Name)
3233
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package metadata
2+
3+
const (
4+
// CommandAnnotationPlugin is added to every stub command added by
5+
// AddPluginCommandStubs with the value "true" and so can be
6+
// used to distinguish plugin stubs from regular commands.
7+
CommandAnnotationPlugin = "com.docker.cli.plugin"
8+
9+
// CommandAnnotationPluginVendor is added to every stub command
10+
// added by AddPluginCommandStubs and contains the vendor of
11+
// that plugin.
12+
CommandAnnotationPluginVendor = "com.docker.cli.plugin.vendor"
13+
14+
// CommandAnnotationPluginVersion is added to every stub command
15+
// added by AddPluginCommandStubs and contains the version of
16+
// that plugin.
17+
CommandAnnotationPluginVersion = "com.docker.cli.plugin.version"
18+
19+
// CommandAnnotationPluginInvalid is added to any stub command
20+
// added by AddPluginCommandStubs for an invalid command (that
21+
// is, one which failed it's candidate test) and contains the
22+
// reason for the failure.
23+
CommandAnnotationPluginInvalid = "com.docker.cli.plugin-invalid"
24+
25+
// CommandAnnotationPluginCommandPath is added to overwrite the
26+
// command path for a plugin invocation.
27+
CommandAnnotationPluginCommandPath = "com.docker.cli.plugin.command_path"
28+
)

‎cli/cobra.go‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"sort"
88
"strings"
99

10-
pluginmanager "github.com/docker/cli/cli-plugins/manager"
10+
"github.com/docker/cli/cli-plugins/metadata"
1111
"github.com/docker/cli/cli/command"
1212
cliflags "github.com/docker/cli/cli/flags"
1313
"github.com/docker/docker/pkg/homedir"
@@ -252,7 +252,7 @@ func hasAdditionalHelp(cmd *cobra.Command) bool {
252252
}
253253

254254
func isPlugin(cmd *cobra.Command) bool {
255-
return pluginmanager.IsPluginCommand(cmd)
255+
return cmd.Annotations[metadata.CommandAnnotationPlugin] == "true"
256256
}
257257

258258
func hasAliases(cmd *cobra.Command) bool {
@@ -356,9 +356,9 @@ func decoratedName(cmd *cobra.Command) string {
356356
}
357357

358358
func vendorAndVersion(cmd *cobra.Command) string {
359-
if vendor, ok := cmd.Annotations[pluginmanager.CommandAnnotationPluginVendor]; ok && isPlugin(cmd) {
359+
if vendor, ok := cmd.Annotations[metadata.CommandAnnotationPluginVendor]; ok && isPlugin(cmd) {
360360
version := ""
361-
if v, ok := cmd.Annotations[pluginmanager.CommandAnnotationPluginVersion]; ok && v != "" {
361+
if v, ok := cmd.Annotations[metadata.CommandAnnotationPluginVersion]; ok && v != "" {
362362
version = ", " + v
363363
}
364364
return fmt.Sprintf("(%s%s)", vendor, version)
@@ -417,7 +417,7 @@ func invalidPlugins(cmd *cobra.Command) []*cobra.Command {
417417
}
418418

419419
func invalidPluginReason(cmd *cobra.Command) string {
420-
return cmd.Annotations[pluginmanager.CommandAnnotationPluginInvalid]
420+
return cmd.Annotations[metadata.CommandAnnotationPluginInvalid]
421421
}
422422

423423
const usageTemplate = `Usage:

‎cli/cobra_test.go‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package cli
33
import (
44
"testing"
55

6-
pluginmanager "github.com/docker/cli/cli-plugins/manager"
6+
"github.com/docker/cli/cli-plugins/metadata"
77
"github.com/google/go-cmp/cmp/cmpopts"
88
"github.com/spf13/cobra"
99
"gotest.tools/v3/assert"
@@ -49,9 +49,9 @@ func TestVendorAndVersion(t *testing.T) {
4949
cmd := &cobra.Command{
5050
Use: "test",
5151
Annotations: map[string]string{
52-
pluginmanager.CommandAnnotationPlugin: "true",
53-
pluginmanager.CommandAnnotationPluginVendor: tc.vendor,
54-
pluginmanager.CommandAnnotationPluginVersion: tc.version,
52+
metadata.CommandAnnotationPlugin: "true",
53+
metadata.CommandAnnotationPluginVendor: tc.vendor,
54+
metadata.CommandAnnotationPluginVersion: tc.version,
5555
},
5656
}
5757
assert.Equal(t, vendorAndVersion(cmd), tc.expected)
@@ -69,8 +69,8 @@ func TestInvalidPlugin(t *testing.T) {
6969
assert.Assert(t, is.Len(invalidPlugins(root), 0))
7070

7171
sub1.Annotations = map[string]string{
72-
pluginmanager.CommandAnnotationPlugin: "true",
73-
pluginmanager.CommandAnnotationPluginInvalid: "foo",
72+
metadata.CommandAnnotationPlugin: "true",
73+
metadata.CommandAnnotationPluginInvalid: "foo",
7474
}
7575
root.AddCommand(sub1, sub2)
7676
sub1.AddCommand(sub1sub1, sub1sub2)
@@ -100,6 +100,6 @@ func TestDecoratedName(t *testing.T) {
100100
topLevelCommand := &cobra.Command{Use: "pluginTopLevelCommand"}
101101
root.AddCommand(topLevelCommand)
102102
assert.Equal(t, decoratedName(topLevelCommand), "pluginTopLevelCommand ")
103-
topLevelCommand.Annotations = map[string]string{pluginmanager.CommandAnnotationPlugin: "true"}
103+
topLevelCommand.Annotations = map[string]string{metadata.CommandAnnotationPlugin: "true"}
104104
assert.Equal(t, decoratedName(topLevelCommand), "pluginTopLevelCommand*")
105105
}

‎cmd/docker/builder.go‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"strings"
99

1010
pluginmanager "github.com/docker/cli/cli-plugins/manager"
11+
"github.com/docker/cli/cli-plugins/metadata"
1112
"github.com/docker/cli/cli/command"
1213
"github.com/docker/docker/api/types"
1314
"github.com/pkg/errors"
@@ -127,7 +128,7 @@ func processBuilder(dockerCli command.Cli, cmd *cobra.Command, args, osargs []st
127128
}
128129

129130
// overwrite the command path for this plugin using the alias name.
130-
cmd.Annotations[pluginmanager.CommandAnnotationPluginCommandPath] = strings.Join(append([]string{cmd.CommandPath()}, fwcmdpath...), " ")
131+
cmd.Annotations[metadata.CommandAnnotationPluginCommandPath] = strings.Join(append([]string{cmd.CommandPath()}, fwcmdpath...), " ")
131132

132133
return fwargs, fwosargs, envs, nil
133134
}

0 commit comments

Comments
 (0)