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

Commit 6f8b9b8

Browse files
authored
Add coder envs rm command for removing environments (#145)
1 parent d32d196 commit 6f8b9b8

File tree

1 file changed

+80
-11
lines changed

1 file changed

+80
-11
lines changed

internal/cmd/envs.go

Lines changed: 80 additions & 11 deletions
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,61 @@ 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+
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+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy