Skip to content

Commit ecc6942

Browse files
authored
Merge pull request #6621 from thaJeztah/28.x_plugin_hide
[28.x backport] Plugin may set itself as hidden
2 parents 5306df3 + c475c69 commit ecc6942

File tree

4 files changed

+32
-5
lines changed

4 files changed

+32
-5
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: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -376,13 +376,10 @@ func orchestratorSubCommands(cmd *cobra.Command) []*cobra.Command {
376376
func allManagementSubCommands(cmd *cobra.Command) []*cobra.Command {
377377
cmds := []*cobra.Command{}
378378
for _, sub := range cmd.Commands() {
379-
if isPlugin(sub) {
380-
if invalidPluginReason(sub) == "" {
381-
cmds = append(cmds, sub)
382-
}
379+
if invalidPluginReason(sub) != "" {
383380
continue
384381
}
385-
if sub.IsAvailableCommand() && sub.HasSubCommands() {
382+
if sub.IsAvailableCommand() && (isPlugin(sub) || sub.HasSubCommands()) {
386383
cmds = append(cmds, sub)
387384
}
388385
}

cli/cobra_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,33 @@ 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+
Run: func(cmd *cobra.Command, args []string) {},
68+
}
69+
70+
sub1sub1 := &cobra.Command{Use: "sub1sub1"}
71+
sub1sub2 := &cobra.Command{Use: "sub1sub2"}
72+
sub2 := &cobra.Command{
73+
Use: "sub2",
74+
Annotations: map[string]string{
75+
metadata.CommandAnnotationPlugin: "true",
76+
},
77+
Run: func(cmd *cobra.Command, args []string) {},
78+
}
79+
80+
root.AddCommand(sub1, sub2)
81+
sub1.AddCommand(sub1sub1, sub1sub2)
82+
83+
assert.DeepEqual(t, allManagementSubCommands(root), []*cobra.Command{sub2}, cmpopts.IgnoreFields(cobra.Command{}, "Run"), cmpopts.IgnoreUnexported(cobra.Command{}))
84+
}
85+
5986
func TestCommandAliases(t *testing.T) {
6087
root := &cobra.Command{Use: "root"}
6188
sub := &cobra.Command{Use: "subcommand", Aliases: []string{"alias1", "alias2"}}

0 commit comments

Comments
 (0)