Skip to content

Commit 1359850

Browse files
authored
feat(cli): validate name length on template create (coder#3823)
* feat(cli): add template create validation test This adds a test to validate that `template create` prints an error message if called with a template name exceeding the 32-char limit. * fixup * fixup test * feat(cli): add name validation to templatecreate This adds a validation step to ensure the template name is less than 32 characters. * fixup!: use utf8.RuneCountInString * fixup!: remove pty from test
1 parent 720c9da commit 1359850

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

cli/templatecreate.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"path/filepath"
77
"strings"
88
"time"
9+
"unicode/utf8"
910

1011
"github.com/briandowns/spinner"
1112
"github.com/spf13/cobra"
@@ -49,6 +50,10 @@ func templateCreate() *cobra.Command {
4950
templateName = args[0]
5051
}
5152

53+
if utf8.RuneCountInString(templateName) > 31 {
54+
return xerrors.Errorf("Template name must be less than 32 characters")
55+
}
56+
5257
_, err = client.TemplateByName(cmd.Context(), organization.ID, templateName)
5358
if err == nil {
5459
return xerrors.Errorf("A template already exists named %q!", templateName)

cli/templatecreate_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,21 @@ func TestTemplateCreate(t *testing.T) {
241241
err = create()
242242
require.NoError(t, err, "Template must be recreated without error")
243243
})
244+
245+
t.Run("WithParameterExceedingCharLimit", func(t *testing.T) {
246+
t.Parallel()
247+
client := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerD: true})
248+
coderdtest.CreateFirstUser(t, client)
249+
cmd, root := clitest.New(t, "templates", "create", "1234567890123456789012345678901234567891", "--test.provisioner", string(database.ProvisionerTypeEcho))
250+
clitest.SetupConfig(t, client, root)
251+
252+
execDone := make(chan error)
253+
go func() {
254+
execDone <- cmd.Execute()
255+
}()
256+
257+
require.EqualError(t, <-execDone, "Template name must be less than 32 characters")
258+
})
244259
}
245260

246261
func createTestParseResponse() []*proto.Parse_Response {

0 commit comments

Comments
 (0)