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

Commit 9b05be4

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

File tree

3 files changed

+111
-11
lines changed

3 files changed

+111
-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

+84-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",
@@ -183,6 +194,11 @@ coder envs create --cpu 4 --disk 100 --memory 8 --image 5f443b16-30652892427b955
183194
return xerrors.Errorf("create environment: %w", err)
184195
}
185196

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+
)
186202
if follow {
187203
clog.LogSuccess("creating environment...")
188204
if err := trailBuildLogs(cmd.Context(), client, env.ID); err != nil {
@@ -318,3 +334,60 @@ coder envs edit back-end-env --disk 20`,
318334
cmd.Flags().BoolVar(&follow, "follow", false, "follow buildlog after initiating rebuild")
319335
return cmd
320336
}
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

Comments
 (0)