Skip to content
This repository was archived by the owner on Aug 30, 2024. It is now read-only.

Commit ffc797b

Browse files
committed
Add coder envs rm command for removing environments
1 parent d32d196 commit ffc797b

File tree

3 files changed

+106
-11
lines changed

3 files changed

+106
-11
lines changed

docs/coder_envs.md

+1
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,6 @@ Perform operations on the Coder environments owned by the active user.
2323

2424
* [coder](coder.md) - coder provides a CLI for working with an existing Coder Enterprise installation
2525
* [coder envs ls](coder_envs_ls.md) - list all environments owned by the active user
26+
* [coder envs rm](coder_envs_rm.md) - remove Coder environments by name
2627
* [coder envs stop](coder_envs_stop.md) - stop Coder environments by name
2728

docs/coder_envs_rm.md

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
## coder envs rm
2+
3+
remove Coder environments by name
4+
5+
```
6+
coder envs rm [...environment_names] [flags]
7+
```
8+
9+
### Options
10+
11+
```
12+
-f, --force force remove the specified environments without prompting first
13+
-h, --help help for rm
14+
```
15+
16+
### Options inherited from parent commands
17+
18+
```
19+
--user string Specify the user whose resources to target (default "me")
20+
-v, --verbose show verbose output
21+
```
22+
23+
### SEE ALSO
24+
25+
* [coder envs](coder_envs.md) - Interact with Coder environments
26+

internal/cmd/envs.go

+79-11
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"cdr.dev/coder-cli/coder-sdk"
1010
"cdr.dev/coder-cli/internal/clog"
1111
"cdr.dev/coder-cli/internal/x/xtabwriter"
12+
"github.com/manifoldco/promptui"
1213
"github.com/spf13/cobra"
1314
"golang.org/x/sync/errgroup"
1415
"golang.org/x/xerrors"
@@ -24,7 +25,6 @@ const (
2425
)
2526

2627
func envsCommand() *cobra.Command {
27-
var outputFmt string
2828
var user string
2929
cmd := &cobra.Command{
3030
Use: "envs",
@@ -33,7 +33,22 @@ func envsCommand() *cobra.Command {
3333
}
3434
cmd.PersistentFlags().StringVar(&user, "user", coder.Me, "Specify the user whose resources to target")
3535

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{
3752
Use: "ls",
3853
Short: "list all environments owned by the active user",
3954
Long: "List all Coder environments owned by the active user.",
@@ -42,7 +57,7 @@ func envsCommand() *cobra.Command {
4257
if err != nil {
4358
return err
4459
}
45-
envs, err := getEnvs(cmd.Context(), client, user)
60+
envs, err := getEnvs(cmd.Context(), client, *user)
4661
if err != nil {
4762
return err
4863
}
@@ -70,17 +85,13 @@ func envsCommand() *cobra.Command {
7085
return nil
7186
},
7287
}
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+
8091
return cmd
8192
}
8293

83-
func stopEnvCommand(user *string) *cobra.Command {
94+
func stopEnvsCommand(user *string) *cobra.Command {
8495
return &cobra.Command{
8596
Use: "stop [...environment_names]",
8697
Short: "stop Coder environments by name",
@@ -318,3 +329,60 @@ coder envs edit back-end-env --disk 20`,
318329
cmd.Flags().BoolVar(&follow, "follow", false, "follow buildlog after initiating rebuild")
319330
return cmd
320331
}
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

Comments
 (0)