Skip to content

Commit

Permalink
Adapt telemetry logic for command-mapping
Browse files Browse the repository at this point in the history
- When building the command-tree of each plugin, we include all root
commands since the plugin can provide many such commands; before a
plugin only provided its own name as a root command.

- When building the command-tree of each plugin, we also include any
applicable target. This is important so telemetry can map the command
provided by the user with the command tree.

- When saving a command invocation to telemetry, we need to pass the
entire command path, since the plugin name is no longer necessarily a
prefix in the invocation.

- Fix finding aliases by determinining the source command-path within
the plugin command-tree and invoking it with "-h".

- Clear the previous telemety command-tree if the last CLI that was run
was < 1.5.3. This is because the command-tree generated by 1.5.3 is
different than for previous versions.

Signed-off-by: Marc Khouzam <[email protected]>
  • Loading branch information
marckhouzam committed Jan 28, 2025
1 parent 1df0c5e commit 6b69507
Show file tree
Hide file tree
Showing 15 changed files with 866 additions and 202 deletions.
3 changes: 3 additions & 0 deletions pkg/cli/plugin_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,9 @@ func getCmdForPluginEx(p *PluginInfo, cmdName string, mapEntry *plugin.CommandMa
"scope": p.Scope,
"type": common.CommandTypePlugin,
"pluginInstallationPath": p.InstallationPath,
// Telemetry uses the below annotation to identify the source of the command
// so that it can invoke it directly from the plugin binary.
common.AnnotationForCmdSrcPath: strings.Join(srcHierarchy, " "),
},
Hidden: hidden,
Aliases: aliases,
Expand Down
69 changes: 66 additions & 3 deletions pkg/cli/plugin_cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/stretchr/testify/assert"

"github.com/vmware-tanzu/tanzu-cli/pkg/common"
"github.com/vmware-tanzu/tanzu-plugin-runtime/plugin"
)

Expand Down Expand Up @@ -43,10 +44,72 @@ func TestGetCmdForPlugin(t *testing.T) {
cmd := GetCmdForPlugin(pi)

err = cmd.Execute()
assert.Equal(cmd.Name(), pi.Name)
assert.Equal(cmd.Short, pi.Description)
assert.Equal(cmd.Aliases, pi.Aliases)
assert.Nil(err)

assert.Equal(pi.Name, cmd.Name())
assert.Equal(pi.Description, cmd.Short)
assert.Equal(pi.Aliases, cmd.Aliases)

annotations := cmd.Annotations
assert.Equal(5, len(annotations))
assert.Equal(string(pi.Group), annotations["group"])
assert.Equal(pi.Scope, annotations["scope"])
assert.Equal(common.CommandTypePlugin, annotations["type"])
assert.Equal(pi.InstallationPath, annotations["pluginInstallationPath"])
// No remapping in this test
assert.Equal("", annotations[common.AnnotationForCmdSrcPath])
}

func TestGetCmdForRemappedPlugin(t *testing.T) {
assert := assert.New(t)

dir, err := os.MkdirTemp("", "tanzu-cli-getcmd")
assert.Nil(err)
defer os.RemoveAll(dir)

path, err := setupFakePlugin(dir, "fakefoo", "")
assert.Nil(err)

const (
originalCmdName = "fakefoo"
renamedCmdName = "fakefoo2"
)

pi := &PluginInfo{
Name: originalCmdName,
Description: "Fake foo",
Group: plugin.SystemCmdGroup,
Aliases: []string{"ff"},
InstallationPath: path,
Hidden: true,
}

remapping := &plugin.CommandMapEntry{
SourceCommandPath: originalCmdName,
DestinationCommandPath: renamedCmdName,
Description: "Other desc",
Aliases: []string{"ff2"},
}

cmd := getCmdForPluginEx(pi, renamedCmdName, remapping)

err = cmd.Execute()
assert.Nil(err)

assert.Equal(renamedCmdName, cmd.Name())
assert.Equal(remapping.Description, cmd.Short)
assert.Equal(remapping.Aliases, cmd.Aliases)
// A remapped command should not be hidden even if the original command is hidden
assert.False(cmd.Hidden)

annotations := cmd.Annotations
assert.Equal(5, len(annotations))
assert.Equal(string(pi.Group), annotations["group"])
assert.Equal(pi.Scope, annotations["scope"])
assert.Equal(common.CommandTypePlugin, annotations["type"])
assert.Equal(pi.InstallationPath, annotations["pluginInstallationPath"])
// We should see the remapped command name in the annotations
assert.Equal(remapping.SourceCommandPath, annotations[common.AnnotationForCmdSrcPath])
}

func TestEnvForPlugin(t *testing.T) {
Expand Down
5 changes: 5 additions & 0 deletions pkg/common/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,8 @@ const CoreName = "core"

// CommandTypePlugin represents the command type is plugin
const CommandTypePlugin = "plugin"

// Command Annotations
const (
AnnotationForCmdSrcPath = "cmdSrcPath"
)
123 changes: 59 additions & 64 deletions pkg/fakes/plugin_cmd_tree_cache_fake.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/globalinit/catalog_cache_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func init() {

func triggerForPreCommandRemapping() bool {
// If the last executed CLI version is < 1.3.0, we need to refresh the plugin catalog.
return lastversion.GetLastExecutedCLIVersion() == lastversion.OlderThan1_3_0
return lastversion.IsLessThan(lastversion.Version1_3_0)
}

// refreshPluginCatalog reads the info from each installed plugin
Expand Down
2 changes: 1 addition & 1 deletion pkg/globalinit/plugin_discovery_source_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func init() {

func triggerForPluginDiscoverySourceUpdater() bool {
// If the last executed CLI version is < 1.3.0, we need to update the discovery image source
return lastversion.GetLastExecutedCLIVersion() == lastversion.OlderThan1_3_0
return lastversion.IsLessThan(lastversion.Version1_3_0)
}

// updatePluginDiscoverySource updates the plugin discovery source to point CLI to
Expand Down
Loading

0 comments on commit 6b69507

Please sign in to comment.