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

Commit6f8b9b8

Browse files
authored
Add coder envs rm command for removing environments (#145)
1 parentd32d196 commit6f8b9b8

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

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp