Skip to content

Commit

Permalink
feat: make 'templates update [name]' optional (#2761)
Browse files Browse the repository at this point in the history
- If the name is not specified the current working directory
  name is used or the name specified by "--directory". This
  reflects 'templates create" behavior.
  • Loading branch information
sreya committed Jul 1, 2022
1 parent 0dbfd26 commit 554d991
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 3 deletions.
13 changes: 10 additions & 3 deletions cli/templateupdate.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cli
import (
"fmt"
"os"
"path/filepath"
"time"

"github.com/briandowns/spinner"
Expand All @@ -24,8 +25,8 @@ func templateUpdate() *cobra.Command {
)

cmd := &cobra.Command{
Use: "update <template>",
Args: cobra.ExactArgs(1),
Use: "update [template]",
Args: cobra.MaximumNArgs(1),
Short: "Update the source-code of a template from the current directory or as specified by flag",
RunE: func(cmd *cobra.Command, args []string) error {
client, err := createClient(cmd)
Expand All @@ -36,7 +37,13 @@ func templateUpdate() *cobra.Command {
if err != nil {
return err
}
template, err := client.TemplateByName(cmd.Context(), organization.ID, args[0])

name := filepath.Base(directory)
if len(args) > 0 {
name = args[0]
}

template, err := client.TemplateByName(cmd.Context(), organization.ID, name)
if err != nil {
return err
}
Expand Down
55 changes: 55 additions & 0 deletions cli/templateupdate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cli_test

import (
"context"
"path/filepath"
"testing"

"github.com/google/uuid"
Expand Down Expand Up @@ -113,6 +114,7 @@ func TestTemplateUpdate(t *testing.T) {
user := coderdtest.CreateFirstUser(t, client)
version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil)
_ = coderdtest.AwaitTemplateVersionJob(t, client, version.ID)

template := coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID)

// Test the cli command.
Expand Down Expand Up @@ -152,6 +154,59 @@ func TestTemplateUpdate(t *testing.T) {
assert.Len(t, templateVersions, 2)
assert.NotEqual(t, template.ActiveVersionID, templateVersions[1].ID)
})

t.Run("UseWorkingDir", func(t *testing.T) {
t.Parallel()
client := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerD: true})
user := coderdtest.CreateFirstUser(t, client)
version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil)
_ = coderdtest.AwaitTemplateVersionJob(t, client, version.ID)

// Test the cli command.
source := clitest.CreateTemplateVersionSource(t, &echo.Responses{
Parse: echo.ParseComplete,
Provision: echo.ProvisionComplete,
})

template := coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID,
func(r *codersdk.CreateTemplateRequest) {
r.Name = filepath.Base(source)
})

// Don't pass the name of the template, it should use the
// directory of the source.
cmd, root := clitest.New(t, "templates", "update", "--directory", source, "--test.provisioner", string(database.ProvisionerTypeEcho))
clitest.SetupConfig(t, client, root)
pty := ptytest.New(t)
cmd.SetIn(pty.Input())
cmd.SetOut(pty.Output())

execDone := make(chan error)
go func() {
execDone <- cmd.Execute()
}()

matches := []struct {
match string
write string
}{
{match: "Upload", write: "yes"},
}
for _, m := range matches {
pty.ExpectMatch(m.match)
pty.WriteLine(m.write)
}

require.NoError(t, <-execDone)

// Assert that the template version changed.
templateVersions, err := client.TemplateVersionsByTemplate(context.Background(), codersdk.TemplateVersionsByTemplateRequest{
TemplateID: template.ID,
})
require.NoError(t, err)
assert.Len(t, templateVersions, 2)
assert.NotEqual(t, template.ActiveVersionID, templateVersions[1].ID)
})
}

func latestTemplateVersion(t *testing.T, client *codersdk.Client, templateID uuid.UUID) (codersdk.TemplateVersion, []codersdk.Parameter) {
Expand Down

0 comments on commit 554d991

Please sign in to comment.