@@ -2,10 +2,10 @@ package cli
22
33import (
44 "fmt"
5- "sort"
65 "time"
76
87 "github.com/spf13/cobra"
8+ "golang.org/x/exp/slices"
99 "golang.org/x/xerrors"
1010
1111 "github.com/coder/coder/cli/cliflag"
@@ -17,50 +17,78 @@ import (
1717func workspaceCreate () * cobra.Command {
1818 var (
1919 workspaceName string
20+ templateName string
2021 )
2122 cmd := & cobra.Command {
22- Use : "create [template ]" ,
23+ Use : "create [name ]" ,
2324 Short : "Create a workspace from a template" ,
2425 RunE : func (cmd * cobra.Command , args []string ) error {
2526 client , err := createClient (cmd )
2627 if err != nil {
2728 return err
2829 }
30+
2931 organization , err := currentOrganization (cmd , client )
3032 if err != nil {
3133 return err
3234 }
3335
34- templateName := ""
3536 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 )
3759 }
3860
3961 var template codersdk.Template
4062 if templateName == "" {
4163 _ , _ = fmt .Fprintln (cmd .OutOrStdout (), cliui .Styles .Wrap .Render ("Select a template below to preview the provisioned infrastructure:" ))
4264
43- templateNames := []string {}
44- templateByName := map [string ]codersdk.Template {}
4565 templates , err := client .TemplatesByOrganization (cmd .Context (), organization .ID )
4666 if err != nil {
4767 return err
4868 }
69+
70+ templateNames := make ([]string , 0 , len (templates ))
71+ templateByName := make (map [string ]codersdk.Template , len (templates ))
72+
4973 for _ , template := range templates {
5074 templateName := template .Name
75+
5176 if template .WorkspaceOwnerCount > 0 {
5277 developerText := "developer"
5378 if template .WorkspaceOwnerCount != 1 {
5479 developerText = "developers"
5580 }
81+
5682 templateName += cliui .Styles .Placeholder .Render (fmt .Sprintf (" (used by %d %s)" , template .WorkspaceOwnerCount , developerText ))
5783 }
84+
5885 templateNames = append (templateNames , templateName )
5986 templateByName [templateName ] = template
6087 }
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
6390 })
91+
6492 // Move the cursor up a single line for nicer display!
6593 option , err := cliui .Select (cmd , cliui.SelectOptions {
6694 Options : templateNames ,
@@ -69,36 +97,13 @@ func workspaceCreate() *cobra.Command {
6997 if err != nil {
7098 return err
7199 }
100+
72101 template = templateByName [option ]
73102 } else {
74103 template , err = client .TemplateByName (cmd .Context (), organization .ID , templateName )
75104 if err != nil {
76105 return xerrors .Errorf ("get template by name: %w" , err )
77106 }
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 )
102107 }
103108
104109 templateVersion , err := client .TemplateVersion (cmd .Context (), template .ActiveVersionID )
@@ -164,10 +169,12 @@ func workspaceCreate() *cobra.Command {
164169 if err != nil {
165170 return err
166171 }
172+
167173 err = cliui .WorkspaceBuild (cmd .Context (), cmd .OutOrStdout (), client , workspace .LatestBuild .ID , before )
168174 if err != nil {
169175 return err
170176 }
177+
171178 resources , err = client .WorkspaceResourcesByBuild (cmd .Context (), workspace .LatestBuild .ID )
172179 if err != nil {
173180 return err
@@ -179,11 +186,12 @@ func workspaceCreate() *cobra.Command {
179186 if err != nil {
180187 return err
181188 }
189+
182190 _ , _ = fmt .Fprintf (cmd .OutOrStdout (), "The %s workspace has been created!\n " , cliui .Styles .Keyword .Render (workspace .Name ))
183191 return nil
184192 },
185193 }
186- cliflag .StringVarP (cmd .Flags (), & workspaceName , "name" , "n" , "CODER_WORKSPACE_NAME" , "" , "Specify a workspace name." )
187194
195+ cliflag .StringVarP (cmd .Flags (), & templateName , "template" , "t" , "CODER_TEMPLATE_NAME" , "" , "Specify a template name." )
188196 return cmd
189197}
0 commit comments