Skip to content

Commit fcba8e5

Browse files
committed
增加config配置文件支持
1 parent ce8d3fe commit fcba8e5

File tree

4 files changed

+134
-81
lines changed

4 files changed

+134
-81
lines changed

cmd/gobatis-cmd/main.go

Lines changed: 94 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -9,87 +9,109 @@
99
package main
1010

1111
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"
2022
)
2123

2224
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()
3942

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+
}
4548

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+
}
5256

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
5473

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()
6680

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)
80102
}
81103

82104
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
95117
}

configs/gobatis-conf.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"driver": "mysql",
3+
"path": ".",
4+
"package": "test",
5+
"namespace": "test",
6+
"modelFile": "test_model.go",
7+
"tagName": "xfield",
8+
"mapperFile": "xml",
9+
"plugin": "",
10+
"keyword": false,
11+
"tableName": "",
12+
"dbName": "test_db",
13+
"host": "localhost",
14+
"port": 3306,
15+
"user": "",
16+
"password": ""
17+
}

pkg/config/config.go

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,23 @@
66
package config
77

88
type Config struct {
9-
Driver string
10-
Path string
11-
PackageName string
12-
Namespace string
13-
ModelFile string
14-
TagName string
15-
MapperFile string
16-
Plugin string
17-
Keyword bool
9+
Driver string `json:"driver"`
10+
Path string `json:"path"`
11+
PackageName string `json:"package"`
12+
Namespace string `json:"namespace"`
13+
ModelFile string `json:"modelFile"`
14+
TagName string `json:"tagName"`
15+
MapperFile string `json:"mapperFile"`
16+
Plugin string `json:"plugin"`
17+
Keyword bool `json:"keyword"`
18+
}
19+
20+
type FileConfig struct {
21+
Config
22+
TableName string `json:"tableName"`
23+
DBName string `json:"dbName"`
24+
Host string `json:"host"`
25+
Port int `json:"port"`
26+
User string `json:"user"`
27+
Password string `json:"password"`
1828
}

test/db_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ import (
1212
"testing"
1313
)
1414

15+
func TestMysqlInfo(t *testing.T) {
16+
t.Log(db2.GenDBInfo("mysql","testdb", "test", "test", "localhost", 3306))
17+
}
18+
1519
func TestMysql(t *testing.T) {
1620
db := db2.GetDriver("postgres")
1721
if db == nil {

0 commit comments

Comments
 (0)