Thanks to visit codestin.com
Credit goes to github.com

Skip to content

chore(cli): validate various names locally #14551

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions cli/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,13 @@ func (r *RootCmd) create() *serpent.Command {
workspaceName, err = cliui.Prompt(inv, cliui.PromptOptions{
Text: "Specify a name for your workspace:",
Validate: func(workspaceName string) error {
_, err = client.WorkspaceByOwnerAndName(inv.Context(), codersdk.Me, workspaceName, codersdk.WorkspaceOptions{})
err = codersdk.NameValid(workspaceName)
if err != nil {
return xerrors.Errorf("workspace name %q is invalid: %w", workspaceName, err)
}
_, err = client.WorkspaceByOwnerAndName(inv.Context(), workspaceOwner, workspaceName, codersdk.WorkspaceOptions{})
if err == nil {
return xerrors.Errorf("A workspace already exists named %q!", workspaceName)
return xerrors.Errorf("a workspace already exists named %q", workspaceName)
}
return nil
},
Expand All @@ -71,10 +75,13 @@ func (r *RootCmd) create() *serpent.Command {
return err
}
}

err = codersdk.NameValid(workspaceName)
if err != nil {
return xerrors.Errorf("workspace name %q is invalid: %w", workspaceName, err)
}
_, err = client.WorkspaceByOwnerAndName(inv.Context(), workspaceOwner, workspaceName, codersdk.WorkspaceOptions{})
if err == nil {
return xerrors.Errorf("A workspace already exists named %q!", workspaceName)
return xerrors.Errorf("a workspace already exists named %q", workspaceName)
}

var sourceWorkspace codersdk.Workspace
Expand Down
7 changes: 6 additions & 1 deletion cli/organizationmanage.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ func (r *RootCmd) createOrganization() *serpent.Command {
Handler: func(inv *serpent.Invocation) error {
orgName := inv.Args[0]

err := codersdk.NameValid(orgName)
if err != nil {
return xerrors.Errorf("organization name %q is invalid: %w", orgName, err)
}

// This check is not perfect since not all users can read all organizations.
// So ignore the error and if the org already exists, prevent the user
// from creating it.
Expand All @@ -38,7 +43,7 @@ func (r *RootCmd) createOrganization() *serpent.Command {
return xerrors.Errorf("organization %q already exists", orgName)
}

_, err := cliui.Prompt(inv, cliui.PromptOptions{
_, err = cliui.Prompt(inv, cliui.PromptOptions{
Text: fmt.Sprintf("Are you sure you want to create an organization with the name %s?\n%s",
pretty.Sprint(cliui.DefaultStyles.Code, orgName),
pretty.Sprint(cliui.BoldFmt(), "This action is irreversible."),
Expand Down
13 changes: 10 additions & 3 deletions cli/templatepush.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"path/filepath"
"strings"
"time"
"unicode/utf8"

"github.com/briandowns/spinner"
"github.com/google/uuid"
Expand Down Expand Up @@ -57,8 +56,16 @@ func (r *RootCmd) templatePush() *serpent.Command {
return err
}

if utf8.RuneCountInString(name) > 32 {
return xerrors.Errorf("Template name must be no more than 32 characters")
err = codersdk.NameValid(name)
if err != nil {
return xerrors.Errorf("template name %q is invalid: %w", name, err)
}

if versionName != "" {
err = codersdk.TemplateVersionNameValid(versionName)
if err != nil {
return xerrors.Errorf("template version name %q is invalid: %w", versionName, err)
}
}

var createTemplate bool
Expand Down
18 changes: 17 additions & 1 deletion cli/usercreate.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ func (r *RootCmd) userCreate() *serpent.Command {
if username == "" {
username, err = cliui.Prompt(inv, cliui.PromptOptions{
Text: "Username:",
Validate: func(username string) error {
err = codersdk.NameValid(username)
if err != nil {
return xerrors.Errorf("username %q is invalid: %w", username, err)
}
return nil
},
})
if err != nil {
return err
Expand Down Expand Up @@ -144,7 +151,16 @@ Create a workspace `+pretty.Sprint(cliui.DefaultStyles.Code, "coder create")+`!
Flag: "username",
FlagShorthand: "u",
Description: "Specifies a username for the new user.",
Value: serpent.StringOf(&username),
Value: serpent.Validate(serpent.StringOf(&username), func(_username *serpent.String) error {
username := _username.String()
if username != "" {
err := codersdk.NameValid(username)
if err != nil {
return xerrors.Errorf("username %q is invalid: %w", username, err)
}
}
return nil
}),
},
{
Flag: "full-name",
Expand Down
125 changes: 0 additions & 125 deletions coderd/httpapi/name.go

This file was deleted.

16 changes: 15 additions & 1 deletion enterprise/cli/groupcreate.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ func (r *RootCmd) groupCreate() *serpent.Command {
return xerrors.Errorf("current organization: %w", err)
}

err = codersdk.GroupNameValid(inv.Args[0])
if err != nil {
return xerrors.Errorf("group name %q is invalid: %w", inv.Args[0], err)
}

group, err := client.CreateGroup(ctx, org.ID, codersdk.CreateGroupRequest{
Name: inv.Args[0],
DisplayName: displayName,
Expand All @@ -61,7 +66,16 @@ func (r *RootCmd) groupCreate() *serpent.Command {
Flag: "display-name",
Description: `Optional human friendly name for the group.`,
Env: "CODER_DISPLAY_NAME",
Value: serpent.StringOf(&displayName),
Value: serpent.Validate(serpent.StringOf(&displayName), func(_displayName *serpent.String) error {
displayName := _displayName.String()
if displayName != "" {
err := codersdk.DisplayNameValid(displayName)
if err != nil {
return xerrors.Errorf("group display name %q is invalid: %w", displayName, err)
}
}
return nil
}),
},
}
orgContext.AttachOptions(cmd)
Expand Down
Loading