-
Notifications
You must be signed in to change notification settings - Fork 0
/
richelieu.go
103 lines (92 loc) · 2.21 KB
/
richelieu.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
package main
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"os"
"strconv"
"strings"
"time"
"github.com/brianvoe/gofakeit/v4"
"github.com/estebgonza/go-richelieu/generator"
"github.com/urfave/cli/v2"
)
const (
appName string = "Richelieu"
appDescription string = "Data generator that respects cardinality and schema structures#."
appVersion string = "0.1"
defaultPlanFile string = "plan.json"
)
const helpTemplate = `
Usage: {{.HelpName}} [command]
{{if .Commands}}Commands:
{{range .Commands}}{{if not .HideHelp}}{{join .Names ", "}}{{ "\t"}}{{.Usage}}{{ "\n" }}{{end}}{{end}}{{end}}
`
func main() {
cli.AppHelpTemplate = fmt.Sprintf(helpTemplate)
app := cli.NewApp()
app.Name = appName
app.Usage = appDescription
app.Version = appVersion
app.Commands = []*cli.Command{
{
Name: "generate",
Usage: "Execute the generation plan",
Action: generate,
},
{
Name: "create",
Usage: "Create a generation plan",
Action: create,
},
}
log.Println("Starting generation...")
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
log.Println("Done generating")
}
func generate(c *cli.Context) error {
var planFile *os.File
var byteValue []byte
var p generator.Plan
gofakeit.Seed(time.Now().UnixNano())
planFile, err := os.Open(defaultPlanFile)
if err != nil {
return errors.New("No plan.json found")
}
byteValue, _ = ioutil.ReadAll(planFile)
json.Unmarshal(byteValue, &p)
errExec := generator.Execute(&p)
if errExec != nil {
return errExec
}
fmt.Printf("Done. %d rows just generated.\n", p.Rows)
return nil
}
func create(c *cli.Context) error {
if c.Args().Len() == 0 {
return errors.New("Please specify columns type to init a generation plan")
}
cols := strings.Split(c.Args().Get(0), ",")
var columns []generator.PlanColumn
for index, t := range cols {
if err := generator.ChecksSupportedType(t); err != nil {
return err
}
var pc generator.PlanColumn
pc.Name = strings.ToLower(strconv.Itoa(index) + "_" + t)
pc.Distinct = 5
pc.Type = t
columns = append(columns, pc)
}
var plan generator.Plan
plan.Rows = 10000
plan.PlanColumns = columns
b, _ := json.Marshal(plan)
ioutil.WriteFile(defaultPlanFile, b, 0644)
return nil
}