|
1 | | -/** |
2 | | - * Copyright (C) 2019, Xiongfa Li. |
3 | | - * All right reserved. |
4 | | - * @author xiongfa.li |
5 | | - * @version V1.0 |
6 | | - * Description: |
| 1 | +/* |
| 2 | + * Copyright (c) 2022, AcmeStack |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
7 | 16 | */ |
8 | 17 |
|
9 | 18 | package main |
10 | 19 |
|
11 | 20 | import ( |
12 | | - "encoding/json" |
13 | | - "flag" |
14 | | - "fmt" |
15 | | - "github.com/xfali/gobatis-cmd/pkg/config" |
16 | | - "github.com/xfali/gobatis-cmd/pkg/db" |
17 | | - "github.com/xfali/gobatis-cmd/pkg/generator" |
18 | | - "github.com/xfali/gobatis-cmd/pkg/io" |
19 | | - "io/ioutil" |
20 | | - "log" |
21 | | - "os" |
22 | | - "strings" |
| 21 | + "encoding/json" |
| 22 | + "flag" |
| 23 | + "fmt" |
| 24 | + "github.com/acmestack/gobatis-cmd/pkg/config" |
| 25 | + "github.com/acmestack/gobatis-cmd/pkg/db" |
| 26 | + "github.com/acmestack/gobatis-cmd/pkg/generator" |
| 27 | + "github.com/acmestack/gobatis-cmd/pkg/io" |
| 28 | + "io/ioutil" |
| 29 | + "log" |
| 30 | + "os" |
| 31 | + "strings" |
23 | 32 | ) |
24 | 33 |
|
25 | 34 | func main() { |
26 | | - driver := flag.String("driver", "mysql", "driver of db") |
27 | | - packageName := flag.String("pkg", "xfali.gobatis.default", "Set the package name of .go file") |
28 | | - dbName := flag.String("db", "", "the name of db instance used in model files") |
29 | | - tableName := flag.String("table", "", "the name of table to be generated") |
30 | | - host := flag.String("host", "localhost", "host of db") |
31 | | - port := flag.Int("port", 3306, "port of db ") |
32 | | - username := flag.String("user", "", "user name of db") |
33 | | - pw := flag.String("pw", "", "password of db") |
34 | | - path := flag.String("path", "", "root path to save files") |
35 | | - modelfile := flag.String("model", "", "the name of model file") |
36 | | - tagName := flag.String("tag", "xfield", "the name of field tag,eg: xfield,json xfield,json,yaml") |
37 | | - mapper := flag.String("mapper", "xml", "generate mapper file: xml | template | go") |
38 | | - plugin := flag.String("plugin", "", "path of plugin") |
39 | | - keyword := flag.Bool("keyword", false, "with Keyword escape") |
40 | | - namespace := flag.String("namespace", "", "namespace") |
41 | | - confFile := flag.String("f", "", "config file") |
42 | | - register := flag.Bool("register", false, "add register code") |
43 | | - flag.Parse() |
| 35 | + driver := flag.String("driver", "mysql", "driver of db") |
| 36 | + packageName := flag.String("pkg", "acmestack.gobatis.default", "Set the package name of .go file") |
| 37 | + dbName := flag.String("db", "", "the name of db instance used in model files") |
| 38 | + tableName := flag.String("table", "", "the name of table to be generated") |
| 39 | + host := flag.String("host", "localhost", "host of db") |
| 40 | + port := flag.Int("port", 3306, "port of db ") |
| 41 | + username := flag.String("user", "", "user name of db") |
| 42 | + pw := flag.String("pw", "", "password of db") |
| 43 | + path := flag.String("path", "", "root path to save files") |
| 44 | + modelfile := flag.String("model", "", "the name of model file") |
| 45 | + tagName := flag.String("tag", "xfield", "the name of field tag,eg: xfield,json xfield,json,yaml") |
| 46 | + mapper := flag.String("mapper", "xml", "generate mapper file: xml | template | go") |
| 47 | + plugin := flag.String("plugin", "", "path of plugin") |
| 48 | + keyword := flag.Bool("keyword", false, "with Keyword escape") |
| 49 | + namespace := flag.String("namespace", "", "namespace") |
| 50 | + confFile := flag.String("f", "", "config file") |
| 51 | + register := flag.Bool("register", false, "add register code") |
| 52 | + flag.Parse() |
44 | 53 |
|
45 | | - conf := config.FileConfig{} |
46 | | - if *confFile != "" { |
47 | | - err := loadFromFile(&conf, *confFile) |
48 | | - if err != nil { |
49 | | - fmt.Println(err) |
50 | | - os.Exit(1) |
51 | | - } |
52 | | - conf.Path = formatPath(conf.Path) |
53 | | - } else { |
54 | | - conf.Driver = *driver |
55 | | - conf.Path = formatPath(*path) |
56 | | - conf.PackageName = *packageName |
57 | | - conf.Namespace = *namespace |
58 | | - conf.ModelFile = *modelfile |
59 | | - conf.TagName = *tagName |
60 | | - conf.MapperFile = *mapper |
61 | | - conf.Plugin = *plugin |
62 | | - conf.Keyword = *keyword |
63 | | - conf.Register = *register |
64 | | - conf.TableName = *tableName |
65 | | - conf.DBName = *dbName |
66 | | - conf.Host = *host |
67 | | - conf.Port = *port |
68 | | - conf.User = *username |
69 | | - conf.Password = *pw |
70 | | - } |
| 54 | + conf := config.FileConfig{} |
| 55 | + if *confFile != "" { |
| 56 | + err := loadFromFile(&conf, *confFile) |
| 57 | + if err != nil { |
| 58 | + fmt.Println(err) |
| 59 | + os.Exit(1) |
| 60 | + } |
| 61 | + conf.Path = formatPath(conf.Path) |
| 62 | + } else { |
| 63 | + conf.Driver = *driver |
| 64 | + conf.Path = formatPath(*path) |
| 65 | + conf.PackageName = *packageName |
| 66 | + conf.Namespace = *namespace |
| 67 | + conf.ModelFile = *modelfile |
| 68 | + conf.TagName = *tagName |
| 69 | + conf.MapperFile = *mapper |
| 70 | + conf.Plugin = *plugin |
| 71 | + conf.Keyword = *keyword |
| 72 | + conf.Register = *register |
| 73 | + conf.TableName = *tableName |
| 74 | + conf.DBName = *dbName |
| 75 | + conf.Host = *host |
| 76 | + conf.Port = *port |
| 77 | + conf.User = *username |
| 78 | + conf.Password = *pw |
| 79 | + } |
71 | 80 |
|
72 | | - dbDriver := db.GetDriver(conf.Driver) |
73 | | - if dbDriver == nil { |
74 | | - log.Print("not support driver: ", conf.Driver) |
75 | | - os.Exit(-1) |
76 | | - } |
| 81 | + dbDriver := db.GetDriver(conf.Driver) |
| 82 | + if dbDriver == nil { |
| 83 | + log.Print("not support driver: ", conf.Driver) |
| 84 | + os.Exit(-1) |
| 85 | + } |
77 | 86 |
|
78 | | - err := dbDriver.Open(conf.Driver, db.GenDBInfo(conf.Driver, conf.DBName, conf.User, conf.Password, conf.Host, conf.Port)) |
79 | | - if err != nil { |
80 | | - log.Print(err) |
81 | | - os.Exit(-1) |
82 | | - } |
83 | | - defer dbDriver.Close() |
| 87 | + err := dbDriver.Open(conf.Driver, db.GenDBInfo(conf.Driver, conf.DBName, conf.User, conf.Password, conf.Host, conf.Port)) |
| 88 | + if err != nil { |
| 89 | + log.Print(err) |
| 90 | + os.Exit(-1) |
| 91 | + } |
| 92 | + defer dbDriver.Close() |
84 | 93 |
|
85 | | - if conf.TableName == "" { |
86 | | - tables, err2 := dbDriver.QueryTableNames(conf.DBName) |
87 | | - if err2 != nil { |
88 | | - log.Print(err2) |
89 | | - os.Exit(-2) |
90 | | - } |
91 | | - for _, v := range tables { |
92 | | - generator.GenOneTable(conf.Config, dbDriver, conf.DBName, v) |
93 | | - } |
94 | | - } else { |
95 | | - generator.GenOneTable(conf.Config, dbDriver, conf.DBName, conf.TableName) |
96 | | - } |
97 | | - os.Exit(0) |
| 94 | + if conf.TableName == "" { |
| 95 | + tables, err2 := dbDriver.QueryTableNames(conf.DBName) |
| 96 | + if err2 != nil { |
| 97 | + log.Print(err2) |
| 98 | + os.Exit(-2) |
| 99 | + } |
| 100 | + for _, v := range tables { |
| 101 | + generator.GenOneTable(conf.Config, dbDriver, conf.DBName, v) |
| 102 | + } |
| 103 | + } else { |
| 104 | + generator.GenOneTable(conf.Config, dbDriver, conf.DBName, conf.TableName) |
| 105 | + } |
| 106 | + os.Exit(0) |
98 | 107 | } |
99 | 108 |
|
100 | 109 | func loadFromFile(conf *config.FileConfig, path string) error { |
101 | | - b, err := ioutil.ReadFile(path) |
102 | | - if err != nil { |
103 | | - return err |
104 | | - } |
105 | | - fmt.Printf("config file: %s\n", string(b)) |
106 | | - return json.Unmarshal(b, conf) |
| 110 | + b, err := ioutil.ReadFile(path) |
| 111 | + if err != nil { |
| 112 | + return err |
| 113 | + } |
| 114 | + fmt.Printf("config file: %s\n", string(b)) |
| 115 | + return json.Unmarshal(b, conf) |
107 | 116 | } |
108 | 117 |
|
109 | 118 | func formatPath(path string) string { |
110 | | - root := strings.TrimSpace(path) |
111 | | - if root == "" { |
112 | | - root = "./" |
113 | | - } else { |
114 | | - if !io.IsPathExists(path) { |
115 | | - io.Mkdir(path) |
116 | | - } |
117 | | - if root[len(root)-1:] != "/" { |
118 | | - root = root + "/" |
119 | | - } |
120 | | - } |
121 | | - return root |
| 119 | + root := strings.TrimSpace(path) |
| 120 | + if root == "" { |
| 121 | + root = "./" |
| 122 | + } else { |
| 123 | + if !io.IsPathExists(path) { |
| 124 | + io.Mkdir(path) |
| 125 | + } |
| 126 | + if root[len(root)-1:] != "/" { |
| 127 | + root = root + "/" |
| 128 | + } |
| 129 | + } |
| 130 | + return root |
122 | 131 | } |
0 commit comments