Skip to content

Commit 8a7b733

Browse files
committed
chore(cli): validate various names locally
1 parent b8e14b0 commit 8a7b733

File tree

5 files changed

+55
-8
lines changed

5 files changed

+55
-8
lines changed

cli/create.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,11 @@ func (r *RootCmd) create() *serpent.Command {
6060
workspaceName, err = cliui.Prompt(inv, cliui.PromptOptions{
6161
Text: "Specify a name for your workspace:",
6262
Validate: func(workspaceName string) error {
63-
_, err = client.WorkspaceByOwnerAndName(inv.Context(), codersdk.Me, workspaceName, codersdk.WorkspaceOptions{})
63+
err = codersdk.NameValid(workspaceName)
64+
if err != nil {
65+
return xerrors.Errorf("Workspace name %s", err)
66+
}
67+
_, err = client.WorkspaceByOwnerAndName(inv.Context(), workspaceOwner, workspaceName, codersdk.WorkspaceOptions{})
6468
if err == nil {
6569
return xerrors.Errorf("A workspace already exists named %q!", workspaceName)
6670
}
@@ -71,7 +75,10 @@ func (r *RootCmd) create() *serpent.Command {
7175
return err
7276
}
7377
}
74-
78+
err = codersdk.NameValid(workspaceName)
79+
if err != nil {
80+
return xerrors.Errorf("Workspace name %s", err)
81+
}
7582
_, err = client.WorkspaceByOwnerAndName(inv.Context(), workspaceOwner, workspaceName, codersdk.WorkspaceOptions{})
7683
if err == nil {
7784
return xerrors.Errorf("A workspace already exists named %q!", workspaceName)

cli/organizationmanage.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ func (r *RootCmd) createOrganization() *serpent.Command {
3030
Handler: func(inv *serpent.Invocation) error {
3131
orgName := inv.Args[0]
3232

33+
err := codersdk.NameValid(orgName)
34+
if err != nil {
35+
return xerrors.Errorf("Organization name %w", err)
36+
}
37+
3338
// This check is not perfect since not all users can read all organizations.
3439
// So ignore the error and if the org already exists, prevent the user
3540
// from creating it.
@@ -38,7 +43,7 @@ func (r *RootCmd) createOrganization() *serpent.Command {
3843
return xerrors.Errorf("organization %q already exists", orgName)
3944
}
4045

41-
_, err := cliui.Prompt(inv, cliui.PromptOptions{
46+
_, err = cliui.Prompt(inv, cliui.PromptOptions{
4247
Text: fmt.Sprintf("Are you sure you want to create an organization with the name %s?\n%s",
4348
pretty.Sprint(cliui.DefaultStyles.Code, orgName),
4449
pretty.Sprint(cliui.BoldFmt(), "This action is irreversible."),

cli/templatepush.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"path/filepath"
1111
"strings"
1212
"time"
13-
"unicode/utf8"
1413

1514
"github.com/briandowns/spinner"
1615
"github.com/google/uuid"
@@ -57,8 +56,16 @@ func (r *RootCmd) templatePush() *serpent.Command {
5756
return err
5857
}
5958

60-
if utf8.RuneCountInString(name) > 32 {
61-
return xerrors.Errorf("Template name must be no more than 32 characters")
59+
err = codersdk.NameValid(name)
60+
if err != nil {
61+
return xerrors.Errorf("Template name %w", err)
62+
}
63+
64+
if versionName != "" {
65+
err = codersdk.TemplateVersionNameValid(versionName)
66+
if err != nil {
67+
return xerrors.Errorf("Template version name %w", err)
68+
}
6269
}
6370

6471
var createTemplate bool

cli/usercreate.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ func (r *RootCmd) userCreate() *serpent.Command {
4444
if username == "" {
4545
username, err = cliui.Prompt(inv, cliui.PromptOptions{
4646
Text: "Username:",
47+
Validate: func(username string) error {
48+
err = codersdk.NameValid(username)
49+
if err != nil {
50+
return xerrors.Errorf("Username %s", err)
51+
}
52+
return nil
53+
},
4754
})
4855
if err != nil {
4956
return err
@@ -144,7 +151,15 @@ Create a workspace `+pretty.Sprint(cliui.DefaultStyles.Code, "coder create")+`!
144151
Flag: "username",
145152
FlagShorthand: "u",
146153
Description: "Specifies a username for the new user.",
147-
Value: serpent.StringOf(&username),
154+
Value: serpent.Validate(serpent.StringOf(&username), func(username *serpent.String) error {
155+
if username.String() != "" {
156+
err := codersdk.NameValid(username.String())
157+
if err != nil {
158+
return xerrors.Errorf("Username %s", err)
159+
}
160+
}
161+
return nil
162+
}),
148163
},
149164
{
150165
Flag: "full-name",

enterprise/cli/groupcreate.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ func (r *RootCmd) groupCreate() *serpent.Command {
3535
return xerrors.Errorf("current organization: %w", err)
3636
}
3737

38+
err = codersdk.GroupNameValid(inv.Args[0])
39+
if err != nil {
40+
return xerrors.Errorf("Group name %w", err)
41+
}
42+
3843
group, err := client.CreateGroup(ctx, org.ID, codersdk.CreateGroupRequest{
3944
Name: inv.Args[0],
4045
DisplayName: displayName,
@@ -61,7 +66,15 @@ func (r *RootCmd) groupCreate() *serpent.Command {
6166
Flag: "display-name",
6267
Description: `Optional human friendly name for the group.`,
6368
Env: "CODER_DISPLAY_NAME",
64-
Value: serpent.StringOf(&displayName),
69+
Value: serpent.Validate(serpent.StringOf(&displayName), func(displayName *serpent.String) error {
70+
if displayName.String() != "" {
71+
err := codersdk.DisplayNameValid(displayName.String())
72+
if err != nil {
73+
return xerrors.Errorf("Group display name %w", err)
74+
}
75+
}
76+
return nil
77+
}),
6578
},
6679
}
6780
orgContext.AttachOptions(cmd)

0 commit comments

Comments
 (0)