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

Commitffc797b

Browse files
committed
Add coder envs rm command for removing environments
1 parentd32d196 commitffc797b

File tree

3 files changed

+106
-11
lines changed

3 files changed

+106
-11
lines changed

‎docs/coder_envs.md

Lines changed: 1 addition & 0 deletions
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

Lines changed: 26 additions & 0 deletions
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

Lines changed: 79 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
funcenvsCommand()*cobra.Command {
27-
varoutputFmtstring
2828
varuserstring
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+
returncmd
46+
}
47+
48+
funclsEnvsCommand(user*string)*cobra.Command {
49+
varoutputFmtstring
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
iferr!=nil {
4358
returnerr
4459
}
45-
envs,err:=getEnvs(cmd.Context(),client,user)
60+
envs,err:=getEnvs(cmd.Context(),client,*user)
4661
iferr!=nil {
4762
returnerr
4863
}
@@ -70,17 +85,13 @@ func envsCommand() *cobra.Command {
7085
returnnil
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
returncmd
8192
}
8293

83-
funcstopEnvCommand(user*string)*cobra.Command {
94+
funcstopEnvsCommand(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
returncmd
320331
}
332+
333+
funcrmEnvsCommand(user*string)*cobra.Command {
334+
varforcebool
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+
iferr!=nil {
343+
returnerr
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+
returnerr
352+
}
353+
}
354+
355+
varegroup errgroup.Group
356+
varfailuresint32
357+
for_,envName:=rangeargs {
358+
envName:=envName
359+
egroup.Go(func()error {
360+
env,err:=findEnv(ctx,client,envName,*user)
361+
iferr!=nil {
362+
atomic.AddInt32(&failures,1)
363+
clog.Log(err)
364+
returnerr
365+
}
366+
iferr=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+
returnerr
374+
}
375+
clog.LogSuccess(fmt.Sprintf("deleted environment %q",env.Name))
376+
returnnil
377+
})
378+
}
379+
380+
iferr=egroup.Wait();err!=nil {
381+
returnxerrors.Errorf("%d failure(s) emitted",failures)
382+
}
383+
returnnil
384+
},
385+
}
386+
cmd.Flags().BoolVarP(&force,"force","f",false,"force remove the specified environments without prompting first")
387+
returncmd
388+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp