-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.go
189 lines (157 loc) · 4.83 KB
/
cli.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
package main
import (
"fmt"
"os"
"github.com/gernest/wow"
"github.com/gernest/wow/spin"
"github.com/urfave/cli/v2"
)
func startCli() *cli.App {
// Version Flag
cli.VersionFlag = &cli.BoolFlag{
Name: "version",
Aliases: []string{"V"},
Usage: "Print out version",
}
// Main CLI
app := &cli.App{
Name: "Yeo!",
Version: "v0.4.9",
Usage: "Backup utilities for PostgreSQL databases",
Commands: []*cli.Command{
{
Name: "backup",
Usage: "creates a backup of a database. This is also known as 'dump'.",
UsageText: "yeo backup [database] [filename]\n\nExample: yeo backup my_db my_db_backup.dump",
Action: func(cCtx *cli.Context) error {
database := cCtx.Args().Get(0)
filename := cCtx.Args().Get(1)
// Validates input
if err := validateBackupArgs(cCtx.Args().Slice(), database); err != nil {
fmt.Println(err)
os.Exit(1)
}
// Validates that there's a connection with database
if err := checkDbCons(database); err != nil {
fmt.Println(err)
os.Exit(1)
}
os.Setenv("FILENAME", filename)
// Checks '.dump' filename provided is valid
if err := validateFilename(filename); err != nil {
fmt.Println(err)
os.Exit(1)
}
// Backs up a database
if err := backup(database, filename, false); err != nil {
fmt.Println(err)
os.Exit(1)
}
return nil
},
},
{
Name: "revive",
Usage: "revives a database from a backup. This is also known as 'restore'.",
UsageText: "yeo revive [options] [dump filename] [database]\n\nExample 1: yeo revive db_backup.dump local_db\n\nExample 2: yeo revive --allow db_backup.dump prod",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "allow",
Usage: "allows to revive into non-local databases",
},
},
Action: func(cCtx *cli.Context) error {
filename := cCtx.Args().Get(0)
database := cCtx.Args().Get(1)
// Validates input
if err := validateReviveArgs(cCtx.Args().Slice(), database); err != nil {
fmt.Println(err)
os.Exit(1)
}
// Checks '.dump' filename provided is valid
if err := validateFilename(filename); err != nil {
fmt.Println(err)
os.Exit(1)
}
// Validates that there's a connection with database
if err := checkDbCons(database); err != nil {
fmt.Println(err)
os.Exit(1)
}
// Checks the target database is allowed
isUnlocked := cCtx.Bool("allow")
if err := validateTargetDb(filename, database, "revive", isUnlocked); err != nil {
fmt.Println(err)
os.Exit(1)
}
// Prepares database by dropping and creating a new database
if err := prepareDb(database); err != nil {
fmt.Println(err)
os.Exit(1)
}
// Restores a database
if err := revive(database, filename, false); err != nil {
fmt.Println(err)
os.Exit(1)
}
return nil
},
},
{
Name: "clone",
Usage: "clones a database into another database. This is also known as 'dump' and 'restore'.",
UsageText: "yeo clone [options] [origin database] [target database]\n\nExample 1: yeo clone prod local_db\n\nExample 2: yeo clone --allow local_db prod",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "allow",
Usage: "allows to clone into non-local databases",
},
},
Action: func(cCtx *cli.Context) error {
// Cloning doesn't need specific filename and so a temporal name is given
temp_filename := "temp.dump"
// This is set for the 'cleanup' function
os.Setenv("FILENAME", temp_filename)
// User args
og_database := cCtx.Args().Get(0)
target_database := cCtx.Args().Get(1)
// Validates input
if err := validateCloneArgs(cCtx.Args().Slice(), og_database, target_database); err != nil {
fmt.Println(err)
os.Exit(1)
}
isUnlocked := cCtx.Bool("allow")
// Checks the target database is allowed
if err := validateTargetDb(og_database, target_database, "clone", isUnlocked); err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println() // Used for padding
// Spin wheel
w := wow.New(os.Stdout, spin.Get(spin.Dots), fmt.Sprintf(" Cloning '%s' database", og_database))
w.Start()
// Dumps a database
err := backup(og_database, temp_filename, true)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// Prepares database by dropping and creating a new database
if err := prepareDb(target_database); err != nil {
fmt.Println(err)
os.Exit(1)
}
// Restores a database
if err = revive(target_database, temp_filename, true); err != nil {
fmt.Println(err)
os.Exit(1)
}
// Removes the 'temp.dump' file generated during the process
cleanup()
return nil
},
},
},
}
return app
}