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
+ "github.com/manifoldco/promptui"
12
13
"github.com/spf13/cobra"
13
14
"golang.org/x/sync/errgroup"
14
15
"golang.org/x/xerrors"
@@ -24,7 +25,6 @@ const (
24
25
)
25
26
26
27
func envsCommand () * cobra.Command {
27
- var outputFmt string
28
28
var user string
29
29
cmd := & cobra.Command {
30
30
Use : "envs" ,
@@ -33,7 +33,22 @@ func envsCommand() *cobra.Command {
33
33
}
34
34
cmd .PersistentFlags ().StringVar (& user , "user" , coder .Me , "Specify the user whose resources to target" )
35
35
36
- lsCmd := & cobra.Command {
36
+ cmd .AddCommand (
37
+ lsEnvsCommand (& user ),
38
+ stopEnvsCommand (& user ),
39
+ rmEnvsCommand (& user ),
40
+ watchBuildLogCommand (),
41
+ rebuildEnvCommand (),
42
+ createEnvCommand (),
43
+ editEnvCommand (& user ),
44
+ )
45
+ return cmd
46
+ }
47
+
48
+ func lsEnvsCommand (user * string ) * cobra.Command {
49
+ var outputFmt string
50
+
51
+ cmd := & cobra.Command {
37
52
Use : "ls" ,
38
53
Short : "list all environments owned by the active user" ,
39
54
Long : "List all Coder environments owned by the active user." ,
@@ -42,7 +57,7 @@ func envsCommand() *cobra.Command {
42
57
if err != nil {
43
58
return err
44
59
}
45
- envs , err := getEnvs (cmd .Context (), client , user )
60
+ envs , err := getEnvs (cmd .Context (), client , * user )
46
61
if err != nil {
47
62
return err
48
63
}
@@ -70,17 +85,13 @@ func envsCommand() *cobra.Command {
70
85
return nil
71
86
},
72
87
}
73
- lsCmd .Flags ().StringVarP (& outputFmt , "output" , "o" , "human" , "human | json" )
74
- cmd .AddCommand (lsCmd )
75
- cmd .AddCommand (editEnvCommand (& user ))
76
- cmd .AddCommand (stopEnvCommand (& user ))
77
- cmd .AddCommand (watchBuildLogCommand ())
78
- cmd .AddCommand (rebuildEnvCommand ())
79
- cmd .AddCommand (createEnvCommand ())
88
+
89
+ cmd .Flags ().StringVarP (& outputFmt , "output" , "o" , "human" , "human | json" )
90
+
80
91
return cmd
81
92
}
82
93
83
- func stopEnvCommand (user * string ) * cobra.Command {
94
+ func stopEnvsCommand (user * string ) * cobra.Command {
84
95
return & cobra.Command {
85
96
Use : "stop [...environment_names]" ,
86
97
Short : "stop Coder environments by name" ,
@@ -183,6 +194,11 @@ coder envs create --cpu 4 --disk 100 --memory 8 --image 5f443b16-30652892427b955
183
194
return xerrors .Errorf ("create environment: %w" , err )
184
195
}
185
196
197
+ clog .LogSuccess (
198
+ "creating environment..." ,
199
+ clog .BlankLine ,
200
+ clog .Tipf (`run "coder envs watch-build %q" to trail the build logs` , args [0 ]),
201
+ )
186
202
if follow {
187
203
clog .LogSuccess ("creating environment..." )
188
204
if err := trailBuildLogs (cmd .Context (), client , env .ID ); err != nil {
@@ -318,3 +334,60 @@ coder envs edit back-end-env --disk 20`,
318
334
cmd .Flags ().BoolVar (& follow , "follow" , false , "follow buildlog after initiating rebuild" )
319
335
return cmd
320
336
}
337
+
338
+ func rmEnvsCommand (user * string ) * cobra.Command {
339
+ var force bool
340
+ cmd := & cobra.Command {
341
+ Use : "rm [...environment_names]" ,
342
+ Short : "remove Coder environments by name" ,
343
+ Args : cobra .MinimumNArgs (1 ),
344
+ RunE : func (cmd * cobra.Command , args []string ) error {
345
+ ctx := cmd .Context ()
346
+ client , err := newClient ()
347
+ if err != nil {
348
+ return err
349
+ }
350
+ if ! force {
351
+ confirm := promptui.Prompt {
352
+ Label : fmt .Sprintf ("Delete environments %q? (all data will be lost)" , args ),
353
+ IsConfirm : true ,
354
+ }
355
+ if _ , err := confirm .Run (); err != nil {
356
+ return err
357
+ }
358
+ }
359
+
360
+ var egroup errgroup.Group
361
+ var failures int32
362
+ for _ , envName := range args {
363
+ envName := envName
364
+ egroup .Go (func () error {
365
+ env , err := findEnv (ctx , client , envName , * user )
366
+ if err != nil {
367
+ atomic .AddInt32 (& failures , 1 )
368
+ clog .Log (err )
369
+ return err
370
+ }
371
+ if err = client .DeleteEnvironment (cmd .Context (), env .ID ); err != nil {
372
+ atomic .AddInt32 (& failures , 1 )
373
+ err = clog .Error (
374
+ fmt .Sprintf (`failed to delete environment "%s"` , env .Name ),
375
+ clog .Causef (err .Error ()),
376
+ )
377
+ clog .Log (err )
378
+ return err
379
+ }
380
+ clog .LogSuccess (fmt .Sprintf ("deleted environment %q" , env .Name ))
381
+ return nil
382
+ })
383
+ }
384
+
385
+ if err = egroup .Wait (); err != nil {
386
+ return xerrors .Errorf ("%d failure(s) emitted" , failures )
387
+ }
388
+ return nil
389
+ },
390
+ }
391
+ cmd .Flags ().BoolVarP (& force , "force" , "f" , false , "force remove the specified environments without prompting first" )
392
+ return cmd
393
+ }
0 commit comments