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

Commit0d0c495

Browse files
committed
Add coder envs rm command for removing environments
1 parent9392b25 commit0d0c495

File tree

3 files changed

+112
-11
lines changed

3 files changed

+112
-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: 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/envs.go

Lines changed: 82 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ 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"
15+
"golang.org/x/sync/semaphore"
1416
"golang.org/x/xerrors"
1517
)
1618

@@ -24,7 +26,6 @@ const (
2426
)
2527

2628
funcenvsCommand()*cobra.Command {
27-
varoutputFmtstring
2829
varuserstring
2930
cmd:=&cobra.Command{
3031
Use:"envs",
@@ -33,7 +34,22 @@ func envsCommand() *cobra.Command {
3334
}
3435
cmd.PersistentFlags().StringVar(&user,"user",coder.Me,"Specify the user whose resources to target")
3536

36-
lsCmd:=&cobra.Command{
37+
cmd.AddCommand(
38+
lsEnvsCommand(&user),
39+
stopEnvsCommand(&user),
40+
rmEnvsCommand(&user),
41+
watchBuildLogCommand(),
42+
rebuildEnvCommand(),
43+
createEnvCommand(),
44+
editEnvCommand(&user),
45+
)
46+
returncmd
47+
}
48+
49+
funclsEnvsCommand(user*string)*cobra.Command {
50+
varoutputFmtstring
51+
52+
cmd:=&cobra.Command{
3753
Use:"ls",
3854
Short:"list all environments owned by the active user",
3955
Long:"List all Coder environments owned by the active user.",
@@ -42,7 +58,7 @@ func envsCommand() *cobra.Command {
4258
iferr!=nil {
4359
returnerr
4460
}
45-
envs,err:=getEnvs(cmd.Context(),client,user)
61+
envs,err:=getEnvs(cmd.Context(),client,*user)
4662
iferr!=nil {
4763
returnerr
4864
}
@@ -70,17 +86,13 @@ func envsCommand() *cobra.Command {
7086
returnnil
7187
},
7288
}
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())
89+
90+
cmd.Flags().StringVarP(&outputFmt,"output","o","human","human | json")
91+
8092
returncmd
8193
}
8294

83-
funcstopEnvCommand(user*string)*cobra.Command {
95+
funcstopEnvsCommand(user*string)*cobra.Command {
8496
return&cobra.Command{
8597
Use:"stop [...environment_names]",
8698
Short:"stop Coder environments by name",
@@ -310,3 +322,62 @@ coder envs edit back-end-env --disk 20`,
310322
cmd.Flags().BoolVar(&follow,"follow",false,"follow buildlog after initiating rebuild")
311323
returncmd
312324
}
325+
326+
funcrmEnvsCommand(user*string)*cobra.Command {
327+
varforcebool
328+
cmd:=&cobra.Command{
329+
Use:"rm [...environment_names]",
330+
Short:"remove Coder environments by name",
331+
Args:cobra.MinimumNArgs(1),
332+
RunE:func(cmd*cobra.Command,args []string)error {
333+
ctx:=cmd.Context()
334+
client,err:=newClient()
335+
iferr!=nil {
336+
returnerr
337+
}
338+
339+
// only show one confirmation dialogue at a time
340+
confirmLimiter:=semaphore.NewWeighted(1)
341+
342+
varegroup errgroup.Group
343+
for_,envName:=rangeargs {
344+
envName:=envName
345+
egroup.Go(func()error {
346+
env,err:=findEnv(ctx,client,envName,*user)
347+
iferr!=nil {
348+
clog.Log(err)
349+
returnerr
350+
}
351+
if!force {
352+
confirm:= promptui.Prompt{
353+
Label:fmt.Sprintf("Delete environment\"%s\"? (all data will be lost)",env.Name),
354+
IsConfirm:true,
355+
}
356+
iferr:=confirmLimiter.Acquire(ctx,1);err!=nil {
357+
returnerr
358+
}
359+
360+
if_,err=confirm.Run();err!=nil {
361+
confirmLimiter.Release(1)
362+
returnxerrors.Errorf("confirm deletion of environment\"%s\"",env.Name)
363+
}
364+
confirmLimiter.Release(1)
365+
}
366+
iferr=client.DeleteEnvironment(cmd.Context(),env.ID);err!=nil {
367+
err=clog.Error(fmt.Sprintf(`failed to delete environment "%s"`,env.Name),clog.Cause(err.Error()))
368+
clog.Log(err)
369+
returnerr
370+
}
371+
returnnil
372+
})
373+
}
374+
375+
iferr=egroup.Wait();err!=nil {
376+
returnxerrors.Errorf("some environment remove operations failed")
377+
}
378+
returnnil
379+
},
380+
}
381+
cmd.Flags().BoolVarP(&force,"force","f",false,"force remove the specified environments without prompting first")
382+
returncmd
383+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp