Skip to content

Commit

Permalink
Do not hide remapped subcommands (#831)
Browse files Browse the repository at this point in the history
If the plugin's descriptor Hidden field is set to true, only the root
command of the plugin should be hidden; any mapped subcommands should
stay visible.

Signed-off-by: Marc Khouzam <[email protected]>
  • Loading branch information
marckhouzam authored Nov 19, 2024
1 parent ef7601d commit 92c98e8
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
9 changes: 8 additions & 1 deletion pkg/cli/plugin_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ func getCmdForPluginEx(p *PluginInfo, cmdName string, mapEntry *plugin.CommandMa
var srcHierarchy, dstHierarchy []string
aliases := p.Aliases
description := p.Description
hidden := p.Hidden

if mapEntry != nil {
srcHierarchy = hierarchyFromPath(mapEntry.SourceCommandPath)
Expand All @@ -163,6 +164,12 @@ func getCmdForPluginEx(p *PluginInfo, cmdName string, mapEntry *plugin.CommandMa
// is not a toplevel command
if len(srcHierarchy) > 0 {
aliases = mapEntry.Aliases
// There currently is no "mapEntry.Hidden" field, so there is
// no way to hide a remapped subcommand.
// But we want to make sure a remapped subcommand is not
// hidden even if the plugin root command is hidden, so we force
// the hidden field to false for subcommands.
hidden = false
}
if mapEntry.Description != "" {
description = mapEntry.Description
Expand Down Expand Up @@ -192,7 +199,7 @@ func getCmdForPluginEx(p *PluginInfo, cmdName string, mapEntry *plugin.CommandMa
"type": common.CommandTypePlugin,
"pluginInstallationPath": p.InstallationPath,
},
Hidden: p.Hidden,
Hidden: hidden,
Aliases: aliases,
}

Expand Down
42 changes: 41 additions & 1 deletion pkg/command/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1075,6 +1075,7 @@ type fakePluginRemapAttributes struct {
supportedContextType []configtypes.ContextType
invokedAs []string
aliases []string
hidden bool
commandMap []plugin.CommandMapEntry
}

Expand All @@ -1094,7 +1095,7 @@ func setupFakePluginInfo(p fakePluginRemapAttributes, pluginDir string) *cli.Plu
Group: cmdGroup,
Version: "v0.1.0",
Aliases: []string{},
Hidden: false,
Hidden: p.hidden,
InstallationPath: filepath.Join(pluginDir, fmt.Sprintf("%s_%s", p.name, string(p.target))),
Target: p.target,
InvokedAs: []string{},
Expand Down Expand Up @@ -1890,6 +1891,23 @@ func TestCommandRemapping(t *testing.T) {
args: []string{"kubernetes", "dummy", "deeper", "say", "hello"},
expected: []string{"Remap of plugin into command tree (dummy) associated with another plugin is not supported"},
},
{
test: "plugin root command hidden even when remapped",
pluginVariants: []fakePluginRemapAttributes{
{
name: "dummy2",
target: configtypes.TargetGlobal,
hidden: true,
commandMap: []plugin.CommandMapEntry{
{
DestinationCommandPath: "dummy",
},
},
},
},
args: []string{"-h"},
unexpected: []string{"dummy"},
},
// --- Command level mapping tests
{
test: "command-level: plugin command is mapped to top level appears at top level",
Expand Down Expand Up @@ -2069,6 +2087,28 @@ func TestCommandRemapping(t *testing.T) {
args: []string{"sic", "arg1", "arg2"},
expected: []string{"args = (show-invoke-context arg1 arg2), context is ():(dummy):(show-invoke-context)"},
},
{
test: "command-level: subcommand not hidden even if plugin hidden",
pluginVariants: []fakePluginRemapAttributes{
{
name: "dummy2",
target: configtypes.TargetGlobal,
hidden: true,
commandMap: []plugin.CommandMapEntry{
{
DestinationCommandPath: "show-context",
SourceCommandPath: "show-invoke-context",
Description: "show context command",
},
},
},
},
args: []string{"-h"},
// The mapped command should not be hidden
expected: []string{"show-context"},
// The plugin command should be hidden
unexpected: []string{"dummy2"},
},
{
test: "when nothing under platform-engineering command group",
args: []string{"tpe"},
Expand Down

0 comments on commit 92c98e8

Please sign in to comment.