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

Commit 22ae5cb

Browse files
authored
Allow multiple env args to env stop command (#144)
* Allow multiple env args to env stop command * Add doc examples for coder env stop command with coder envs ls
1 parent e5d55d0 commit 22ae5cb

File tree

4 files changed

+55
-17
lines changed

4 files changed

+55
-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

+35-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,48 @@ 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+
flog.Error("failed to find environment by name \"%s\": %v", env.Name, err)
99+
return xerrors.Errorf("find environment by name: %w", err)
100+
}
101+
102+
if err = client.StopEnvironment(cmd.Context(), env.ID); err != nil {
103+
flog.Error("failed to stop environment \"%s\": %v", env.Name, err)
104+
return xerrors.Errorf("stop environment: %w", err)
105+
}
106+
flog.Success("Successfully stopped environment %q", envName)
107+
return nil
108+
})
85109
}
86110

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

0 commit comments

Comments
 (0)