Skip to content

Commit 99cc84b

Browse files
authored
CLOUDP-337876: add aliases to plugins (#4229)
1 parent 7379d3f commit 99cc84b

File tree

5 files changed

+74
-4
lines changed

5 files changed

+74
-4
lines changed

internal/cli/plugin/list.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ func (opts *ListOps) Run() error {
2929
}
3030

3131
const listTemplate = `NAME DESCRIPTION VERSION COMMANDS {{range valueOrEmptySlice .}}
32-
{{.Name}} {{.Description}} {{.Version}} {{ (index .Commands 0).Name }} {{range slice .Commands 1 }}
33-
{{.Name}}{{end}}
32+
{{.Name}} {{.Description}} {{.Version}} {{- range $i, $cmd := .Commands}}{{if $i}}
33+
{{else}} {{end}}{{$cmd.Name}}{{formatAliases $cmd.Aliases}}{{end}}
3434
{{end}}
3535
`
3636

internal/cli/plugin/list_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package plugin
1616

1717
import (
1818
"bytes"
19+
"strings"
1920
"testing"
2021

2122
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/cli"
@@ -43,3 +44,32 @@ func TestList_Run(t *testing.T) {
4344
func TestList_Template(t *testing.T) {
4445
test.VerifyOutputTemplate(t, listTemplate, getTestPlugins(t).GetValidAndInvalidPlugins())
4546
}
47+
48+
func TestList_TemplateWithAliases(t *testing.T) {
49+
plugins := getTestPlugins(t)
50+
51+
listOpts := &ListOps{
52+
Opts: Opts{
53+
plugins: plugins,
54+
},
55+
OutputOpts: cli.OutputOpts{
56+
Template: listTemplate,
57+
OutWriter: new(bytes.Buffer),
58+
},
59+
}
60+
61+
if err := listOpts.Run(); err != nil {
62+
t.Fatalf("Run() unexpected error: %v", err)
63+
}
64+
65+
output := listOpts.OutWriter.(*bytes.Buffer).String()
66+
67+
// Check that aliases are included in the output
68+
if !strings.Contains(output, "[aliases: cmd1, c1]") {
69+
t.Errorf("Expected to find aliases for command1, but output was: %s", output)
70+
}
71+
72+
if !strings.Contains(output, "[aliases: tf]") {
73+
t.Errorf("Expected to find aliases for command3, but output was: %s", output)
74+
}
75+
}

internal/cli/plugin/plugin_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func getTestPlugins(t *testing.T) *plugin.ValidatedPlugins {
3737
Description: "plugin1 description",
3838
Version: version1,
3939
Commands: []*plugin.Command{
40-
{Name: "command1"},
40+
{Name: "command1", Aliases: []string{"cmd1", "c1"}},
4141
{Name: "command 2"},
4242
},
4343
Github: &plugin.Github{
@@ -50,7 +50,7 @@ func getTestPlugins(t *testing.T) *plugin.ValidatedPlugins {
5050
Description: "plugin2 description",
5151
Version: version2,
5252
Commands: []*plugin.Command{
53-
{Name: "command3"},
53+
{Name: "command3", Aliases: []string{"tf"}},
5454
{Name: "command4"},
5555
},
5656
Github: &plugin.Github{

internal/templatewriter/templatewriter.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package templatewriter
1717
import (
1818
"io"
1919
"reflect"
20+
"strings"
2021
"text/tabwriter"
2122
"text/template"
2223
)
@@ -30,6 +31,7 @@ const (
3031

3132
var funcMap = template.FuncMap{
3233
"valueOrEmptySlice": valueOrEmptySlice,
34+
"formatAliases": formatAliases,
3335
}
3436

3537
func valueOrEmptySlice(slice any) (result any) {
@@ -45,6 +47,16 @@ func valueOrEmptySlice(slice any) (result any) {
4547
return slice
4648
}
4749

50+
// formatAliases formats command aliases for display.
51+
// Returns a formatted string like " [aliases: cmd1, c2]" if aliases exist,
52+
// or an empty string if no aliases are present.
53+
func formatAliases(aliases []string) string {
54+
if len(aliases) == 0 {
55+
return ""
56+
}
57+
return " [aliases: " + strings.Join(aliases, ", ") + "]"
58+
}
59+
4860
// newTabWriter returns a tabwriter that handles tabs(`\t) to space columns evenly.
4961
func newTabWriter(output io.Writer) *tabwriter.Writer {
5062
return tabwriter.NewWriter(output, tabwriterMinWidth, tabwriterWidth, tabwriterPadding, tabwriterPadChar, 0)

internal/templatewriter/templatewriter_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,34 @@ func Test_Print(t *testing.T) {
8383
expected: "items: ",
8484
wantErr: require.NoError,
8585
},
86+
{
87+
name: "formatAliases with aliases",
88+
template: "command{{formatAliases .Aliases}}",
89+
data: struct{ Aliases []string }{Aliases: []string{"cmd1", "c1"}},
90+
expected: "command [aliases: cmd1, c1]",
91+
wantErr: require.NoError,
92+
},
93+
{
94+
name: "formatAliases with single alias",
95+
template: "command{{formatAliases .Aliases}}",
96+
data: struct{ Aliases []string }{Aliases: []string{"tf"}},
97+
expected: "command [aliases: tf]",
98+
wantErr: require.NoError,
99+
},
100+
{
101+
name: "formatAliases with no aliases",
102+
template: "command{{formatAliases .Aliases}}",
103+
data: struct{ Aliases []string }{Aliases: []string{}},
104+
expected: "command",
105+
wantErr: require.NoError,
106+
},
107+
{
108+
name: "formatAliases with nil aliases",
109+
template: "command{{formatAliases .Aliases}}",
110+
data: struct{ Aliases []string }{Aliases: nil},
111+
expected: "command",
112+
wantErr: require.NoError,
113+
},
86114
}
87115

88116
for _, conf := range tests {

0 commit comments

Comments
 (0)