Skip to content

Commit 71088ae

Browse files
committed
chore: cli template list includes all templates
Shows all accessible templates from all organizations
1 parent 05fdb9c commit 71088ae

File tree

2 files changed

+37
-8
lines changed

2 files changed

+37
-8
lines changed

cli/templatelist.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
)
1212

1313
func (r *RootCmd) templateList() *serpent.Command {
14-
orgContext := NewOrganizationContext()
1514
formatter := cliui.NewOutputFormatter(
1615
cliui.TableFormat([]templateTableRow{}, []string{"name", "organization name", "last updated", "used by"}),
1716
cliui.JSONFormat(),
@@ -26,17 +25,13 @@ func (r *RootCmd) templateList() *serpent.Command {
2625
r.InitClient(client),
2726
),
2827
Handler: func(inv *serpent.Invocation) error {
29-
organization, err := orgContext.Selected(inv, client)
30-
if err != nil {
31-
return err
32-
}
33-
templates, err := client.TemplatesByOrganization(inv.Context(), organization.ID)
28+
templates, err := client.Templates(inv.Context(), codersdk.TemplateFilter{})
3429
if err != nil {
3530
return err
3631
}
3732

3833
if len(templates) == 0 {
39-
_, _ = fmt.Fprintf(inv.Stderr, "%s No templates found in %s! Create one:\n\n", Caret, color.HiWhiteString(organization.Name))
34+
_, _ = fmt.Fprintf(inv.Stderr, "%s No templates found! Create one:\n\n", Caret)
4035
_, _ = fmt.Fprintln(inv.Stderr, color.HiMagentaString(" $ coder templates push <directory>\n"))
4136
return nil
4237
}
@@ -53,6 +48,5 @@ func (r *RootCmd) templateList() *serpent.Command {
5348
}
5449

5550
formatter.AttachOptions(&cmd.Options)
56-
orgContext.AttachOptions(cmd)
5751
return cmd
5852
}

cli/templatelist_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,4 +114,39 @@ func TestTemplateList(t *testing.T) {
114114
pty.ExpectMatch(org.Name)
115115
pty.ExpectMatch("Create one:")
116116
})
117+
118+
t.Run("MultiOrg", func(t *testing.T) {
119+
t.Parallel()
120+
client := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerDaemon: true})
121+
owner := coderdtest.CreateFirstUser(t, client)
122+
123+
// Template in the first organization
124+
firstVersion := coderdtest.CreateTemplateVersion(t, client, owner.OrganizationID, nil)
125+
_ = coderdtest.AwaitTemplateVersionJobCompleted(t, client, firstVersion.ID)
126+
_ = coderdtest.CreateTemplate(t, client, owner.OrganizationID, firstVersion.ID)
127+
128+
secondOrg := coderdtest.CreateOrganization(t, client, coderdtest.CreateOrganizationOptions{
129+
IncludeProvisionerDaemon: true,
130+
})
131+
secondVersion := coderdtest.CreateTemplateVersion(t, client, secondOrg.ID, nil)
132+
_ = coderdtest.CreateTemplate(t, client, secondOrg.ID, secondVersion.ID)
133+
134+
// Create a site wide template admin
135+
templateAdmin, _ := coderdtest.CreateAnotherUser(t, client, owner.OrganizationID, rbac.RoleTemplateAdmin())
136+
137+
inv, root := clitest.New(t, "templates", "list", "--output=json")
138+
clitest.SetupConfig(t, templateAdmin, root)
139+
140+
ctx, cancelFunc := context.WithTimeout(context.Background(), testutil.WaitLong)
141+
defer cancelFunc()
142+
143+
out := bytes.NewBuffer(nil)
144+
inv.Stdout = out
145+
err := inv.WithContext(ctx).Run()
146+
require.NoError(t, err)
147+
148+
var templates []codersdk.Template
149+
require.NoError(t, json.Unmarshal(out.Bytes(), &templates))
150+
require.Len(t, templates, 2)
151+
})
117152
}

0 commit comments

Comments
 (0)