This repository was archived by the owner on Aug 30, 2024. It is now read-only.
- Notifications
You must be signed in to change notification settings - Fork18
Add coder envs rm command for removing environments#145
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
91 changes: 80 additions & 11 deletionsinternal/cmd/envs.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -9,6 +9,7 @@ import ( | ||
"cdr.dev/coder-cli/coder-sdk" | ||
"cdr.dev/coder-cli/internal/clog" | ||
"cdr.dev/coder-cli/internal/x/xtabwriter" | ||
"github.com/manifoldco/promptui" | ||
"github.com/spf13/cobra" | ||
"golang.org/x/sync/errgroup" | ||
"golang.org/x/xerrors" | ||
@@ -24,7 +25,6 @@ const ( | ||
) | ||
func envsCommand() *cobra.Command { | ||
var user string | ||
cmd := &cobra.Command{ | ||
Use: "envs", | ||
@@ -33,7 +33,22 @@ func envsCommand() *cobra.Command { | ||
} | ||
cmd.PersistentFlags().StringVar(&user, "user", coder.Me, "Specify the user whose resources to target") | ||
cmd.AddCommand( | ||
lsEnvsCommand(&user), | ||
stopEnvsCommand(&user), | ||
rmEnvsCommand(&user), | ||
watchBuildLogCommand(), | ||
rebuildEnvCommand(), | ||
createEnvCommand(), | ||
editEnvCommand(&user), | ||
) | ||
return cmd | ||
} | ||
func lsEnvsCommand(user *string) *cobra.Command { | ||
var outputFmt string | ||
cmd := &cobra.Command{ | ||
Use: "ls", | ||
Short: "list all environments owned by the active user", | ||
Long: "List all Coder environments owned by the active user.", | ||
@@ -42,7 +57,7 @@ func envsCommand() *cobra.Command { | ||
if err != nil { | ||
return err | ||
} | ||
envs, err := getEnvs(cmd.Context(), client,*user) | ||
if err != nil { | ||
return err | ||
} | ||
@@ -70,17 +85,13 @@ func envsCommand() *cobra.Command { | ||
return nil | ||
}, | ||
} | ||
cmd.Flags().StringVarP(&outputFmt, "output", "o", "human", "human | json") | ||
return cmd | ||
} | ||
funcstopEnvsCommand(user *string) *cobra.Command { | ||
return &cobra.Command{ | ||
Use: "stop [...environment_names]", | ||
Short: "stop Coder environments by name", | ||
@@ -318,3 +329,61 @@ coder envs edit back-end-env --disk 20`, | ||
cmd.Flags().BoolVar(&follow, "follow", false, "follow buildlog after initiating rebuild") | ||
return cmd | ||
} | ||
func rmEnvsCommand(user *string) *cobra.Command { | ||
var force bool | ||
cmd := &cobra.Command{ | ||
Use: "rm [...environment_names]", | ||
Short: "remove Coder environments by name", | ||
Hidden: true, | ||
Args: cobra.MinimumNArgs(1), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
ctx := cmd.Context() | ||
client, err := newClient() | ||
if err != nil { | ||
return err | ||
} | ||
if !force { | ||
confirm := promptui.Prompt{ | ||
Label: fmt.Sprintf("Delete environments %q? (all data will be lost)", args), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. I like how we're using the | ||
IsConfirm: true, | ||
} | ||
if _, err := confirm.Run(); err != nil { | ||
return err | ||
} | ||
} | ||
var egroup errgroup.Group | ||
var failures int32 | ||
for _, envName := range args { | ||
envName := envName | ||
egroup.Go(func() error { | ||
env, err := findEnv(ctx, client, envName, *user) | ||
if err != nil { | ||
atomic.AddInt32(&failures, 1) | ||
clog.Log(err) | ||
return err | ||
} | ||
if err = client.DeleteEnvironment(cmd.Context(), env.ID); err != nil { | ||
atomic.AddInt32(&failures, 1) | ||
err = clog.Error( | ||
fmt.Sprintf(`failed to delete environment "%s"`, env.Name), | ||
clog.Causef(err.Error()), | ||
) | ||
clog.Log(err) | ||
return err | ||
} | ||
clog.LogSuccess(fmt.Sprintf("deleted environment %q", env.Name)) | ||
return nil | ||
}) | ||
} | ||
if err = egroup.Wait(); err != nil { | ||
return xerrors.Errorf("%d failure(s) emitted", failures) | ||
} | ||
return nil | ||
}, | ||
} | ||
cmd.Flags().BoolVarP(&force, "force", "f", false, "force remove the specified environments without prompting first") | ||
return cmd | ||
} |
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.