@@ -2,10 +2,10 @@ package cli
2
2
3
3
import (
4
4
"fmt"
5
- "sort"
6
5
"time"
7
6
8
7
"github.com/spf13/cobra"
8
+ "golang.org/x/exp/slices"
9
9
"golang.org/x/xerrors"
10
10
11
11
"github.com/coder/coder/cli/cliflag"
@@ -17,50 +17,79 @@ import (
17
17
func workspaceCreate () * cobra.Command {
18
18
var (
19
19
workspaceName string
20
+ templateName string
20
21
)
21
22
cmd := & cobra.Command {
22
- Use : "create [template ]" ,
23
+ Use : "create [name ]" ,
23
24
Short : "Create a workspace from a template" ,
24
25
RunE : func (cmd * cobra.Command , args []string ) error {
25
26
client , err := createClient (cmd )
26
27
if err != nil {
27
28
return err
28
29
}
30
+
29
31
organization , err := currentOrganization (cmd , client )
30
32
if err != nil {
31
33
return err
32
34
}
33
35
34
- templateName := ""
35
36
if len (args ) >= 1 {
36
- templateName = args [0 ]
37
+ workspaceName = args [0 ]
38
+ }
39
+
40
+ if workspaceName == "" {
41
+ workspaceName , err = cliui .Prompt (cmd , cliui.PromptOptions {
42
+ Text : "Specify a name for your workspace:" ,
43
+ Validate : func (workspaceName string ) error {
44
+ _ , err = client .WorkspaceByName (cmd .Context (), codersdk .Me , workspaceName )
45
+ if err == nil {
46
+ return xerrors .Errorf ("A workspace already exists named %q!" , workspaceName )
47
+ }
48
+ return nil
49
+ },
50
+ })
51
+ if err != nil {
52
+ return err
53
+ }
54
+ }
55
+
56
+ _ , err = client .WorkspaceByName (cmd .Context (), codersdk .Me , workspaceName )
57
+ if err == nil {
58
+ return xerrors .Errorf ("A workspace already exists named %q!" , workspaceName )
37
59
}
38
60
39
61
var template codersdk.Template
40
62
if templateName == "" {
41
63
_ , _ = fmt .Fprintln (cmd .OutOrStdout (), cliui .Styles .Wrap .Render ("Select a template below to preview the provisioned infrastructure:" ))
42
64
43
- templateNames := []string {}
44
- templateByName := map [string ]codersdk.Template {}
45
65
templates , err := client .TemplatesByOrganization (cmd .Context (), organization .ID )
46
66
if err != nil {
47
67
return err
48
68
}
69
+
70
+ slices .SortFunc (templates , func (a , b codersdk.Template ) bool {
71
+ return a .WorkspaceOwnerCount > b .WorkspaceOwnerCount
72
+ })
73
+
74
+ templateNames := make ([]string , 0 , len (templates ))
75
+ templateByName := make (map [string ]codersdk.Template , len (templates ))
76
+
49
77
for _ , template := range templates {
50
78
templateName := template .Name
79
+
51
80
if template .WorkspaceOwnerCount > 0 {
52
81
developerText := "developer"
53
82
if template .WorkspaceOwnerCount != 1 {
54
83
developerText = "developers"
55
84
}
85
+
56
86
templateName += cliui .Styles .Placeholder .Render (fmt .Sprintf (" (used by %d %s)" , template .WorkspaceOwnerCount , developerText ))
57
87
}
88
+
58
89
templateNames = append (templateNames , templateName )
59
90
templateByName [templateName ] = template
60
91
}
61
- sort .Slice (templateNames , func (i , j int ) bool {
62
- return templateByName [templateNames [i ]].WorkspaceOwnerCount > templateByName [templateNames [j ]].WorkspaceOwnerCount
63
- })
92
+
64
93
// Move the cursor up a single line for nicer display!
65
94
option , err := cliui .Select (cmd , cliui.SelectOptions {
66
95
Options : templateNames ,
@@ -69,36 +98,13 @@ func workspaceCreate() *cobra.Command {
69
98
if err != nil {
70
99
return err
71
100
}
101
+
72
102
template = templateByName [option ]
73
103
} else {
74
104
template , err = client .TemplateByName (cmd .Context (), organization .ID , templateName )
75
105
if err != nil {
76
106
return xerrors .Errorf ("get template by name: %w" , err )
77
107
}
78
- if err != nil {
79
- return err
80
- }
81
- }
82
-
83
- if workspaceName == "" {
84
- workspaceName , err = cliui .Prompt (cmd , cliui.PromptOptions {
85
- Text : "Specify a name for your workspace:" ,
86
- Validate : func (workspaceName string ) error {
87
- _ , err = client .WorkspaceByName (cmd .Context (), codersdk .Me , workspaceName )
88
- if err == nil {
89
- return xerrors .Errorf ("A workspace already exists named %q!" , workspaceName )
90
- }
91
- return nil
92
- },
93
- })
94
- if err != nil {
95
- return err
96
- }
97
- }
98
-
99
- _ , err = client .WorkspaceByName (cmd .Context (), codersdk .Me , workspaceName )
100
- if err == nil {
101
- return xerrors .Errorf ("A workspace already exists named %q!" , workspaceName )
102
108
}
103
109
104
110
templateVersion , err := client .TemplateVersion (cmd .Context (), template .ActiveVersionID )
@@ -164,10 +170,12 @@ func workspaceCreate() *cobra.Command {
164
170
if err != nil {
165
171
return err
166
172
}
173
+
167
174
err = cliui .WorkspaceBuild (cmd .Context (), cmd .OutOrStdout (), client , workspace .LatestBuild .ID , before )
168
175
if err != nil {
169
176
return err
170
177
}
178
+
171
179
resources , err = client .WorkspaceResourcesByBuild (cmd .Context (), workspace .LatestBuild .ID )
172
180
if err != nil {
173
181
return err
@@ -179,11 +187,12 @@ func workspaceCreate() *cobra.Command {
179
187
if err != nil {
180
188
return err
181
189
}
190
+
182
191
_ , _ = fmt .Fprintf (cmd .OutOrStdout (), "The %s workspace has been created!\n " , cliui .Styles .Keyword .Render (workspace .Name ))
183
192
return nil
184
193
},
185
194
}
186
- cliflag .StringVarP (cmd .Flags (), & workspaceName , "name" , "n" , "CODER_WORKSPACE_NAME" , "" , "Specify a workspace name." )
187
195
196
+ cliflag .StringVarP (cmd .Flags (), & templateName , "template" , "t" , "CODER_TEMPLATE_NAME" , "" , "Specify a template name." )
188
197
return cmd
189
198
}
0 commit comments