-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.go
109 lines (88 loc) · 2.28 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
97
98
99
100
101
102
103
104
105
106
107
108
109
package main
import (
"database/sql"
"errors"
"fmt"
"log"
"strings"
"github.com/gobuffalo/packr"
"github.com/spf13/cobra"
_ "github.com/go-sql-driver/mysql"
)
var (
output *string
dbName *string
pkgName *string
conn *string
database *sql.DB
version string
box packr.Box
)
func init() {
log.SetFlags(log.LstdFlags | log.Lshortfile)
}
func main() {
box = packr.NewBox("./tmpl")
rootCmd := &cobra.Command{}
pkgName = rootCmd.PersistentFlags().StringP("package", "p", "generated_models", "name of package")
output = rootCmd.PersistentFlags().StringP("output", "o", "generated_models", "path to package")
dbName = rootCmd.PersistentFlags().StringP("database", "d", "", "name of database")
conn = rootCmd.PersistentFlags().StringP("connection", "c", "", "user:pass@host:port")
generateCmd := &cobra.Command{
Use: "generate",
Run: generate,
Short: "Generate models from a database connection",
}
migrateCmd := &cobra.Command{
Use: "migrate",
Run: migrate,
Short: "Generate migration files from a database connection",
}
versionCmd := &cobra.Command{
Use: "version",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println(version)
},
Short: "Returns the current version name",
}
rootCmd.AddCommand(generateCmd, migrateCmd, versionCmd)
if err := rootCmd.Execute(); err != nil {
log.Fatal(err)
}
}
var formatErr = errors.New("invalid connection string format")
func mkDsn(connect, dbname string) string {
parts := strings.Split(connect, "@")
if len(parts) < 2 {
log.Fatal(formatErr)
}
credentials := strings.Split(parts[0], ":")
if len(credentials) < 2 {
log.Fatal(formatErr)
}
database := strings.Split(parts[1], ":")
if len(database) < 2 {
log.Fatal(formatErr)
}
return fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?parseTime=true", credentials[0], credentials[1], database[0], database[1], dbname)
}
func connect() {
// connect to database
var err error
database, err = sql.Open("mysql", mkDsn(*conn, *dbName))
if err != nil {
log.Fatal(err)
}
// check for a valid connection
if err := database.Ping(); err != nil {
log.Fatal(err)
}
}
func validate() {
if dbName == nil || *dbName == "" {
log.Fatal("Please provide a database name")
}
if conn == nil || *conn == "" {
log.Fatal("Please provide a connection string")
}
}