-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
83 lines (63 loc) · 1.82 KB
/
main.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
package main
import (
"fmt"
"os"
"errors"
//"context"
"time"
"github.com/msrevive/db-migration/internal/migrate"
"github.com/msrevive/db-migration/internal/migrate/bboltdb"
"github.com/msrevive/db-migration/internal/migrate/badgerdb"
_ "modernc.org/sqlite"
"github.com/spf13/pflag"
)
type flags struct {
originDB string
originDBFile string
destDB string
destDBFile string
}
func doFlags(args []string) *flags {
flgs := &flags{}
flagSet := pflag.NewFlagSet(args[0], pflag.ExitOnError)
flagSet.StringVar(&flgs.originDBFile, "origindbfile", "", "Original database file.")
flagSet.StringVar(&flgs.destDB, "destdb", "badger", "Database to migrate to.")
flagSet.StringVar(&flgs.destDBFile, "destdbfile", "", "What should the destination database file be name.")
flagSet.Parse(args[1:])
return flgs
}
func main() {
// Handle argument flags
flags := doFlags(os.Args)
fmt.Printf("Starting application with arguements %s\n", os.Args[1:])
if flags.originDBFile == "" {
fmt.Println("ERROR: no origin DB supplied!")
os.Exit(1)
}
if flags.destDBFile == "" {
fmt.Println("ERROR: no destination DB supplied!")
os.Exit(1)
}
if _, err := os.Stat(flags.originDBFile); errors.Is(err, os.ErrNotExist) {
fmt.Printf("ERROR: unable to find origin DB file! %s\n", flags.originDBFile)
os.Exit(1)
}
// Handle migration stuff
var migration migrate.Migrate
switch flags.destDB {
case "bbolt":
migration = bboltdb.New()
case "badger":
migration = badgerdb.New()
default:
fmt.Printf("ERROR: destination DB type not supported %s\n", flags.destDB)
os.Exit(1)
}
fmt.Printf("Beginning migration of DB to %s...\n", flags.destDB)
start := time.Now()
if err := migration.Migrate(flags.originDBFile, flags.destDBFile); err != nil {
panic(err)
}
fmt.Printf("Migration finished, took %v\n", time.Since(start))
os.Exit(0)
}