Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update telemetry to support command-remapping #848

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions pkg/cli/plugin_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,10 @@ 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 within the plugin so that it can determine how
// to invoke this command 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"
)
Loading