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

Commit 67e7278

Browse files
committed
Add "coder envs stop [environment_name]" command
1 parent b7cd44f commit 67e7278

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

coder-sdk/env.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,11 @@ func (c Client) DeleteEnvironment(ctx context.Context, envID string) error {
107107
return c.requestBody(ctx, http.MethodDelete, "/api/environments/"+envID, nil, nil)
108108
}
109109

110+
// StopEnvironment stops the stops.
111+
func (c Client) StopEnvironment(ctx context.Context, envID string) error {
112+
return c.requestBody(ctx, http.MethodPut, "/api/environments/"+envID+"/stop", nil, nil)
113+
}
114+
110115
// DialWsep dials an environments command execution interface
111116
// See https://github.com/cdr/wsep for details.
112117
func (c Client) DialWsep(ctx context.Context, env *Environment) (*websocket.Conn, error) {

internal/cmd/envs.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,34 @@ func makeEnvsCommand() *cobra.Command {
6161
}
6262
lsCmd.Flags().StringVarP(&outputFmt, "output", "o", "human", "human | json")
6363
cmd.AddCommand(lsCmd)
64+
cmd.AddCommand(stopEnvCommand(&user))
6465

6566
return cmd
6667
}
68+
69+
func stopEnvCommand(user *string) *cobra.Command {
70+
return &cobra.Command{
71+
Use: "stop [environment_name]",
72+
Short: "stop Coder environments by name",
73+
Long: "Stop a Coder environment by name.",
74+
Args: cobra.ExactArgs(1),
75+
RunE: func(cmd *cobra.Command, args []string) error {
76+
client, err := newClient()
77+
if err != nil {
78+
return xerrors.Errorf("new client: %w", err)
79+
}
80+
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)
85+
}
86+
87+
if err = client.StopEnvironment(cmd.Context(), env.ID); err != nil {
88+
return xerrors.Errorf("stop environment: %w", err)
89+
}
90+
flog.Success("Successfully stopped environment %s", envName)
91+
return nil
92+
},
93+
}
94+
}

0 commit comments

Comments
 (0)