@@ -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,78 @@ 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
+ templateNames := make ([]string , 0 , len (templates ))
71
+ templateByName := make (map [string ]codersdk.Template , len (templates ))
72
+
49
73
for _ , template := range templates {
50
74
templateName := template .Name
75
+
51
76
if template .WorkspaceOwnerCount > 0 {
52
77
developerText := "developer"
53
78
if template .WorkspaceOwnerCount != 1 {
54
79
developerText = "developers"
55
80
}
81
+
56
82
templateName += cliui .Styles .Placeholder .Render (fmt .Sprintf (" (used by %d %s)" , template .WorkspaceOwnerCount , developerText ))
57
83
}
84
+
58
85
templateNames = append (templateNames , templateName )
59
86
templateByName [templateName ] = template
60
87
}
61
- sort . Slice (templateNames , func (i , j int ) bool {
62
- return templateByName [templateNames [ i ]] .WorkspaceOwnerCount > templateByName [templateNames [ j ] ].WorkspaceOwnerCount
88
+ slices . SortFunc (templateNames , func (a , b string ) bool {
89
+ return templateByName [a ] .WorkspaceOwnerCount > templateByName [b ].WorkspaceOwnerCount
63
90
})
91
+
64
92
// Move the cursor up a single line for nicer display!
65
93
option , err := cliui .Select (cmd , cliui.SelectOptions {
66
94
Options : templateNames ,
@@ -69,36 +97,13 @@ func workspaceCreate() *cobra.Command {
69
97
if err != nil {
70
98
return err
71
99
}
100
+
72
101
template = templateByName [option ]
73
102
} else {
74
103
template , err = client .TemplateByName (cmd .Context (), organization .ID , templateName )
75
104
if err != nil {
76
105
return xerrors .Errorf ("get template by name: %w" , err )
77
106
}
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
107
}
103
108
104
109
templateVersion , err := client .TemplateVersion (cmd .Context (), template .ActiveVersionID )
@@ -164,10 +169,12 @@ func workspaceCreate() *cobra.Command {
164
169
if err != nil {
165
170
return err
166
171
}
172
+
167
173
err = cliui .WorkspaceBuild (cmd .Context (), cmd .OutOrStdout (), client , workspace .LatestBuild .ID , before )
168
174
if err != nil {
169
175
return err
170
176
}
177
+
171
178
resources , err = client .WorkspaceResourcesByBuild (cmd .Context (), workspace .LatestBuild .ID )
172
179
if err != nil {
173
180
return err
@@ -179,11 +186,12 @@ func workspaceCreate() *cobra.Command {
179
186
if err != nil {
180
187
return err
181
188
}
189
+
182
190
_ , _ = fmt .Fprintf (cmd .OutOrStdout (), "The %s workspace has been created!\n " , cliui .Styles .Keyword .Render (workspace .Name ))
183
191
return nil
184
192
},
185
193
}
186
- cliflag .StringVarP (cmd .Flags (), & workspaceName , "name" , "n" , "CODER_WORKSPACE_NAME" , "" , "Specify a workspace name." )
187
194
195
+ cliflag .StringVarP (cmd .Flags (), & templateName , "template" , "t" , "CODER_TEMPLATE_NAME" , "" , "Specify a template name." )
188
196
return cmd
189
197
}
0 commit comments