/
command_rm.go
194 lines (166 loc) · 4.33 KB
/
command_rm.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package command
import (
"bufio"
"errors"
"flag"
"fmt"
"os"
"os/signal"
"strings"
"github.com/aws/aws-sdk-go/service/rds"
"github.com/uchimanajet7/rds-try/utils"
)
// RmCommand struct is the *Command and OptSnap and OptForce and OptItem variable
type RmCommand struct {
*Command
OptSnap bool
OptForce bool
OptItem string
}
// ErrInterruptedAskDelete is the "OS Interrupted Ask Delete" error
var ErrInterruptedAskDelete = errors.New("OS Interrupted Ask Delete")
// Help is the show help text
func (c *RmCommand) Help() string {
// to-do: removal of the fixed value
helpText := fmt.Sprintf("\nUsage: %s rm [options]\n\n", utils.GetAppName())
helpText += "Options:\n"
helpText += " -s, --snap list up own db snapshots\n"
helpText += " -f, --force forced delete without confirmation\n"
return helpText
}
// Synopsis is the show short help text
func (c *RmCommand) Synopsis() string {
return "delete your created db instances and snapshots"
}
// Run is the start command
func (c *RmCommand) Run(args []string) int {
log.Infof("start command : rm")
// reset flag
fs := flag.NewFlagSet("rm", flag.ExitOnError)
// register flag name
fs.BoolVar(&c.OptSnap, "snap", false, "include own db snapshots to delete")
fs.BoolVar(&c.OptSnap, "s", false, "include own db snapshots to delete")
fs.BoolVar(&c.OptForce, "force", false, "forced delete without confirmation")
fs.BoolVar(&c.OptForce, "f", false, "forced delete without confirmation")
fs.Usage = func() { fmt.Println(c.Help()) }
err := fs.Parse(args)
if err != nil {
log.Errorf("%s", err.Error())
return 1
}
err = c.runDetails(fs)
if err != nil {
log.Errorf("%s", err.Error())
return 1
}
log.Infof("end command : rm")
return 0
}
func (c *RmCommand) runDetails(f *flag.FlagSet) error {
// to get list created in this tool
dbList, err := c.DescribeDBInstancesByTags()
if err != nil {
return err
}
askCount := 0
// show db list
if len(dbList) <= 0 {
fmt.Printf("\ndb instance list not exist\n")
} else {
askCount++
fmt.Printf("\nlist of own db instance\n")
for i, db := range dbList {
fmt.Printf(" [% d] DB Instance: %s\n", i+1, *db.DBInstanceIdentifier)
}
}
// blank new line
fmt.Println("")
var snapList []*rds.DBSnapshot
if c.OptSnap {
// to get list created in this tool
snapList, err = c.DescribeDBSnapshotsByTags()
if err != nil {
return err
}
// show snapshot list
if len(snapList) <= 0 {
fmt.Printf("db snapshot list not exist\n")
} else {
askCount++
fmt.Printf("list of own db snapshot\n")
for i, snap := range snapList {
fmt.Printf(" [% d] DB Snapshot: %s\n", i+1, *snap.DBSnapshotIdentifier)
}
}
// blank new line
fmt.Println("")
}
// list does not exist
if askCount <= 0 {
return nil
}
// confirm delete
var askResp string
if c.OptForce {
askResp = "yes"
} else {
askResp, err = askQuestion("you want to delete all of those? [y/n]:")
if err != nil {
log.Errorf("%s", err.Error())
return err
}
}
// blank new line
fmt.Println("")
switch askResp {
case "y", "Y", "yes", "YES", "Yes":
// delete db instance
err = c.DeleteDBResources(dbList)
if err != nil {
return err
}
// delete db snapshot
if c.OptSnap {
err = c.DeleteDBResources(snapList)
if err != nil {
return err
}
}
}
return nil
}
// this method copied
// see also
// https://github.com/mitchellh/cli
func askQuestion(query string) (string, error) {
// show query string
fmt.Printf("%s ", query)
// Register for interrupts so that we can catch it and immediately
// return...
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, os.Interrupt)
defer signal.Stop(sigCh)
// Ask for input in a go-routine so that we can ignore it.
errCh := make(chan error, 1)
lineCh := make(chan string, 1)
go func() {
r := bufio.NewReader(os.Stdin)
line, err := r.ReadString('\n')
if err != nil {
errCh <- err
return
}
lineCh <- strings.TrimRight(line, "\r\n")
}()
select {
case err := <-errCh:
return "", err
case line := <-lineCh:
return line, nil
case <-sigCh:
// Print a newline so that any further output starts properly
// on a new line.
fmt.Println("")
return "", ErrInterruptedAskDelete
}
}