-
Notifications
You must be signed in to change notification settings - Fork 52
/
delete.go
123 lines (102 loc) · 3.3 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
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
package database
import (
"errors"
"fmt"
"os"
"github.com/planetscale/cli/internal/cmdutil"
"github.com/planetscale/cli/internal/printer"
"github.com/planetscale/planetscale-go/planetscale"
"github.com/AlecAivazis/survey/v2"
"github.com/AlecAivazis/survey/v2/terminal"
"github.com/spf13/cobra"
)
// DeleteCmd is the Cobra command for deleting a database for an authenticated
// user.
func DeleteCmd(ch *cmdutil.Helper) *cobra.Command {
var force bool
cmd := &cobra.Command{
Use: "delete <database>",
Short: "Delete a database instance",
Args: cmdutil.RequiredArgs("database"),
Aliases: []string{"rm"},
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
name := args[0]
client, err := ch.Client()
if err != nil {
return err
}
if !force {
if ch.Printer.Format() != printer.Human {
return fmt.Errorf("cannot delete database with the output format %q (run with -force to override)", ch.Printer.Format())
}
if !printer.IsTTY {
return fmt.Errorf("cannot confirm deletion of database %q (run with -force to override)", name)
}
_, err := client.Databases.Get(ctx, &planetscale.GetDatabaseRequest{
Organization: ch.Config.Organization,
Database: name,
})
if err != nil {
switch cmdutil.ErrCode(err) {
case planetscale.ErrNotFound:
return fmt.Errorf("database %s does not exist in organization %s",
printer.BoldBlue(name), printer.BoldBlue(ch.Config.Organization))
default:
return cmdutil.HandleError(err)
}
}
confirmationMessage := fmt.Sprintf("%s %s %s", printer.Bold("Please type"), printer.BoldBlue(name), printer.Bold("to confirm:"))
prompt := &survey.Input{
Message: confirmationMessage,
}
var userInput string
err = survey.AskOne(prompt, &userInput)
if err != nil {
if err == terminal.InterruptErr {
os.Exit(0)
} else {
return err
}
}
if userInput != name {
return errors.New("incorrect database name entered, skipping database deletion")
}
}
end := ch.Printer.PrintProgress(fmt.Sprintf("Deleting database %s...", printer.BoldBlue(name)))
defer end()
dbr, err := client.Databases.Delete(ctx, &planetscale.DeleteDatabaseRequest{
Organization: ch.Config.Organization,
Database: name,
})
if err != nil {
switch cmdutil.ErrCode(err) {
case planetscale.ErrNotFound:
return fmt.Errorf("database %s does not exist in organization %s",
printer.BoldBlue(name), printer.BoldBlue(ch.Config.Organization))
default:
return cmdutil.HandleError(err)
}
}
end()
if dbr != nil {
return &cmdutil.Error{
Msg: fmt.Sprintf("A deletion request for database %s was successfully created. Database will be deleted after another database administrator also requests deletion.", printer.BoldBlue(name)),
ExitCode: cmdutil.ActionRequestedExitCode,
}
}
if ch.Printer.Format() == printer.Human {
ch.Printer.Printf("Database %s was successfully deleted.\n", printer.BoldBlue(name))
return nil
}
return ch.Printer.PrintResource(
map[string]string{
"result": "database deleted",
"database": name,
},
)
},
}
cmd.Flags().BoolVar(&force, "force", false, "Delete a database without confirmation")
return cmd
}