Compare commits
No commits in common. "main" and "v1" have entirely different histories.
3 changed files with 6 additions and 38 deletions
11
README.md
11
README.md
|
@ -1,11 +0,0 @@
|
|||
# Usage
|
||||
|
||||
pgsplit takes a PostgreSQL dump with multiple databases through STDIN and extracts them to the current directory, named with the database name and an .sql extension.
|
||||
|
||||
`pgsplit -list` prints the database names contained in the SQL dump.
|
||||
|
||||
`pgsplit` extracts all databases.
|
||||
|
||||
`pgsplit -include-all -except foo -except bar` extracts all databases except for foo and bar.
|
||||
|
||||
`pgsplit -exclude-all -except foo -except bar` extracts only databases foo and bar.
|
14
flag.go
14
flag.go
|
@ -19,31 +19,17 @@ func (i *arrayFlags) Set(value string) error {
|
|||
}
|
||||
|
||||
var (
|
||||
flagVersion bool
|
||||
flagList bool
|
||||
flagIncludeAll bool
|
||||
flagExcludeAll bool
|
||||
flagExcept arrayFlags
|
||||
)
|
||||
|
||||
func init() {
|
||||
flag.BoolVar(&flagList, "list", false, "Only list database names.")
|
||||
flag.BoolVar(&flagVersion, "version", false, "Prints version and exits.")
|
||||
flag.BoolVar(&flagIncludeAll, "include-all", false, "Split out all databases, with provided exceptions.")
|
||||
flag.BoolVar(&flagExcludeAll, "exclude-all", false, "Split out no databases, with provided exceptions.")
|
||||
flag.Var(&flagExcept, "except", "Exception to rule, can be used multiple times")
|
||||
flag.Parse()
|
||||
|
||||
if flagVersion {
|
||||
fmt.Println(VERSION)
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
if flagList && (flagIncludeAll || flagExcludeAll || len(flagExcept) > 0) {
|
||||
fmt.Println("-list is not usable with any other flag.")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
if flagIncludeAll && flagExcludeAll {
|
||||
fmt.Println("-include-all and -exclude-all are mutually exclusive")
|
||||
os.Exit(1)
|
||||
|
|
19
main.go
19
main.go
|
@ -9,7 +9,7 @@ import (
|
|||
"slices"
|
||||
)
|
||||
|
||||
const VERSION = "v5"
|
||||
const VERSION = "v1"
|
||||
const MAXLINE = 1048576
|
||||
|
||||
type Db struct {
|
||||
|
@ -18,12 +18,10 @@ type Db struct {
|
|||
file *os.File
|
||||
}
|
||||
|
||||
func NewDb(name string) (db Db) {
|
||||
func NewDb(name string) (db Db, err error) {
|
||||
db.Name = name
|
||||
return
|
||||
}
|
||||
fmt.Printf("Database %s\n", db.Name)
|
||||
|
||||
func (db *Db) Open() (err error) {
|
||||
db.file, err = os.OpenFile(
|
||||
db.Name+".sql",
|
||||
os.O_CREATE|os.O_WRONLY|os.O_TRUNC,
|
||||
|
@ -66,23 +64,18 @@ func main() {
|
|||
prevDb.Close()
|
||||
|
||||
dbName := dbMatch[1]
|
||||
db = NewDb(dbName)
|
||||
if (flagIncludeAll && slices.Contains(flagExcept, dbName)) || (flagExcludeAll && !slices.Contains(flagExcept, dbName)) {
|
||||
fmt.Printf("Skipping %s\n", dbName)
|
||||
fmt.Printf("Skipping %s\n", dbName)
|
||||
continue
|
||||
}
|
||||
|
||||
fmt.Printf("Extracting %s\n", db.Name)
|
||||
if flagList {
|
||||
continue
|
||||
}
|
||||
if err = db.Open(); err != nil {
|
||||
if db, err = NewDb(dbName); err != nil {
|
||||
fmt.Printf("%s: %s\n", dbName, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
if !flagList && db.IsOpen() {
|
||||
if db.IsOpen() {
|
||||
db.Write(line)
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue