/
db_destroy.go
105 lines (83 loc) · 2.76 KB
/
db_destroy.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
package cmd
import (
"errors"
"fmt"
"strings"
"github.com/spf13/cobra"
"github.com/tursodatabase/turso-cli/internal"
"github.com/tursodatabase/turso-cli/internal/turso"
)
func init() {
dbCmd.AddCommand(destroyCmd)
destroyCmd.Flags().BoolVarP(&yesFlag, "yes", "y", false, "Confirms the destruction of all locations of the database.")
addLocationFlag(destroyCmd, "Pick a database location to destroy.")
addInstanceFlag(destroyCmd, "Pick a specific database instance to destroy.")
destroyCmd.RegisterFlagCompletionFunc("instance", completeInstanceName)
}
var destroyCmd = &cobra.Command{
Use: "destroy <database-name>",
Short: "Destroy a database.",
Args: cobra.MinimumNArgs(1),
ValidArgsFunction: dbNameArg,
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
client, err := authedTursoClient()
if err != nil {
return err
}
if len(args) > 1 {
return handleDestroyMultipleDBs(args, client)
}
return handleDestroySingleDB(args, client)
},
}
func handleDestroySingleDB(args []string, client *turso.Client) error {
name := args[0]
db, err := getDatabase(client, name)
if err != nil {
return nil
}
if instanceFlag != "" {
if db.Group != "" {
return fmt.Errorf("group databases do not support instance destruction.\nUse %s instead", internal.Emph("turso group locations rm "+name))
}
return destroyDatabaseInstance(client, name, instanceFlag)
}
if locationFlag != "" {
if db.Group != "" {
return fmt.Errorf("group databases do not support location destruction.\nUse %s instead", internal.Emph("turso group locations rm "+name+" "+locationFlag))
}
return destroyDatabaseRegion(client, name, locationFlag)
}
if yesFlag {
return destroyDatabases(client, args)
}
fmt.Printf("Database %s and all its data will be destroyed.\n", internal.Emph(name))
ok, err := promptConfirmation("Are you sure you want to do this?")
if err != nil {
return fmt.Errorf("could not get prompt confirmed by user: %w", err)
}
if !ok {
fmt.Println("Database destruction avoided.")
return nil
}
return destroyDatabases(client, args)
}
func handleDestroyMultipleDBs(args []string, client *turso.Client) error {
if instanceFlag != "" || locationFlag != "" {
return errors.New("can not use location nor instance flag when deleting more than 1 database")
}
if yesFlag {
return destroyDatabases(client, args)
}
fmt.Printf("Databases %s and all their data will be destroyed.\n", internal.Emph(strings.Join(args, ", ")))
ok, err := promptConfirmation("Are you sure you want to do this?")
if err != nil {
return fmt.Errorf("could not get prompt confirmed by user: %w", err)
}
if !ok {
fmt.Println("Databases destruction avoided.")
return nil
}
return destroyDatabases(client, args)
}