Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Aug 30, 2024. It is now read-only.
/coder-v1-cliPublic archive

Commitf7e76b8

Browse files
committed
Add coder envs rm command for removing environments
1 parent70615c4 commitf7e76b8

File tree

4 files changed

+109
-14
lines changed

4 files changed

+109
-14
lines changed

‎docs/coder_envs.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,5 @@ 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

‎docs/coder_envs_rm.md

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

‎internal/cmd/ceapi.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ import (
55

66
"cdr.dev/coder-cli/coder-sdk"
77
"golang.org/x/xerrors"
8-
9-
"go.coder.com/flog"
108
)
119

1210
// Helpers for working with the Coder Enterprise API.
@@ -73,7 +71,5 @@ func findEnv(ctx context.Context, client *coder.Client, envName, userEmail strin
7371
// Keep track of what we found for the logs.
7472
found=append(found,env.Name)
7573
}
76-
flog.Error("found %q",found)
77-
flog.Error("%q not found",envName)
78-
returnnil,coder.ErrNotFound
74+
returnnil,xerrors.Errorf("found %q,\"%s\" not found: %w",found,envName,coder.ErrNotFound)
7975
}

‎internal/cmd/envs.go

Lines changed: 78 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,21 @@ package cmd
22

33
import (
44
"encoding/json"
5+
"fmt"
56
"os"
67

78
"cdr.dev/coder-cli/coder-sdk"
89
"cdr.dev/coder-cli/internal/x/xtabwriter"
10+
"github.com/manifoldco/promptui"
911
"github.com/spf13/cobra"
1012
"golang.org/x/sync/errgroup"
13+
"golang.org/x/sync/semaphore"
1114
"golang.org/x/xerrors"
1215

1316
"go.coder.com/flog"
1417
)
1518

1619
funcenvsCommand()*cobra.Command {
17-
varoutputFmtstring
1820
varuserstring
1921
cmd:=&cobra.Command{
2022
Use:"envs",
@@ -23,7 +25,20 @@ func envsCommand() *cobra.Command {
2325
}
2426
cmd.PersistentFlags().StringVar(&user,"user",coder.Me,"Specify the user whose resources to target")
2527

26-
lsCmd:=&cobra.Command{
28+
cmd.AddCommand(
29+
lsEnvsCommand(&user),
30+
stopEnvsCommand(&user),
31+
rmEnvsCommand(&user),
32+
watchBuildLogCommand(),
33+
rebuildEnvCommand(),
34+
)
35+
returncmd
36+
}
37+
38+
funclsEnvsCommand(user*string)*cobra.Command {
39+
varoutputFmtstring
40+
41+
cmd:=&cobra.Command{
2742
Use:"ls",
2843
Short:"list all environments owned by the active user",
2944
Long:"List all Coder environments owned by the active user.",
@@ -32,7 +47,7 @@ func envsCommand() *cobra.Command {
3247
iferr!=nil {
3348
returnerr
3449
}
35-
envs,err:=getEnvs(cmd.Context(),client,user)
50+
envs,err:=getEnvs(cmd.Context(),client,*user)
3651
iferr!=nil {
3752
returnerr
3853
}
@@ -60,16 +75,12 @@ func envsCommand() *cobra.Command {
6075
returnnil
6176
},
6277
}
63-
lsCmd.Flags().StringVarP(&outputFmt,"output","o","human","human | json")
64-
cmd.AddCommand(lsCmd)
65-
cmd.AddCommand(stopEnvCommand(&user))
78+
cmd.Flags().StringVarP(&outputFmt,"output","o","human","human | json")
6679

67-
cmd.AddCommand(watchBuildLogCommand())
68-
cmd.AddCommand(rebuildEnvCommand())
6980
returncmd
7081
}
7182

72-
funcstopEnvCommand(user*string)*cobra.Command {
83+
funcstopEnvsCommand(user*string)*cobra.Command {
7384
return&cobra.Command{
7485
Use:"stop [...environment_names]",
7586
Short:"stop Coder environments by name",
@@ -117,3 +128,61 @@ coder envs --user charlie@coder.com ls -o json \
117128
},
118129
}
119130
}
131+
132+
funcrmEnvsCommand(user*string)*cobra.Command {
133+
varforcebool
134+
cmd:=&cobra.Command{
135+
Use:"rm [...environment_names]",
136+
Short:"remove Coder environments by name",
137+
Args:cobra.MinimumNArgs(1),
138+
RunE:func(cmd*cobra.Command,args []string)error {
139+
ctx:=cmd.Context()
140+
client,err:=newClient()
141+
iferr!=nil {
142+
returnerr
143+
}
144+
145+
// only show one confirmation dialogue at a time
146+
confirmLimiter:=semaphore.NewWeighted(1)
147+
148+
varegroup errgroup.Group
149+
for_,envName:=rangeargs {
150+
envName:=envName
151+
egroup.Go(func()error {
152+
env,err:=findEnv(ctx,client,envName,*user)
153+
iferr!=nil {
154+
flog.Error("failed to find environment by name\"%s\": %v",envName,err)
155+
returnerr
156+
}
157+
if!force {
158+
confirm:= promptui.Prompt{
159+
Label:fmt.Sprintf("Delete environment\"%s\"? (all data will be lost)",env.Name),
160+
IsConfirm:true,
161+
}
162+
iferr:=confirmLimiter.Acquire(ctx,1);err!=nil {
163+
returnerr
164+
}
165+
166+
if_,err=confirm.Run();err!=nil {
167+
confirmLimiter.Release(1)
168+
returnxerrors.Errorf("confirm deletion of environment\"%s\"",env.Name)
169+
}
170+
confirmLimiter.Release(1)
171+
}
172+
iferr=client.DeleteEnvironment(cmd.Context(),env.ID);err!=nil {
173+
flog.Error("failed to delete environment\"%s\": %v",env.ID,err)
174+
returnxerrors.Errorf("delete environment: %w",err)
175+
}
176+
returnnil
177+
})
178+
}
179+
180+
iferr=egroup.Wait();err!=nil {
181+
returnxerrors.Errorf("some environment remove operations failed: %w",err)
182+
}
183+
returnnil
184+
},
185+
}
186+
cmd.Flags().BoolVarP(&force,"force","f",false,"force remove the specified environments without prompting first")
187+
returncmd
188+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp