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" ,
@@ -318,3 +329,61 @@ coder envs edit back-end-env --disk 20`,
318
329
cmd .Flags ().BoolVar (& follow ,"follow" ,false ,"follow buildlog after initiating rebuild" )
319
330
return cmd
320
331
}
332
+
333
+ func rmEnvsCommand (user * string )* cobra.Command {
334
+ var force bool
335
+ cmd := & cobra.Command {
336
+ Use :"rm [...environment_names]" ,
337
+ Short :"remove Coder environments by name" ,
338
+ Hidden :true ,
339
+ Args :cobra .MinimumNArgs (1 ),
340
+ RunE :func (cmd * cobra.Command ,args []string )error {
341
+ ctx := cmd .Context ()
342
+ client ,err := newClient ()
343
+ if err != nil {
344
+ return err
345
+ }
346
+ if ! force {
347
+ confirm := promptui.Prompt {
348
+ Label :fmt .Sprintf ("Delete environments %q? (all data will be lost)" ,args ),
349
+ IsConfirm :true ,
350
+ }
351
+ if _ ,err := confirm .Run ();err != nil {
352
+ return err
353
+ }
354
+ }
355
+
356
+ var egroup errgroup.Group
357
+ var failures int32
358
+ for _ ,envName := range args {
359
+ envName := envName
360
+ egroup .Go (func ()error {
361
+ env ,err := findEnv (ctx ,client ,envName ,* user )
362
+ if err != nil {
363
+ atomic .AddInt32 (& failures ,1 )
364
+ clog .Log (err )
365
+ return err
366
+ }
367
+ if err = client .DeleteEnvironment (cmd .Context (),env .ID );err != nil {
368
+ atomic .AddInt32 (& failures ,1 )
369
+ err = clog .Error (
370
+ fmt .Sprintf (`failed to delete environment "%s"` ,env .Name ),
371
+ clog .Causef (err .Error ()),
372
+ )
373
+ clog .Log (err )
374
+ return err
375
+ }
376
+ clog .LogSuccess (fmt .Sprintf ("deleted environment %q" ,env .Name ))
377
+ return nil
378
+ })
379
+ }
380
+
381
+ if err = egroup .Wait ();err != nil {
382
+ return xerrors .Errorf ("%d failure(s) emitted" ,failures )
383
+ }
384
+ return nil
385
+ },
386
+ }
387
+ cmd .Flags ().BoolVarP (& force ,"force" ,"f" ,false ,"force remove the specified environments without prompting first" )
388
+ return cmd
389
+ }