/
delete.go
87 lines (75 loc) · 2.08 KB
/
delete.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package eks
import (
"fmt"
"io/ioutil"
"os"
"time"
"github.com/aws/aws-k8s-tester/eks"
"github.com/aws/aws-k8s-tester/eksconfig"
"github.com/aws/aws-k8s-tester/pkg/fileutil"
"github.com/manifoldco/promptui"
"github.com/spf13/cobra"
)
func newDelete() *cobra.Command {
ac := &cobra.Command{
Use: "delete <subcommand>",
Short: "Delete commands",
}
ac.AddCommand(newDeleteCluster())
return ac
}
func newDeleteCluster() *cobra.Command {
return &cobra.Command{
Use: "cluster",
Short: "Delete eks cluster",
Run: deleteClusterFunc,
}
}
func deleteClusterFunc(cmd *cobra.Command, args []string) {
if !fileutil.Exist(path) {
fmt.Fprintf(os.Stderr, "cannot find configuration %q\n", path)
os.Exit(1)
}
cfg, err := eksconfig.Load(path)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to load configuration %q (%v)\n", path, err)
os.Exit(1)
}
txt, err := ioutil.ReadFile(path)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to read configuration %q (%v)\n", path, err)
os.Exit(1)
}
fmt.Printf("\n\n%q:\n\n%s\n\n\n", path, string(txt))
if enablePrompt {
prompt := promptui.Select{
Label: "Ready to delete EKS resources, should we continue?",
Items: []string{
"No, cancel it!",
"Yes, let's delete!",
},
}
idx, answer, err := prompt.Run()
if err != nil {
panic(err)
}
if idx != 1 {
fmt.Printf("returning 'delete' [index %d, answer %q]\n", idx, answer)
return
}
}
time.Sleep(5 * time.Second)
tester, err := eks.New(cfg)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to create eks deployer %v\n", err)
os.Exit(1)
}
logWriter := tester.LogWriter()
if err = tester.Down(); err != nil {
fmt.Fprintf(logWriter, cfg.Colorize("\n\n\n[yellow]*********************************\n"))
fmt.Fprintf(logWriter, cfg.Colorize(fmt.Sprintf("[default]aws-k8s-tester eks delete cluster [light_magenta]FAIL [default](%v)\n", err)))
os.Exit(1)
}
fmt.Fprintf(logWriter, cfg.Colorize("\n\n\n[yellow]*********************************\n"))
fmt.Fprintf(logWriter, cfg.Colorize("[default]aws-k8s-tester eks delete cluster [light_green]SUCCESS\n"))
}