This repository has been archived by the owner on Jun 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.go
96 lines (78 loc) · 2.23 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
84
85
86
87
88
89
90
91
92
93
94
95
96
package main
import (
"database/sql"
"flag"
"fmt"
"log"
"math/rand"
"os"
"time"
"gopkg.in/cheggaaa/pb.v1"
"github.com/codingconcepts/datagen/internal/pkg/parse"
"github.com/codingconcepts/datagen/internal/pkg/runner"
_ "github.com/go-sql-driver/mysql"
_ "github.com/lib/pq"
)
var semver string
func main() {
rand.Seed(time.Now().UnixNano())
log.SetFlags(log.LstdFlags | log.Lshortfile)
driver := flag.String("driver", "", "name of the database driver to use [postgres|mysql]")
script := flag.String("script", "", "the full or relative path to your script file")
conn := flag.String("conn", "", "the database connection string")
dateFmt := flag.String("datefmt", "2006-01-02", "the Go date format for all database dates")
debug := flag.Bool("debug", false, "dry run without writing to database, ref, row, and each won't work")
version := flag.Bool("version", false, "display the current version number")
flag.Parse()
if *version {
fmt.Println(semver)
os.Exit(2)
}
if *script == "" || *driver == "" || *conn == "" {
flag.Usage()
os.Exit(2)
}
db := mustConnect(*driver, *conn)
defer db.Close()
runner := runner.New(db, runner.WithDateFormat(*dateFmt), runner.WithDebug(*debug))
file, err := os.Open(*script)
if err != nil {
log.Fatalf("error reading script file: %v", err)
}
defer file.Close()
blocks, err := parse.Blocks(file)
if err != nil {
log.Fatalf("error reading blocks from script file: %v", err)
}
bar := newProgressBar(blocks)
for _, block := range blocks {
runner.ResetEach(block.Name)
for i := 0; i < block.Repeat; i++ {
bar.Increment()
if err = runner.Run(block); err != nil {
log.Fatalf("error running block %q: %v", block.Name, err)
}
}
}
bar.FinishPrint("Finished")
}
func newProgressBar(blocks []parse.Block) *pb.ProgressBar {
var count int
for _, block := range blocks {
count += block.Repeat
}
bar := pb.New(count)
bar.SetRefreshRate(time.Millisecond * 100)
bar.ShowCounters = false
return bar.Start()
}
func mustConnect(driver, connStr string) *sql.DB {
conn, err := sql.Open(driver, connStr)
if err != nil {
log.Fatalf("error opening connection: %d", err)
}
if err = conn.Ping(); err != nil {
log.Fatalf("error checking connection: %v", err)
}
return conn
}