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

Commit b3a3807

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

File tree

2 files changed

+31
-13
lines changed

2 files changed

+31
-13
lines changed

internal/cmd/cmd.go

Lines changed: 1 addition & 1 deletion
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

Lines changed: 30 additions & 12 deletions
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,43 @@ 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+
# to stop all of your environments:
79+
coder envs ls -o json | jq -c '.[].name' | xargs coder envs stop
80+
`,
81+
Args: cobra.MinimumNArgs(1),
7582
RunE: func(cmd *cobra.Command, args []string) error {
7683
client, err := newClient()
7784
if err != nil {
7885
return xerrors.Errorf("new client: %w", err)
7986
}
8087

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)
88+
var egroup errgroup.Group
89+
for _, envName := range args {
90+
envName := envName
91+
egroup.Go(func() error {
92+
env, err := findEnv(cmd.Context(), client, envName, *user)
93+
if err != nil {
94+
return xerrors.Errorf("find environment by name: %w", err)
95+
}
96+
97+
if err = client.StopEnvironment(cmd.Context(), env.ID); err != nil {
98+
flog.Error("failed to stop environment \"%s\": %v", env.Name, err)
99+
return xerrors.Errorf("stop environment: %w", err)
100+
}
101+
flog.Success("Successfully stopped environment %q", envName)
102+
return nil
103+
})
85104
}
86105

87-
if err = client.StopEnvironment(cmd.Context(), env.ID); err != nil {
88-
return xerrors.Errorf("stop environment: %w", err)
106+
if err = egroup.Wait(); err != nil {
107+
return xerrors.Errorf("some stop operations failed: %w", err)
89108
}
90-
flog.Success("Successfully stopped environment %q", envName)
91109
return nil
92110
},
93111
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy