Skip to content

Commit e45d313

Browse files
committed
Plugin may set itself as hidden
Signed-off-by: Alano Terblanche <[email protected]>
1 parent b5bac44 commit e45d313

File tree

4 files changed

+31
-0
lines changed

4 files changed

+31
-0
lines changed

cli-plugins/manager/cobra.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ func AddPluginCommandStubs(dockerCLI config.Provider, rootCmd *cobra.Command) (e
3838
rootCmd.AddCommand(&cobra.Command{
3939
Use: p.Name,
4040
Short: p.ShortDescription,
41+
Hidden: p.Hidden,
4142
Run: func(_ *cobra.Command, _ []string) {},
4243
Annotations: annotations,
4344
DisableFlagParsing: true,

cli-plugins/metadata/metadata.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,6 @@ type Metadata struct {
3333
ShortDescription string `json:",omitempty"`
3434
// URL is a pointer to the plugin's homepage.
3535
URL string `json:",omitempty"`
36+
// Hidden hides the plugin in completion and help message output.
37+
Hidden bool `json:",omitempty"`
3638
}

cli/cobra.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,9 @@ func allManagementSubCommands(cmd *cobra.Command) []*cobra.Command {
351351
cmds := []*cobra.Command{}
352352
for _, sub := range cmd.Commands() {
353353
if isPlugin(sub) {
354+
if sub.Hidden {
355+
continue
356+
}
354357
if invalidPluginReason(sub) == "" {
355358
cmds = append(cmds, sub)
356359
}

cli/cobra_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,31 @@ func TestInvalidPlugin(t *testing.T) {
5656
assert.DeepEqual(t, invalidPlugins(root), []*cobra.Command{sub1}, cmpopts.IgnoreUnexported(cobra.Command{}))
5757
}
5858

59+
func TestHiddenPlugin(t *testing.T) {
60+
root := &cobra.Command{Use: "root"}
61+
sub1 := &cobra.Command{
62+
Use: "sub1",
63+
Hidden: true,
64+
Annotations: map[string]string{
65+
metadata.CommandAnnotationPlugin: "true",
66+
},
67+
}
68+
69+
sub1sub1 := &cobra.Command{Use: "sub1sub1"}
70+
sub1sub2 := &cobra.Command{Use: "sub1sub2"}
71+
sub2 := &cobra.Command{
72+
Use: "sub2",
73+
Annotations: map[string]string{
74+
metadata.CommandAnnotationPlugin: "true",
75+
},
76+
}
77+
78+
root.AddCommand(sub1, sub2)
79+
sub1.AddCommand(sub1sub1, sub1sub2)
80+
81+
assert.DeepEqual(t, allManagementSubCommands(root), []*cobra.Command{sub2}, cmpopts.IgnoreUnexported(cobra.Command{}))
82+
}
83+
5984
func TestCommandAliases(t *testing.T) {
6085
root := &cobra.Command{Use: "root"}
6186
sub := &cobra.Command{Use: "subcommand", Aliases: []string{"alias1", "alias2"}}

0 commit comments

Comments
 (0)