|
9 | 9 | package main |
10 | 10 |
|
11 | 11 | import ( |
12 | | - "flag" |
13 | | - "github.com/xfali/gobatis-cmd/internal/pkg/db" |
14 | | - "github.com/xfali/gobatis-cmd/internal/pkg/generator" |
15 | | - "github.com/xfali/gobatis-cmd/pkg/config" |
16 | | - "github.com/xfali/gobatis-cmd/pkg/io" |
17 | | - "log" |
18 | | - "os" |
19 | | - "strings" |
| 12 | + "encoding/json" |
| 13 | + "flag" |
| 14 | + "github.com/xfali/gobatis-cmd/internal/pkg/db" |
| 15 | + "github.com/xfali/gobatis-cmd/internal/pkg/generator" |
| 16 | + "github.com/xfali/gobatis-cmd/pkg/config" |
| 17 | + "github.com/xfali/gobatis-cmd/pkg/io" |
| 18 | + "io/ioutil" |
| 19 | + "log" |
| 20 | + "os" |
| 21 | + "strings" |
20 | 22 | ) |
21 | 23 |
|
22 | 24 | func main() { |
23 | | - driver := flag.String("driver", "mysql", "driver of db") |
24 | | - packageName := flag.String("pkg", "xfali.gobatis.default", "Set the package name of .go file") |
25 | | - dbName := flag.String("db", "", "the name of db instance used in model files") |
26 | | - tableName := flag.String("table", "", "the name of table to be generated") |
27 | | - host := flag.String("host", "localhost", "host of db") |
28 | | - port := flag.Int("port", 3306, "port of db ") |
29 | | - username := flag.String("user", "", "user name of db") |
30 | | - pw := flag.String("pw", "", "password of db") |
31 | | - path := flag.String("path", "", "root path to save files") |
32 | | - modelfile := flag.String("model", "", "the name of model file") |
33 | | - tagName := flag.String("tag", "xfield", "the name of field tag,eg: xfield,json xfield,json,yaml") |
34 | | - mapper := flag.String("mapper", "xml", "generate mapper file: xml | template | go") |
35 | | - plugin := flag.String("plugin", "", "path of plugin") |
36 | | - keyword := flag.Bool("keyword", false, "with Keyword escape") |
37 | | - namespace := flag.String("namespace", "", "namespace") |
38 | | - flag.Parse() |
| 25 | + driver := flag.String("driver", "mysql", "driver of db") |
| 26 | + packageName := flag.String("pkg", "xfali.gobatis.default", "Set the package name of .go file") |
| 27 | + dbName := flag.String("db", "", "the name of db instance used in model files") |
| 28 | + tableName := flag.String("table", "", "the name of table to be generated") |
| 29 | + host := flag.String("host", "localhost", "host of db") |
| 30 | + port := flag.Int("port", 3306, "port of db ") |
| 31 | + username := flag.String("user", "", "user name of db") |
| 32 | + pw := flag.String("pw", "", "password of db") |
| 33 | + path := flag.String("path", "", "root path to save files") |
| 34 | + modelfile := flag.String("model", "", "the name of model file") |
| 35 | + tagName := flag.String("tag", "xfield", "the name of field tag,eg: xfield,json xfield,json,yaml") |
| 36 | + mapper := flag.String("mapper", "xml", "generate mapper file: xml | template | go") |
| 37 | + plugin := flag.String("plugin", "", "path of plugin") |
| 38 | + keyword := flag.Bool("keyword", false, "with Keyword escape") |
| 39 | + namespace := flag.String("namespace", "", "namespace") |
| 40 | + confFile := flag.String("f", "", "config file") |
| 41 | + flag.Parse() |
39 | 42 |
|
40 | | - dbDriver := db.GetDriver(*driver) |
41 | | - if dbDriver == nil { |
42 | | - log.Print("not support driver: ", *driver) |
43 | | - os.Exit(-1) |
44 | | - } |
| 43 | + dbDriver := db.GetDriver(*driver) |
| 44 | + if dbDriver == nil { |
| 45 | + log.Print("not support driver: ", *driver) |
| 46 | + os.Exit(-1) |
| 47 | + } |
45 | 48 |
|
46 | | - err := dbDriver.Open(*driver, db.GenDBInfo(*driver, *dbName, *username, *pw, *host, *port)) |
47 | | - if err != nil { |
48 | | - log.Print(err) |
49 | | - os.Exit(-1) |
50 | | - } |
51 | | - defer dbDriver.Close() |
| 49 | + conf := config.FileConfig{} |
| 50 | + if *confFile != "" { |
| 51 | + err := loadFromFile(&conf, *confFile) |
| 52 | + if err != nil { |
| 53 | + os.Exit(1) |
| 54 | + } |
| 55 | + } |
52 | 56 |
|
53 | | - root := formatPath(*path) |
| 57 | + root := formatPath(*path) |
| 58 | + conf.Driver = *driver |
| 59 | + conf.Path = root |
| 60 | + conf.PackageName = *packageName |
| 61 | + conf.Namespace = *namespace |
| 62 | + conf.ModelFile = *modelfile |
| 63 | + conf.TagName = *tagName |
| 64 | + conf.MapperFile = *mapper |
| 65 | + conf.Plugin = *plugin |
| 66 | + conf.Keyword = *keyword |
| 67 | + conf.TableName = *tableName |
| 68 | + conf.DBName = *dbName |
| 69 | + conf.Host = *host |
| 70 | + conf.Port = *port |
| 71 | + conf.User = *username |
| 72 | + conf.Password = *pw |
54 | 73 |
|
55 | | - config := config.Config{ |
56 | | - Driver: *driver, |
57 | | - Path: root, |
58 | | - PackageName: *packageName, |
59 | | - Namespace: *namespace, |
60 | | - ModelFile: *modelfile, |
61 | | - TagName: *tagName, |
62 | | - MapperFile: *mapper, |
63 | | - Plugin: *plugin, |
64 | | - Keyword: *keyword, |
65 | | - } |
| 74 | + err := dbDriver.Open(conf.Driver, db.GenDBInfo(conf.Driver, conf.DBName, conf.User, conf.Password, conf.Host, conf.Port)) |
| 75 | + if err != nil { |
| 76 | + log.Print(err) |
| 77 | + os.Exit(-1) |
| 78 | + } |
| 79 | + defer dbDriver.Close() |
66 | 80 |
|
67 | | - if *tableName == "" { |
68 | | - tables, err2 := dbDriver.QueryTableNames(*dbName) |
69 | | - if err2 != nil { |
70 | | - log.Print(err2) |
71 | | - os.Exit(-2) |
72 | | - } |
73 | | - for _, v := range tables { |
74 | | - generator.GenOneTable(config, dbDriver, *dbName, v) |
75 | | - } |
76 | | - } else { |
77 | | - generator.GenOneTable(config, dbDriver, *dbName, *tableName) |
78 | | - } |
79 | | - os.Exit(0) |
| 81 | + if conf.TableName == "" { |
| 82 | + tables, err2 := dbDriver.QueryTableNames(conf.DBName) |
| 83 | + if err2 != nil { |
| 84 | + log.Print(err2) |
| 85 | + os.Exit(-2) |
| 86 | + } |
| 87 | + for _, v := range tables { |
| 88 | + generator.GenOneTable(conf.Config, dbDriver, conf.DBName, v) |
| 89 | + } |
| 90 | + } else { |
| 91 | + generator.GenOneTable(conf.Config, dbDriver, conf.DBName, conf.TableName) |
| 92 | + } |
| 93 | + os.Exit(0) |
| 94 | +} |
| 95 | + |
| 96 | +func loadFromFile(conf *config.FileConfig, path string) error { |
| 97 | + b, err := ioutil.ReadFile(path) |
| 98 | + if err != nil { |
| 99 | + return err |
| 100 | + } |
| 101 | + return json.Unmarshal(b, conf) |
80 | 102 | } |
81 | 103 |
|
82 | 104 | func formatPath(path string) string { |
83 | | - root := strings.TrimSpace(path) |
84 | | - if root == "" { |
85 | | - root = "./" |
86 | | - } else { |
87 | | - if !io.IsPathExists(path) { |
88 | | - io.Mkdir(path) |
89 | | - } |
90 | | - if root[len(root)-1:] != "/" { |
91 | | - root = root + "/" |
92 | | - } |
93 | | - } |
94 | | - return root |
| 105 | + root := strings.TrimSpace(path) |
| 106 | + if root == "" { |
| 107 | + root = "./" |
| 108 | + } else { |
| 109 | + if !io.IsPathExists(path) { |
| 110 | + io.Mkdir(path) |
| 111 | + } |
| 112 | + if root[len(root)-1:] != "/" { |
| 113 | + root = root + "/" |
| 114 | + } |
| 115 | + } |
| 116 | + return root |
95 | 117 | } |
0 commit comments