diff --git a/cli/templatelist.go b/cli/templatelist.go index a8a1315a1f0c9..4691b8a94ebfc 100644 --- a/cli/templatelist.go +++ b/cli/templatelist.go @@ -13,6 +13,7 @@ func templateList() *cobra.Command { ) cmd := &cobra.Command{ Use: "list", + Short: "List all the templates available for the organization", Aliases: []string{"ls"}, RunE: func(cmd *cobra.Command, args []string) error { client, err := createClient(cmd) diff --git a/cli/templatelist_test.go b/cli/templatelist_test.go new file mode 100644 index 0000000000000..0f660e4bd4d79 --- /dev/null +++ b/cli/templatelist_test.go @@ -0,0 +1,65 @@ +package cli_test + +import ( + "testing" + + "github.com/stretchr/testify/require" + + "github.com/coder/coder/cli/clitest" + "github.com/coder/coder/coderd/coderdtest" + "github.com/coder/coder/pty/ptytest" +) + +func TestTemplateList(t *testing.T) { + t.Parallel() + t.Run("ListTemplates", func(t *testing.T) { + t.Parallel() + client := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerD: true}) + user := coderdtest.CreateFirstUser(t, client) + firstVersion := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil) + _ = coderdtest.AwaitTemplateVersionJob(t, client, firstVersion.ID) + firstTemplate := coderdtest.CreateTemplate(t, client, user.OrganizationID, firstVersion.ID) + + secondVersion := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil) + _ = coderdtest.AwaitTemplateVersionJob(t, client, secondVersion.ID) + secondTemplate := coderdtest.CreateTemplate(t, client, user.OrganizationID, secondVersion.ID) + + cmd, root := clitest.New(t, "templates", "list") + clitest.SetupConfig(t, client, root) + + pty := ptytest.New(t) + cmd.SetIn(pty.Input()) + cmd.SetOut(pty.Output()) + + errC := make(chan error) + go func() { + errC <- cmd.Execute() + }() + + require.NoError(t, <-errC) + + pty.ExpectMatch(firstTemplate.Name) + pty.ExpectMatch(secondTemplate.Name) + }) + t.Run("NoTemplates", func(t *testing.T) { + t.Parallel() + client := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerD: true}) + coderdtest.CreateFirstUser(t, client) + + cmd, root := clitest.New(t, "templates", "list") + clitest.SetupConfig(t, client, root) + + pty := ptytest.New(t) + cmd.SetIn(pty.Input()) + cmd.SetOut(pty.Output()) + + errC := make(chan error) + go func() { + errC <- cmd.Execute() + }() + + require.NoError(t, <-errC) + + pty.ExpectMatch("No templates found in testuser! Create one:") + }) +} diff --git a/cli/templateversions.go b/cli/templateversions.go index 668b7dbf4ca63..2d5844d685e39 100644 --- a/cli/templateversions.go +++ b/cli/templateversions.go @@ -1,15 +1,88 @@ package cli -import "github.com/spf13/cobra" +import ( + "fmt" + "strings" + + "github.com/google/uuid" + "github.com/jedib0t/go-pretty/v6/table" + "github.com/spf13/cobra" + "golang.org/x/xerrors" + + "github.com/coder/coder/cli/cliui" + "github.com/coder/coder/codersdk" +) func templateVersions() *cobra.Command { - return &cobra.Command{ + cmd := &cobra.Command{ Use: "versions", + Short: "Manage different versions of the specified template", Aliases: []string{"version"}, + Example: formatExamples( + example{ + Description: "List versions of a specific template", + Command: "coder templates versions list my-template", + }, + ), + } + cmd.AddCommand( + templateVersionsList(), + ) + + return cmd +} + +func templateVersionsList() *cobra.Command { + return &cobra.Command{ + Use: "list