Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Aug 30, 2024. It is now read-only.
/coder-v1-cliPublic archive

Add coder envs rm command for removing environments#145

Merged
cmoog merged 1 commit intomasterfromenvs-rm
Oct 26, 2020
Merged
Changes fromall 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
91 changes: 80 additions & 11 deletionsinternal/cmd/envs.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -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"
Expand All@@ -24,7 +25,6 @@ const (
)

func envsCommand() *cobra.Command {
var outputFmt string
var user string
cmd := &cobra.Command{
Use: "envs",
Expand All@@ -33,7 +33,22 @@ func envsCommand() *cobra.Command {
}
cmd.PersistentFlags().StringVar(&user, "user", coder.Me, "Specify the user whose resources to target")

lsCmd := &cobra.Command{
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.",
Expand All@@ -42,7 +57,7 @@ func envsCommand() *cobra.Command {
if err != nil {
return err
}
envs, err := getEnvs(cmd.Context(), client, user)
envs, err := getEnvs(cmd.Context(), client,*user)
if err != nil {
return err
}
Expand DownExpand Up@@ -70,17 +85,13 @@ func envsCommand() *cobra.Command {
return nil
},
}
lsCmd.Flags().StringVarP(&outputFmt, "output", "o", "human", "human | json")
cmd.AddCommand(lsCmd)
cmd.AddCommand(editEnvCommand(&user))
cmd.AddCommand(stopEnvCommand(&user))
cmd.AddCommand(watchBuildLogCommand())
cmd.AddCommand(rebuildEnvCommand())
cmd.AddCommand(createEnvCommand())

cmd.Flags().StringVarP(&outputFmt, "output", "o", "human", "human | json")

return cmd
}

funcstopEnvCommand(user *string) *cobra.Command {
funcstopEnvsCommand(user *string) *cobra.Command {
return &cobra.Command{
Use: "stop [...environment_names]",
Short: "stop Coder environments by name",
Expand DownExpand Up@@ -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),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I like how we're using theforce flag with this confirmation and all.

cmoog reacted with thumbs up emoji
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
}

[8]ページ先頭

©2009-2025 Movatter.jp