Skip to content

Commit 8f85464

Browse files
feat(codersdk): export name validators (coder#14551)
1 parent 01a904c commit 8f85464

File tree

6 files changed

+59
-135
lines changed

6 files changed

+59
-135
lines changed

cli/create.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,13 @@ 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 %q is invalid: %w", workspaceName, err)
66+
}
67+
_, err = client.WorkspaceByOwnerAndName(inv.Context(), workspaceOwner, workspaceName, codersdk.WorkspaceOptions{})
6468
if err == nil {
65-
return xerrors.Errorf("A workspace already exists named %q!", workspaceName)
69+
return xerrors.Errorf("a workspace already exists named %q", workspaceName)
6670
}
6771
return nil
6872
},
@@ -71,10 +75,13 @@ 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 %q is invalid: %w", workspaceName, err)
81+
}
7582
_, err = client.WorkspaceByOwnerAndName(inv.Context(), workspaceOwner, workspaceName, codersdk.WorkspaceOptions{})
7683
if err == nil {
77-
return xerrors.Errorf("A workspace already exists named %q!", workspaceName)
84+
return xerrors.Errorf("a workspace already exists named %q", workspaceName)
7885
}
7986

8087
var sourceWorkspace codersdk.Workspace

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 %q is invalid: %w", orgName, 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 %q is invalid: %w", name, err)
62+
}
63+
64+
if versionName != "" {
65+
err = codersdk.TemplateVersionNameValid(versionName)
66+
if err != nil {
67+
return xerrors.Errorf("template version name %q is invalid: %w", versionName, err)
68+
}
6269
}
6370

6471
var createTemplate bool

cli/usercreate.go

Lines changed: 17 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 %q is invalid: %w", username, err)
51+
}
52+
return nil
53+
},
4754
})
4855
if err != nil {
4956
return err
@@ -144,7 +151,16 @@ 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+
username := _username.String()
156+
if username != "" {
157+
err := codersdk.NameValid(username)
158+
if err != nil {
159+
return xerrors.Errorf("username %q is invalid: %w", username, err)
160+
}
161+
}
162+
return nil
163+
}),
148164
},
149165
{
150166
Flag: "full-name",

coderd/httpapi/name.go

Lines changed: 0 additions & 125 deletions
This file was deleted.

enterprise/cli/groupcreate.go

Lines changed: 15 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 %q is invalid: %w", inv.Args[0], err)
41+
}
42+
3843
group, err := client.CreateGroup(ctx, org.ID, codersdk.CreateGroupRequest{
3944
Name: inv.Args[0],
4045
DisplayName: displayName,
@@ -61,7 +66,16 @@ 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+
displayName := _displayName.String()
71+
if displayName != "" {
72+
err := codersdk.DisplayNameValid(displayName)
73+
if err != nil {
74+
return xerrors.Errorf("group display name %q is invalid: %w", displayName, err)
75+
}
76+
}
77+
return nil
78+
}),
6579
},
6680
}
6781
orgContext.AttachOptions(cmd)

0 commit comments

Comments
 (0)