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

Commit ed80598

Browse files
committed
Allow multiple env args to env stop command
1 parent e5d55d0 commit ed80598

File tree

4 files changed

+54
-17
lines changed

4 files changed

+54
-17
lines changed

docs/coder_envs.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,4 @@ 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 stop](coder_envs_stop.md) - stop a Coder environment by name
26+
* [coder envs stop](coder_envs_stop.md) - stop Coder environments by name

docs/coder_envs_stop.md

+18-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,28 @@
11
## coder envs stop
22

3-
stop a Coder environment by name
3+
stop Coder environments by name
44

55
### Synopsis
66

7-
Stop a Coder environment by name
7+
Stop Coder environments by name
88

99
```
10-
coder envs stop [environment_name] [flags]
10+
coder envs stop [...environment_names] [flags]
11+
```
12+
13+
### Examples
14+
15+
```
16+
coder envs stop front-end-env
17+
coder envs stop front-end-env backend-env
18+
19+
# stop all of your environments
20+
coder envs ls -o json | jq -c '.[].name' | xargs coder envs stop
21+
22+
# stop all environments for a given user
23+
coder envs --user charlie@coder.com ls -o json \
24+
| jq -c '.[].name' \
25+
| xargs coder envs --user charlie@coder.com stop
1126
```
1227

1328
### Options

internal/cmd/cmd.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func Make() *cobra.Command {
2424
makeUsersCmd(),
2525
makeConfigSSHCmd(),
2626
makeSecretsCmd(),
27-
makeEnvsCommand(),
27+
envsCommand(),
2828
makeSyncCmd(),
2929
makeURLCmd(),
3030
makeResourceCmd(),

internal/cmd/envs.go

+34-12
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@ import (
77
"cdr.dev/coder-cli/coder-sdk"
88
"cdr.dev/coder-cli/internal/x/xtabwriter"
99
"github.com/spf13/cobra"
10+
"golang.org/x/sync/errgroup"
1011
"golang.org/x/xerrors"
1112

1213
"go.coder.com/flog"
1314
)
1415

15-
func makeEnvsCommand() *cobra.Command {
16+
func envsCommand() *cobra.Command {
1617
var outputFmt string
1718
var user string
1819
cmd := &cobra.Command{
@@ -68,26 +69,47 @@ func makeEnvsCommand() *cobra.Command {
6869

6970
func stopEnvCommand(user *string) *cobra.Command {
7071
return &cobra.Command{
71-
Use: "stop [environment_name]",
72-
Short: "stop a Coder environment by name",
73-
Long: "Stop a Coder environment by name",
74-
Args: cobra.ExactArgs(1),
72+
Use: "stop [...environment_names]",
73+
Short: "stop Coder environments by name",
74+
Long: "Stop Coder environments by name",
75+
Example: `coder envs stop front-end-env
76+
coder envs stop front-end-env backend-env
77+
78+
# stop all of your environments
79+
coder envs ls -o json | jq -c '.[].name' | xargs coder envs stop
80+
81+
# stop all environments for a given user
82+
coder envs --user charlie@coder.com ls -o json \
83+
| jq -c '.[].name' \
84+
| xargs coder envs --user charlie@coder.com stop`,
85+
Args: cobra.MinimumNArgs(1),
7586
RunE: func(cmd *cobra.Command, args []string) error {
7687
client, err := newClient()
7788
if err != nil {
7889
return xerrors.Errorf("new client: %w", err)
7990
}
8091

81-
envName := args[0]
82-
env, err := findEnv(cmd.Context(), client, envName, *user)
83-
if err != nil {
84-
return xerrors.Errorf("find environment by name: %w", err)
92+
var egroup errgroup.Group
93+
for _, envName := range args {
94+
envName := envName
95+
egroup.Go(func() error {
96+
env, err := findEnv(cmd.Context(), client, envName, *user)
97+
if err != nil {
98+
return xerrors.Errorf("find environment by name: %w", err)
99+
}
100+
101+
if err = client.StopEnvironment(cmd.Context(), env.ID); err != nil {
102+
flog.Error("failed to stop environment \"%s\": %v", env.Name, err)
103+
return xerrors.Errorf("stop environment: %w", err)
104+
}
105+
flog.Success("Successfully stopped environment %q", envName)
106+
return nil
107+
})
85108
}
86109

87-
if err = client.StopEnvironment(cmd.Context(), env.ID); err != nil {
88-
return xerrors.Errorf("stop environment: %w", err)
110+
if err = egroup.Wait(); err != nil {
111+
return xerrors.Errorf("some stop operations failed: %w", err)
89112
}
90-
flog.Success("Successfully stopped environment %q", envName)
91113
return nil
92114
},
93115
}

0 commit comments

Comments
 (0)