(opts CreateOptions)
| 34 | } |
| 35 | |
| 36 | func (r *RootCmd) Create(opts CreateOptions) *serpent.Command { |
| 37 | var ( |
| 38 | templateName string |
| 39 | templateVersion string |
| 40 | presetName string |
| 41 | startAt string |
| 42 | stopAfter time.Duration |
| 43 | workspaceName string |
| 44 | |
| 45 | parameterFlags workspaceParameterFlags |
| 46 | autoUpdates string |
| 47 | copyParametersFrom string |
| 48 | noWait bool |
| 49 | // Organization context is only required if more than 1 template |
| 50 | // shares the same name across multiple organizations. |
| 51 | orgContext = NewOrganizationContext() |
| 52 | ) |
| 53 | cmd := &serpent.Command{ |
| 54 | Annotations: workspaceCommand, |
| 55 | Use: "create [workspace]", |
| 56 | Short: "Create a workspace", |
| 57 | Long: FormatExamples( |
| 58 | Example{ |
| 59 | Description: "Create a workspace for another user (if you have permission)", |
| 60 | Command: "coder create <username>/<workspace_name>", |
| 61 | }, |
| 62 | ), |
| 63 | Handler: func(inv *serpent.Invocation) error { |
| 64 | client, err := r.InitClient(inv) |
| 65 | if err != nil { |
| 66 | return err |
| 67 | } |
| 68 | |
| 69 | workspaceOwner := codersdk.Me |
| 70 | if len(inv.Args) >= 1 { |
| 71 | workspaceOwner, workspaceName, err = codersdk.SplitWorkspaceIdentifier(inv.Args[0]) |
| 72 | if err != nil { |
| 73 | return err |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | if workspaceName == "" { |
| 78 | workspaceName, err = cliui.Prompt(inv, cliui.PromptOptions{ |
| 79 | Text: "Specify a name for your workspace:", |
| 80 | Validate: func(workspaceName string) error { |
| 81 | err = codersdk.NameValid(workspaceName) |
| 82 | if err != nil { |
| 83 | return xerrors.Errorf("workspace name %q is invalid: %w", workspaceName, err) |
| 84 | } |
| 85 | _, err = client.WorkspaceByOwnerAndName(inv.Context(), workspaceOwner, workspaceName, codersdk.WorkspaceOptions{}) |
| 86 | if err == nil { |
| 87 | return xerrors.Errorf("a workspace already exists named %q", workspaceName) |
| 88 | } |
| 89 | return nil |
| 90 | }, |
| 91 | }) |
| 92 | if err != nil { |
| 93 | return err |
no test coverage detected