Skip to content

Commit 554d991

Browse files
authored
feat: make 'templates update [name]' optional (#2761)
- If the name is not specified the current working directory name is used or the name specified by "--directory". This reflects 'templates create" behavior.
1 parent 0dbfd26 commit 554d991

File tree

2 files changed

+65
-3
lines changed

2 files changed

+65
-3
lines changed

cli/templateupdate.go

+10-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cli
33
import (
44
"fmt"
55
"os"
6+
"path/filepath"
67
"time"
78

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

2627
cmd := &cobra.Command{
27-
Use: "update <template>",
28-
Args: cobra.ExactArgs(1),
28+
Use: "update [template]",
29+
Args: cobra.MaximumNArgs(1),
2930
Short: "Update the source-code of a template from the current directory or as specified by flag",
3031
RunE: func(cmd *cobra.Command, args []string) error {
3132
client, err := createClient(cmd)
@@ -36,7 +37,13 @@ func templateUpdate() *cobra.Command {
3637
if err != nil {
3738
return err
3839
}
39-
template, err := client.TemplateByName(cmd.Context(), organization.ID, args[0])
40+
41+
name := filepath.Base(directory)
42+
if len(args) > 0 {
43+
name = args[0]
44+
}
45+
46+
template, err := client.TemplateByName(cmd.Context(), organization.ID, name)
4047
if err != nil {
4148
return err
4249
}

cli/templateupdate_test.go

+55
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cli_test
22

33
import (
44
"context"
5+
"path/filepath"
56
"testing"
67

78
"github.com/google/uuid"
@@ -113,6 +114,7 @@ func TestTemplateUpdate(t *testing.T) {
113114
user := coderdtest.CreateFirstUser(t, client)
114115
version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil)
115116
_ = coderdtest.AwaitTemplateVersionJob(t, client, version.ID)
117+
116118
template := coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID)
117119

118120
// Test the cli command.
@@ -152,6 +154,59 @@ func TestTemplateUpdate(t *testing.T) {
152154
assert.Len(t, templateVersions, 2)
153155
assert.NotEqual(t, template.ActiveVersionID, templateVersions[1].ID)
154156
})
157+
158+
t.Run("UseWorkingDir", func(t *testing.T) {
159+
t.Parallel()
160+
client := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerD: true})
161+
user := coderdtest.CreateFirstUser(t, client)
162+
version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil)
163+
_ = coderdtest.AwaitTemplateVersionJob(t, client, version.ID)
164+
165+
// Test the cli command.
166+
source := clitest.CreateTemplateVersionSource(t, &echo.Responses{
167+
Parse: echo.ParseComplete,
168+
Provision: echo.ProvisionComplete,
169+
})
170+
171+
template := coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID,
172+
func(r *codersdk.CreateTemplateRequest) {
173+
r.Name = filepath.Base(source)
174+
})
175+
176+
// Don't pass the name of the template, it should use the
177+
// directory of the source.
178+
cmd, root := clitest.New(t, "templates", "update", "--directory", source, "--test.provisioner", string(database.ProvisionerTypeEcho))
179+
clitest.SetupConfig(t, client, root)
180+
pty := ptytest.New(t)
181+
cmd.SetIn(pty.Input())
182+
cmd.SetOut(pty.Output())
183+
184+
execDone := make(chan error)
185+
go func() {
186+
execDone <- cmd.Execute()
187+
}()
188+
189+
matches := []struct {
190+
match string
191+
write string
192+
}{
193+
{match: "Upload", write: "yes"},
194+
}
195+
for _, m := range matches {
196+
pty.ExpectMatch(m.match)
197+
pty.WriteLine(m.write)
198+
}
199+
200+
require.NoError(t, <-execDone)
201+
202+
// Assert that the template version changed.
203+
templateVersions, err := client.TemplateVersionsByTemplate(context.Background(), codersdk.TemplateVersionsByTemplateRequest{
204+
TemplateID: template.ID,
205+
})
206+
require.NoError(t, err)
207+
assert.Len(t, templateVersions, 2)
208+
assert.NotEqual(t, template.ActiveVersionID, templateVersions[1].ID)
209+
})
155210
}
156211

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

0 commit comments

Comments
 (0)