mirror of
https://github.com/coder/coder.git
synced 2025-07-13 21:36:50 +00:00
chore: include all templates in cli template list (#13841)
* chore: cli template list includes all templates Shows all accessible templates from all organizations
This commit is contained in:
@ -11,7 +11,6 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func (r *RootCmd) templateList() *serpent.Command {
|
func (r *RootCmd) templateList() *serpent.Command {
|
||||||
orgContext := NewOrganizationContext()
|
|
||||||
formatter := cliui.NewOutputFormatter(
|
formatter := cliui.NewOutputFormatter(
|
||||||
cliui.TableFormat([]templateTableRow{}, []string{"name", "organization name", "last updated", "used by"}),
|
cliui.TableFormat([]templateTableRow{}, []string{"name", "organization name", "last updated", "used by"}),
|
||||||
cliui.JSONFormat(),
|
cliui.JSONFormat(),
|
||||||
@ -26,17 +25,13 @@ func (r *RootCmd) templateList() *serpent.Command {
|
|||||||
r.InitClient(client),
|
r.InitClient(client),
|
||||||
),
|
),
|
||||||
Handler: func(inv *serpent.Invocation) error {
|
Handler: func(inv *serpent.Invocation) error {
|
||||||
organization, err := orgContext.Selected(inv, client)
|
templates, err := client.Templates(inv.Context(), codersdk.TemplateFilter{})
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
templates, err := client.TemplatesByOrganization(inv.Context(), organization.ID)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(templates) == 0 {
|
if len(templates) == 0 {
|
||||||
_, _ = fmt.Fprintf(inv.Stderr, "%s No templates found in %s! Create one:\n\n", Caret, color.HiWhiteString(organization.Name))
|
_, _ = fmt.Fprintf(inv.Stderr, "%s No templates found! Create one:\n\n", Caret)
|
||||||
_, _ = fmt.Fprintln(inv.Stderr, color.HiMagentaString(" $ coder templates push <directory>\n"))
|
_, _ = fmt.Fprintln(inv.Stderr, color.HiMagentaString(" $ coder templates push <directory>\n"))
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -53,6 +48,5 @@ func (r *RootCmd) templateList() *serpent.Command {
|
|||||||
}
|
}
|
||||||
|
|
||||||
formatter.AttachOptions(&cmd.Options)
|
formatter.AttachOptions(&cmd.Options)
|
||||||
orgContext.AttachOptions(cmd)
|
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
@ -88,9 +88,6 @@ func TestTemplateList(t *testing.T) {
|
|||||||
client := coderdtest.New(t, &coderdtest.Options{})
|
client := coderdtest.New(t, &coderdtest.Options{})
|
||||||
owner := coderdtest.CreateFirstUser(t, client)
|
owner := coderdtest.CreateFirstUser(t, client)
|
||||||
|
|
||||||
org, err := client.Organization(context.Background(), owner.OrganizationID)
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
templateAdmin, _ := coderdtest.CreateAnotherUser(t, client, owner.OrganizationID, rbac.RoleTemplateAdmin())
|
templateAdmin, _ := coderdtest.CreateAnotherUser(t, client, owner.OrganizationID, rbac.RoleTemplateAdmin())
|
||||||
|
|
||||||
inv, root := clitest.New(t, "templates", "list")
|
inv, root := clitest.New(t, "templates", "list")
|
||||||
@ -110,8 +107,42 @@ func TestTemplateList(t *testing.T) {
|
|||||||
|
|
||||||
require.NoError(t, <-errC)
|
require.NoError(t, <-errC)
|
||||||
|
|
||||||
pty.ExpectMatch("No templates found in")
|
pty.ExpectMatch("No templates found")
|
||||||
pty.ExpectMatch(org.Name)
|
|
||||||
pty.ExpectMatch("Create one:")
|
pty.ExpectMatch("Create one:")
|
||||||
})
|
})
|
||||||
|
|
||||||
|
t.Run("MultiOrg", func(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
client := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerDaemon: true})
|
||||||
|
owner := coderdtest.CreateFirstUser(t, client)
|
||||||
|
|
||||||
|
// Template in the first organization
|
||||||
|
firstVersion := coderdtest.CreateTemplateVersion(t, client, owner.OrganizationID, nil)
|
||||||
|
_ = coderdtest.AwaitTemplateVersionJobCompleted(t, client, firstVersion.ID)
|
||||||
|
_ = coderdtest.CreateTemplate(t, client, owner.OrganizationID, firstVersion.ID)
|
||||||
|
|
||||||
|
secondOrg := coderdtest.CreateOrganization(t, client, coderdtest.CreateOrganizationOptions{
|
||||||
|
IncludeProvisionerDaemon: true,
|
||||||
|
})
|
||||||
|
secondVersion := coderdtest.CreateTemplateVersion(t, client, secondOrg.ID, nil)
|
||||||
|
_ = coderdtest.CreateTemplate(t, client, secondOrg.ID, secondVersion.ID)
|
||||||
|
|
||||||
|
// Create a site wide template admin
|
||||||
|
templateAdmin, _ := coderdtest.CreateAnotherUser(t, client, owner.OrganizationID, rbac.RoleTemplateAdmin())
|
||||||
|
|
||||||
|
inv, root := clitest.New(t, "templates", "list", "--output=json")
|
||||||
|
clitest.SetupConfig(t, templateAdmin, root)
|
||||||
|
|
||||||
|
ctx, cancelFunc := context.WithTimeout(context.Background(), testutil.WaitLong)
|
||||||
|
defer cancelFunc()
|
||||||
|
|
||||||
|
out := bytes.NewBuffer(nil)
|
||||||
|
inv.Stdout = out
|
||||||
|
err := inv.WithContext(ctx).Run()
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
var templates []codersdk.Template
|
||||||
|
require.NoError(t, json.Unmarshal(out.Bytes(), &templates))
|
||||||
|
require.Len(t, templates, 2)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
@ -8,9 +8,6 @@ USAGE:
|
|||||||
Aliases: ls
|
Aliases: ls
|
||||||
|
|
||||||
OPTIONS:
|
OPTIONS:
|
||||||
-O, --org string, $CODER_ORGANIZATION
|
|
||||||
Select which organization (uuid or name) to use.
|
|
||||||
|
|
||||||
-c, --column string-array (default: name,organization name,last updated,used by)
|
-c, --column string-array (default: name,organization name,last updated,used by)
|
||||||
Columns to display in table output. Available columns: name, created
|
Columns to display in table output. Available columns: name, created
|
||||||
at, last updated, organization id, organization name, provisioner,
|
at, last updated, organization id, organization name, provisioner,
|
||||||
|
9
docs/cli/templates_list.md
generated
9
docs/cli/templates_list.md
generated
@ -33,12 +33,3 @@ Columns to display in table output. Available columns: name, created at, last up
|
|||||||
| Default | <code>table</code> |
|
| Default | <code>table</code> |
|
||||||
|
|
||||||
Output format. Available formats: table, json.
|
Output format. Available formats: table, json.
|
||||||
|
|
||||||
### -O, --org
|
|
||||||
|
|
||||||
| | |
|
|
||||||
| ----------- | -------------------------------- |
|
|
||||||
| Type | <code>string</code> |
|
|
||||||
| Environment | <code>$CODER_ORGANIZATION</code> |
|
|
||||||
|
|
||||||
Select which organization (uuid or name) to use.
|
|
||||||
|
Reference in New Issue
Block a user