-
Notifications
You must be signed in to change notification settings - Fork 4
/
delete_environment.go
61 lines (52 loc) · 1.45 KB
/
delete_environment.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package environment
import (
"context"
"fmt"
"os"
"github.com/spf13/cobra"
"golang.org/x/term"
"github.com/calyptia/api/types"
cfg "github.com/calyptia/cli/config"
"github.com/calyptia/cli/confirm"
)
func NewCmdDeleteEnvironment(c *cfg.Config) *cobra.Command {
var confirmDelete bool
cmd := &cobra.Command{
Use: "environment ENVIRONMENT_NAME",
Args: cobra.ExactArgs(1),
Short: "Delete an environment",
RunE: func(cmd *cobra.Command, args []string) error {
name := args[0]
ctx := context.Background()
environments, err := c.Cloud.Environments(ctx, c.ProjectID, types.EnvironmentsParams{Name: &name})
if err != nil {
return err
}
if len(environments.Items) == 0 {
return fmt.Errorf("environment not found")
}
environment := environments.Items[0]
if !confirmDelete {
cmd.Print("This will remove ALL your agents, core_instances. Do you confirm? [y/N] ")
confirmDelete, err = confirm.Read(cmd.InOrStdin())
if err != nil {
return err
}
if !confirmDelete {
cmd.Println("Aborted")
return nil
}
}
err = c.Cloud.DeleteEnvironment(ctx, environment.ID)
if err != nil {
return err
}
cmd.Printf("Deleted environment ID: %s Name: %s\n", environment.ID, environment.Name)
return nil
},
}
isNonInteractive := os.Stdin == nil || !term.IsTerminal(int(os.Stdin.Fd()))
fs := cmd.Flags()
fs.BoolVar(&confirmDelete, "yes", isNonInteractive, "Confirm deletion")
return cmd
}