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

Allow multiple env args to env stop command #144

Merged
merged 2 commits into from
Oct 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/coder_envs.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ Perform operations on the Coder environments owned by the active user.

* [coder](coder.md) - coder provides a CLI for working with an existing Coder Enterprise installation
* [coder envs ls](coder_envs_ls.md) - list all environments owned by the active user
* [coder envs stop](coder_envs_stop.md) - stop a Coder environment by name
* [coder envs stop](coder_envs_stop.md) - stop Coder environments by name
21 changes: 18 additions & 3 deletions docs/coder_envs_stop.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,28 @@
## coder envs stop

stop a Coder environment by name
stop Coder environments by name

### Synopsis

Stop a Coder environment by name
Stop Coder environments by name

```
coder envs stop [environment_name] [flags]
coder envs stop [...environment_names] [flags]
```

### Examples

```
coder envs stop front-end-env
coder envs stop front-end-env backend-env
# stop all of your environments
coder envs ls -o json | jq -c '.[].name' | xargs coder envs stop
# stop all environments for a given user
coder envs --user charlie@coder.com ls -o json \
| jq -c '.[].name' \
| xargs coder envs --user charlie@coder.com stop
```

### Options
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func Make() *cobra.Command {
makeUsersCmd(),
makeConfigSSHCmd(),
makeSecretsCmd(),
makeEnvsCommand(),
envsCommand(),
makeSyncCmd(),
makeURLCmd(),
makeResourceCmd(),
Expand Down
47 changes: 35 additions & 12 deletions internal/cmd/envs.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ import (
"cdr.dev/coder-cli/coder-sdk"
"cdr.dev/coder-cli/internal/x/xtabwriter"
"github.com/spf13/cobra"
"golang.org/x/sync/errgroup"
"golang.org/x/xerrors"

"go.coder.com/flog"
)

func makeEnvsCommand() *cobra.Command {
func envsCommand() *cobra.Command {
var outputFmt string
var user string
cmd := &cobra.Command{
Expand Down Expand Up @@ -68,26 +69,48 @@ func makeEnvsCommand() *cobra.Command {

func stopEnvCommand(user *string) *cobra.Command {
return &cobra.Command{
Use: "stop [environment_name]",
Short: "stop a Coder environment by name",
Long: "Stop a Coder environment by name",
Args: cobra.ExactArgs(1),
Use: "stop [...environment_names]",
Short: "stop Coder environments by name",
Long: "Stop Coder environments by name",
Example: `coder envs stop front-end-env
coder envs stop front-end-env backend-env

# stop all of your environments
coder envs ls -o json | jq -c '.[].name' | xargs coder envs stop

# stop all environments for a given user
coder envs --user charlie@coder.com ls -o json \
| jq -c '.[].name' \
| xargs coder envs --user charlie@coder.com stop`,
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
client, err := newClient()
if err != nil {
return xerrors.Errorf("new client: %w", err)
}

envName := args[0]
env, err := findEnv(cmd.Context(), client, envName, *user)
if err != nil {
return xerrors.Errorf("find environment by name: %w", err)
var egroup errgroup.Group
for _, envName := range args {
envName := envName
egroup.Go(func() error {
env, err := findEnv(cmd.Context(), client, envName, *user)
if err != nil {
flog.Error("failed to find environment by name \"%s\": %v", env.Name, err)
return xerrors.Errorf("find environment by name: %w", err)
}

if err = client.StopEnvironment(cmd.Context(), env.ID); err != nil {
flog.Error("failed to stop environment \"%s\": %v", env.Name, err)
return xerrors.Errorf("stop environment: %w", err)
}
flog.Success("Successfully stopped environment %q", envName)
return nil
})
}

if err = client.StopEnvironment(cmd.Context(), env.ID); err != nil {
return xerrors.Errorf("stop environment: %w", err)
if err = egroup.Wait(); err != nil {
return xerrors.Errorf("some stop operations failed: %w", err)
}
flog.Success("Successfully stopped environment %q", envName)
return nil
},
}
Expand Down