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,60 @@ 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
+ Args : cobra .MinimumNArgs (1 ),
339
+ RunE : func (cmd * cobra.Command , args []string ) error {
340
+ ctx := cmd .Context ()
341
+ client , err := newClient ()
342
+ if err != nil {
343
+ return err
344
+ }
345
+ if ! force {
346
+ confirm := promptui.Prompt {
347
+ Label : fmt .Sprintf ("Delete environments %q? (all data will be lost)" , args ),
348
+ IsConfirm : true ,
349
+ }
350
+ if _ , err := confirm .Run (); err != nil {
351
+ return err
352
+ }
353
+ }
354
+
355
+ var egroup errgroup.Group
356
+ var failures int32
357
+ for _ , envName := range args {
358
+ envName := envName
359
+ egroup .Go (func () error {
360
+ env , err := findEnv (ctx , client , envName , * user )
361
+ if err != nil {
362
+ atomic .AddInt32 (& failures , 1 )
363
+ clog .Log (err )
364
+ return err
365
+ }
366
+ if err = client .DeleteEnvironment (cmd .Context (), env .ID ); err != nil {
367
+ atomic .AddInt32 (& failures , 1 )
368
+ err = clog .Error (
369
+ fmt .Sprintf (`failed to delete environment "%s"` , env .Name ),
370
+ clog .Causef (err .Error ()),
371
+ )
372
+ clog .Log (err )
373
+ return err
374
+ }
375
+ clog .LogSuccess (fmt .Sprintf ("deleted environment %q" , env .Name ))
376
+ return nil
377
+ })
378
+ }
379
+
380
+ if err = egroup .Wait (); err != nil {
381
+ return xerrors .Errorf ("%d failure(s) emitted" , failures )
382
+ }
383
+ return nil
384
+ },
385
+ }
386
+ cmd .Flags ().BoolVarP (& force , "force" , "f" , false , "force remove the specified environments without prompting first" )
387
+ return cmd
388
+ }
0 commit comments