forked from gobuffalo/pop
/
fizz_cmd.go
73 lines (64 loc) · 1.35 KB
/
fizz_cmd.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
package generate
import (
"context"
"github.com/gobuffalo/attrs"
"github.com/gobuffalo/genny"
"github.com/gobuffalo/logger"
"github.com/gobuffalo/pop/genny/fizz/cempty"
"github.com/gobuffalo/pop/genny/fizz/ctable"
"github.com/spf13/cobra"
)
// FizzCmd generates a new fizz migration
var FizzCmd = &cobra.Command{
Use: "fizz [name]",
Aliases: []string{"migration"},
Short: "Generates Up/Down migrations for your database using fizz.",
RunE: func(cmd *cobra.Command, args []string) error {
name := ""
if len(args) > 0 {
name = args[0]
}
var (
atts attrs.Attrs
err error
)
if len(args) > 1 {
atts, err = attrs.ParseArgs(args[1:]...)
if err != nil {
return err
}
}
run := genny.WetRunner(context.Background())
// Ensure the generator is as verbose as the old one.
lg := logger.New(logger.DebugLevel)
run.Logger = lg
p := cmd.Flag("path")
path := ""
if p != nil {
path = p.Value.String()
}
if len(atts) == 0 {
g, err := cempty.New(&cempty.Options{
TableName: name,
Path: path,
Type: "fizz",
})
if err != nil {
return err
}
run.With(g)
} else {
g, err := ctable.New(&ctable.Options{
TableName: name,
Path: path,
Type: "fizz",
Attrs: atts,
})
if err != nil {
return err
}
run.With(g)
}
return run.Run()
},
}