Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit4bc6cfd

Browse files
committed
feat(codersdk): export name validators
1 parent01a904c commit4bc6cfd

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(workspaceNamestring)error {
63-
_,err=client.WorkspaceByOwnerAndName(inv.Context(),codersdk.Me,workspaceName, codersdk.WorkspaceOptions{})
63+
err=codersdk.NameValid(workspaceName)
64+
iferr!=nil {
65+
returnxerrors.Errorf("workspace name %q is invalid: %w",workspaceName,err)
66+
}
67+
_,err=client.WorkspaceByOwnerAndName(inv.Context(),workspaceOwner,workspaceName, codersdk.WorkspaceOptions{})
6468
iferr==nil {
65-
returnxerrors.Errorf("A workspace already exists named %q!",workspaceName)
69+
returnxerrors.Errorf("a workspace already exists named %q",workspaceName)
6670
}
6771
returnnil
6872
},
@@ -71,10 +75,13 @@ func (r *RootCmd) create() *serpent.Command {
7175
returnerr
7276
}
7377
}
74-
78+
err=codersdk.NameValid(workspaceName)
79+
iferr!=nil {
80+
returnxerrors.Errorf("workspace name %q is invalid: %w",workspaceName,err)
81+
}
7582
_,err=client.WorkspaceByOwnerAndName(inv.Context(),workspaceOwner,workspaceName, codersdk.WorkspaceOptions{})
7683
iferr==nil {
77-
returnxerrors.Errorf("A workspace already exists named %q!",workspaceName)
84+
returnxerrors.Errorf("a workspace already exists named %q",workspaceName)
7885
}
7986

8087
varsourceWorkspace 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+
iferr!=nil {
35+
returnxerrors.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
returnxerrors.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
returnerr
5857
}
5958

60-
ifutf8.RuneCountInString(name)>32 {
61-
returnxerrors.Errorf("Template name must be no more than 32 characters")
59+
err=codersdk.NameValid(name)
60+
iferr!=nil {
61+
returnxerrors.Errorf("template name %q is invalid: %w",name,err)
62+
}
63+
64+
ifversionName!="" {
65+
err=codersdk.TemplateVersionNameValid(versionName)
66+
iferr!=nil {
67+
returnxerrors.Errorf("template version name %q is invalid: %w",versionName,err)
68+
}
6269
}
6370

6471
varcreateTemplatebool

‎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
ifusername=="" {
4545
username,err=cliui.Prompt(inv, cliui.PromptOptions{
4646
Text:"Username:",
47+
Validate:func(usernamestring)error {
48+
err=codersdk.NameValid(username)
49+
iferr!=nil {
50+
returnxerrors.Errorf("username %q is invalid: %w",username,err)
51+
}
52+
returnnil
53+
},
4754
})
4855
iferr!=nil {
4956
returnerr
@@ -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+
ifusername!="" {
157+
err:=codersdk.NameValid(username)
158+
iferr!=nil {
159+
returnxerrors.Errorf("username %q is invalid: %w",username,err)
160+
}
161+
}
162+
returnnil
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
returnxerrors.Errorf("current organization: %w",err)
3636
}
3737

38+
err=codersdk.GroupNameValid(inv.Args[0])
39+
iferr!=nil {
40+
returnxerrors.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+
ifdisplayName!="" {
72+
err:=codersdk.DisplayNameValid(displayName)
73+
iferr!=nil {
74+
returnxerrors.Errorf("group display name %q is invalid: %w",displayName,err)
75+
}
76+
}
77+
returnnil
78+
}),
6579
},
6680
}
6781
orgContext.AttachOptions(cmd)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp