Skip to content

Commit 8c33b02

Browse files
authored
chore: include all templates in cli template list (#13841)
* chore: cli template list includes all templates Shows all accessible templates from all organizations
1 parent f927204 commit 8c33b02

File tree

4 files changed

+38
-25
lines changed

4 files changed

+38
-25
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: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,6 @@ func TestTemplateList(t *testing.T) {
8888
client := coderdtest.New(t, &coderdtest.Options{})
8989
owner := coderdtest.CreateFirstUser(t, client)
9090

91-
org, err := client.Organization(context.Background(), owner.OrganizationID)
92-
require.NoError(t, err)
93-
9491
templateAdmin, _ := coderdtest.CreateAnotherUser(t, client, owner.OrganizationID, rbac.RoleTemplateAdmin())
9592

9693
inv, root := clitest.New(t, "templates", "list")
@@ -110,8 +107,42 @@ func TestTemplateList(t *testing.T) {
110107

111108
require.NoError(t, <-errC)
112109

113-
pty.ExpectMatch("No templates found in")
114-
pty.ExpectMatch(org.Name)
110+
pty.ExpectMatch("No templates found")
115111
pty.ExpectMatch("Create one:")
116112
})
113+
114+
t.Run("MultiOrg", func(t *testing.T) {
115+
t.Parallel()
116+
client := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerDaemon: true})
117+
owner := coderdtest.CreateFirstUser(t, client)
118+
119+
// Template in the first organization
120+
firstVersion := coderdtest.CreateTemplateVersion(t, client, owner.OrganizationID, nil)
121+
_ = coderdtest.AwaitTemplateVersionJobCompleted(t, client, firstVersion.ID)
122+
_ = coderdtest.CreateTemplate(t, client, owner.OrganizationID, firstVersion.ID)
123+
124+
secondOrg := coderdtest.CreateOrganization(t, client, coderdtest.CreateOrganizationOptions{
125+
IncludeProvisionerDaemon: true,
126+
})
127+
secondVersion := coderdtest.CreateTemplateVersion(t, client, secondOrg.ID, nil)
128+
_ = coderdtest.CreateTemplate(t, client, secondOrg.ID, secondVersion.ID)
129+
130+
// Create a site wide template admin
131+
templateAdmin, _ := coderdtest.CreateAnotherUser(t, client, owner.OrganizationID, rbac.RoleTemplateAdmin())
132+
133+
inv, root := clitest.New(t, "templates", "list", "--output=json")
134+
clitest.SetupConfig(t, templateAdmin, root)
135+
136+
ctx, cancelFunc := context.WithTimeout(context.Background(), testutil.WaitLong)
137+
defer cancelFunc()
138+
139+
out := bytes.NewBuffer(nil)
140+
inv.Stdout = out
141+
err := inv.WithContext(ctx).Run()
142+
require.NoError(t, err)
143+
144+
var templates []codersdk.Template
145+
require.NoError(t, json.Unmarshal(out.Bytes(), &templates))
146+
require.Len(t, templates, 2)
147+
})
117148
}

cli/testdata/coder_templates_list_--help.golden

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@ USAGE:
88
Aliases: ls
99

1010
OPTIONS:
11-
-O, --org string, $CODER_ORGANIZATION
12-
Select which organization (uuid or name) to use.
13-
1411
-c, --column string-array (default: name,organization name,last updated,used by)
1512
Columns to display in table output. Available columns: name, created
1613
at, last updated, organization id, organization name, provisioner,

docs/cli/templates_list.md

Lines changed: 0 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)