@@ -9,19 +9,16 @@ import (
9
9
"cdr.dev/coder-cli/coder-sdk"
10
10
"cdr.dev/coder-cli/internal/clog"
11
11
"cdr.dev/coder-cli/internal/x/xtabwriter"
12
+
12
13
"github.com/manifoldco/promptui"
13
14
"github.com/spf13/cobra"
14
15
"golang.org/x/sync/errgroup"
15
16
"golang.org/x/xerrors"
16
17
)
17
18
18
19
const (
19
- defaultOrg = "default"
20
- defaultImgTag = "latest"
21
- defaultCPUCores float32 = 1
22
- defaultMemGB float32 = 1
23
- defaultDiskGB = 10
24
- defaultGPUs = 0
20
+ defaultOrg = "default"
21
+ defaultImgTag = "latest"
25
22
)
26
23
27
24
func envsCommand () * cobra.Command {
@@ -39,7 +36,7 @@ func envsCommand() *cobra.Command {
39
36
rmEnvsCommand (& user ),
40
37
watchBuildLogCommand (),
41
38
rebuildEnvCommand (),
42
- createEnvCommand (),
39
+ createEnvCommand (& user ),
43
40
editEnvCommand (& user ),
44
41
)
45
42
return cmd
@@ -147,7 +144,7 @@ coder envs --user charlie@coder.com ls -o json \
147
144
}
148
145
}
149
146
150
- func createEnvCommand () * cobra.Command {
147
+ func createEnvCommand (user * string ) * cobra.Command {
151
148
var (
152
149
org string
153
150
img string
@@ -171,22 +168,40 @@ coder envs create --cpu 4 --disk 100 --memory 8 --image 5f443b16-30652892427b955
171
168
if img == "" {
172
169
return xerrors .New ("image id unset" )
173
170
}
171
+
172
+ client , err := newClient ()
173
+ if err != nil {
174
+ return err
175
+ }
176
+
177
+ importedImg , err := findImg (cmd .Context (), client , * user , img )
178
+ if err != nil {
179
+ return err
180
+ }
181
+
174
182
// ExactArgs(1) ensures our name value can't panic on an out of bounds.
175
183
createReq := & coder.CreateEnvironmentRequest {
176
184
Name : args [0 ],
177
- ImageID : img ,
185
+ ImageID : importedImg . ID ,
178
186
ImageTag : tag ,
179
187
}
180
- // We're explicitly ignoring errors for these because all of these flags
181
- // have a non-zero-value default value set already .
188
+ // We're explicitly ignoring errors for these because all we
189
+ // need to now is if the numeric type is 0 or not .
182
190
createReq .CPUCores , _ = cmd .Flags ().GetFloat32 ("cpu" )
183
191
createReq .MemoryGB , _ = cmd .Flags ().GetFloat32 ("memory" )
184
192
createReq .DiskGB , _ = cmd .Flags ().GetInt ("disk" )
185
193
createReq .GPUs , _ = cmd .Flags ().GetInt ("gpus" )
186
194
187
- client , err := newClient ()
188
- if err != nil {
189
- return err
195
+ // if any of these defaulted to their zero value we provision
196
+ // the create request with the imported image defaults instead.
197
+ if createReq .CPUCores == 0 {
198
+ createReq .CPUCores = importedImg .DefaultCPUCores
199
+ }
200
+ if createReq .MemoryGB == 0 {
201
+ createReq .MemoryGB = importedImg .DefaultMemoryGB
202
+ }
203
+ if createReq .DiskGB == 0 {
204
+ createReq .DiskGB = importedImg .DefaultDiskGB
190
205
}
191
206
192
207
env , err := client .CreateEnvironment (cmd .Context (), org , * createReq )
@@ -211,11 +226,11 @@ coder envs create --cpu 4 --disk 100 --memory 8 --image 5f443b16-30652892427b955
211
226
}
212
227
cmd .Flags ().StringVarP (& org , "org" , "o" , defaultOrg , "ID of the organization the environment should be created under." )
213
228
cmd .Flags ().StringVarP (& tag , "tag" , "t" , defaultImgTag , "tag of the image the environment will be based off of." )
214
- cmd .Flags ().Float32P ("cpu" , "c" , defaultCPUCores , "number of cpu cores the environment should be provisioned with." )
215
- cmd .Flags ().Float32P ("memory" , "m" , defaultMemGB , "GB of RAM an environment should be provisioned with." )
216
- cmd .Flags ().IntP ("disk" , "d" , defaultDiskGB , "GB of disk storage an environment should be provisioned with." )
217
- cmd .Flags ().IntP ("gpus" , "g" , defaultGPUs , "number GPUs an environment should be provisioned with." )
218
- cmd .Flags ().StringVarP (& img , "image" , "i" , "" , "ID of the image to base the environment off of." )
229
+ cmd .Flags ().Float32P ("cpu" , "c" , 0 , "number of cpu cores the environment should be provisioned with." )
230
+ cmd .Flags ().Float32P ("memory" , "m" , 0 , "GB of RAM an environment should be provisioned with." )
231
+ cmd .Flags ().IntP ("disk" , "d" , 0 , "GB of disk storage an environment should be provisioned with." )
232
+ cmd .Flags ().IntP ("gpus" , "g" , 0 , "number GPUs an environment should be provisioned with." )
233
+ cmd .Flags ().StringVarP (& img , "image" , "i" , "" , "name of the image to base the environment off of." )
219
234
cmd .Flags ().BoolVar (& follow , "follow" , false , "follow buildlog after initiating rebuild" )
220
235
_ = cmd .MarkFlagRequired ("image" )
221
236
return cmd
@@ -289,10 +304,14 @@ coder envs edit back-end-env --disk 20`,
289
304
updateReq .GPUs = & gpus
290
305
}
291
306
292
- if img == "" {
293
- updateReq .ImageID = & env .ImageID
307
+ if img != "" {
308
+ importedImg , err := findImg (cmd .Context (), client , * user , img )
309
+ if err != nil {
310
+ return err
311
+ }
312
+ updateReq .ImageID = & importedImg .ID
294
313
} else {
295
- updateReq .ImageID = & img
314
+ updateReq .ImageID = & env . ImageID
296
315
}
297
316
298
317
if tag == "" {
@@ -320,7 +339,7 @@ coder envs edit back-end-env --disk 20`,
320
339
return nil
321
340
},
322
341
}
323
- cmd .Flags ().StringVarP (& img , "image" , "i" , "" , "image ID of the image you wan't the environment to be based off of." )
342
+ cmd .Flags ().StringVarP (& img , "image" , "i" , "" , "name of the image you wan't the environment to be based off of." )
324
343
cmd .Flags ().StringVarP (& tag , "tag" , "t" , "latest" , "image tag of the image you wan't to base the environment off of." )
325
344
cmd .Flags ().Float32P ("cpu" , "c" , cpuCores , "The number of cpu cores the environment should be provisioned with." )
326
345
cmd .Flags ().Float32P ("memory" , "m" , memGB , "The amount of RAM an environment should be provisioned with." )
0 commit comments