Skip to content

feat: make 'templates update [name]' optional #2761

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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