feat(codersdk): export name validators (#14551)

This commit is contained in:
Ethan
2024-09-04 18:34:39 +10:00
committed by GitHub
parent 01a904c133
commit 8f85464fe6
6 changed files with 59 additions and 135 deletions

View File

@ -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