mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
feat(codersdk): export name validators (#14551)
This commit is contained in:
+11
-4
@@ -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
|
||||
},
|
||||
@@ -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
|
||||
|
||||
@@ -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.
|
||||
@@ -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."),
|
||||
|
||||
+10
-3
@@ -10,7 +10,6 @@ import (
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
"unicode/utf8"
|
||||
|
||||
"github.com/briandowns/spinner"
|
||||
"github.com/google/uuid"
|
||||
@@ -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
|
||||
|
||||
+17
-1
@@ -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
|
||||
@@ -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",
|
||||
|
||||
Reference in New Issue
Block a user