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

Commit8a7b733

Browse files
committed
chore(cli): validate various names locally
1 parentb8e14b0 commit8a7b733

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(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 %s",err)
66+
}
67+
_,err=client.WorkspaceByOwnerAndName(inv.Context(),workspaceOwner,workspaceName, codersdk.WorkspaceOptions{})
6468
iferr==nil {
6569
returnxerrors.Errorf("A workspace already exists named %q!",workspaceName)
6670
}
@@ -71,7 +75,10 @@ func (r *RootCmd) create() *serpent.Command {
7175
returnerr
7276
}
7377
}
74-
78+
err=codersdk.NameValid(workspaceName)
79+
iferr!=nil {
80+
returnxerrors.Errorf("Workspace name %s",err)
81+
}
7582
_,err=client.WorkspaceByOwnerAndName(inv.Context(),workspaceOwner,workspaceName, codersdk.WorkspaceOptions{})
7683
iferr==nil {
7784
returnxerrors.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+
iferr!=nil {
35+
returnxerrors.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
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 %w",err)
62+
}
63+
64+
ifversionName!="" {
65+
err=codersdk.TemplateVersionNameValid(versionName)
66+
iferr!=nil {
67+
returnxerrors.Errorf("Template version name %w",err)
68+
}
6269
}
6370

6471
varcreateTemplatebool

‎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
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 %s",err)
51+
}
52+
returnnil
53+
},
4754
})
4855
iferr!=nil {
4956
returnerr
@@ -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+
ifusername.String()!="" {
156+
err:=codersdk.NameValid(username.String())
157+
iferr!=nil {
158+
returnxerrors.Errorf("Username %s",err)
159+
}
160+
}
161+
returnnil
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
returnxerrors.Errorf("current organization: %w",err)
3636
}
3737

38+
err=codersdk.GroupNameValid(inv.Args[0])
39+
iferr!=nil {
40+
returnxerrors.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+
ifdisplayName.String()!="" {
71+
err:=codersdk.DisplayNameValid(displayName.String())
72+
iferr!=nil {
73+
returnxerrors.Errorf("Group display name %w",err)
74+
}
75+
}
76+
returnnil
77+
}),
6578
},
6679
}
6780
orgContext.AttachOptions(cmd)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp