/
fizz_migrations_create.go
49 lines (40 loc) · 1.04 KB
/
fizz_migrations_create.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
package cmd
import (
"fmt"
"os"
"time"
"github.com/ory/x/flagx"
"github.com/ory/x/stringslice"
"github.com/spf13/cobra"
)
var supportedDialects = []string{
"sqlite",
"cockroach",
"mysql",
"postgres",
}
// fizzMigrationsCreateCmd represents the create command
var fizzMigrationsCreateCmd = &cobra.Command{
Use: "create [name]",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
prefix := time.Now().Format("20060102150405")
d := flagx.MustGetString(cmd, "dialect")
suffix := ".fizz"
if len(d) > 0 {
if stringslice.Has(supportedDialects, d) {
return fmt.Errorf(`expected dialect to be one of %v but got: %s`, supportedDialects, d)
}
suffix = fmt.Sprintf(".%s.sql", d)
}
f, err := os.Create(fmt.Sprintf("%s_%s%s", prefix, args[0], suffix))
if err != nil {
return err
}
return f.Close()
},
}
func init() {
fizzMigrationsCmd.AddCommand(fizzMigrationsCreateCmd)
fizzMigrationsCreateCmd.Flags().StringP("dialect", "d", "", fmt.Sprintf(`Choose a dialect from: %v`, supportedDialects))
}