@@ -14,6 +14,15 @@ import (
14
14
"golang.org/x/xerrors"
15
15
)
16
16
17
+ const (
18
+ defaultOrg = "default"
19
+ defaultImgTag = "latest"
20
+ defaultCPUCores float32 = 1
21
+ defaultMemGB float32 = 1
22
+ defaultDiskGB = 10
23
+ defaultGPUs = 0
24
+ )
25
+
17
26
func envsCommand () * cobra.Command {
18
27
var outputFmt string
19
28
var user string
@@ -64,9 +73,9 @@ func envsCommand() *cobra.Command {
64
73
lsCmd .Flags ().StringVarP (& outputFmt , "output" , "o" , "human" , "human | json" )
65
74
cmd .AddCommand (lsCmd )
66
75
cmd .AddCommand (stopEnvCommand (& user ))
67
-
68
76
cmd .AddCommand (watchBuildLogCommand ())
69
77
cmd .AddCommand (rebuildEnvCommand ())
78
+ cmd .AddCommand (createEnvCommand ())
70
79
return cmd
71
80
}
72
81
@@ -125,3 +134,76 @@ coder envs --user charlie@coder.com ls -o json \
125
134
},
126
135
}
127
136
}
137
+
138
+ func createEnvCommand () * cobra.Command {
139
+ var (
140
+ org string
141
+ img string
142
+ tag string
143
+ follow bool
144
+ )
145
+
146
+ cmd := & cobra.Command {
147
+ Use : "create [environment_name]" ,
148
+ Short : "create a new environment." ,
149
+ Args : cobra .ExactArgs (1 ),
150
+ // Don't unhide this command until we can pass image names instead of image id's.
151
+ Hidden : true ,
152
+ Long : "Create a new environment under the active user." ,
153
+ Example : `# create a new environment using default resource amounts
154
+ coder envs create --image 5f443b16-30652892427b955601330fa5 my-env-name
155
+
156
+ # create a new environment using custom resource amounts
157
+ coder envs create --cpu 4 --disk 100 --memory 8 --image 5f443b16-30652892427b955601330fa5 my-env-name` ,
158
+ RunE : func (cmd * cobra.Command , args []string ) error {
159
+ if img == "" {
160
+ return xerrors .New ("image id unset" )
161
+ }
162
+ // ExactArgs(1) ensures our name value can't panic on an out of bounds.
163
+ createReq := & coder.CreateEnvironmentRequest {
164
+ Name : args [0 ],
165
+ ImageID : img ,
166
+ ImageTag : tag ,
167
+ }
168
+ // We're explicitly ignoring errors for these because all of these flags
169
+ // have a non-zero-value default value set already.
170
+ createReq .CPUCores , _ = cmd .Flags ().GetFloat32 ("cpu" )
171
+ createReq .MemoryGB , _ = cmd .Flags ().GetFloat32 ("memory" )
172
+ createReq .DiskGB , _ = cmd .Flags ().GetInt ("disk" )
173
+ createReq .GPUs , _ = cmd .Flags ().GetInt ("gpus" )
174
+
175
+ client , err := newClient ()
176
+ if err != nil {
177
+ return err
178
+ }
179
+
180
+ env , err := client .CreateEnvironment (cmd .Context (), org , * createReq )
181
+ if err != nil {
182
+ return xerrors .Errorf ("create environment: %w" , err )
183
+ }
184
+
185
+ clog .LogSuccess (
186
+ "creating environment..." ,
187
+ clog .BlankLine ,
188
+ clog .Tip (`run "coder envs watch-build %q" to trail the build logs` , args [0 ]),
189
+ )
190
+
191
+ if follow {
192
+ if err := trailBuildLogs (cmd .Context (), client , env .ID ); err != nil {
193
+ return err
194
+ }
195
+ }
196
+ return nil
197
+ },
198
+ }
199
+ cmd .Flags ().StringVarP (& org , "org" , "o" , defaultOrg , "ID of the organization the environment should be created under." )
200
+ cmd .Flags ().StringVarP (& tag , "tag" , "t" , defaultImgTag , "tag of the image the environment will be based off of." )
201
+ cmd .Flags ().Float32P ("cpu" , "c" , defaultCPUCores , "number of cpu cores the environment should be provisioned with." )
202
+ cmd .Flags ().Float32P ("memory" , "m" , defaultMemGB , "GB of RAM an environment should be provisioned with." )
203
+ cmd .Flags ().IntP ("disk" , "d" , defaultDiskGB , "GB of disk storage an environment should be provisioned with." )
204
+ cmd .Flags ().IntP ("gpus" , "g" , defaultGPUs , "number GPUs an environment should be provisioned with." )
205
+ cmd .Flags ().StringVarP (& img , "image" , "i" , "" , "ID of the image to base the environment off of." )
206
+ cmd .Flags ().BoolVar (& follow , "follow" , false , "follow buildlog after initiating rebuild" )
207
+ cmd .MarkFlagRequired ("image" )
208
+ return cmd
209
+ }
0 commit comments