Skip to content

Commit

Permalink
add config options
Browse files Browse the repository at this point in the history
  • Loading branch information
室岡 豊人 authored and 室岡 豊人 committed Oct 28, 2019
1 parent e2d3339 commit 8b17a46
Show file tree
Hide file tree
Showing 2 changed files with 155 additions and 53 deletions.
30 changes: 26 additions & 4 deletions cmd/cmd/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,34 @@ var confCmd = &cobra.Command{
switch action {
case "add":

err = expl.AddHostAndDatabase(ctx, user, pass, host, database, port, protocol, filePath)
fmt.Printf("conf %s %s %s %s %s %d %s %s\n", action, host, database, user, pass, port, protocol, filePath)
err = expl.AddHostAndDatabase(ctx,
expl.ConfFilePath(filePath),
expl.DBUser(user),
expl.DBPass(pass),
expl.DBHost(host),
expl.DBDatabase(database),
expl.DBPort(port),
expl.DBProtocol(protocol),
)

fmt.Printf("conf %s --host %s --database %s --user %s --pass %s --port %d --protocol %s --conf %s\n",
action, host, database, user, pass, port, protocol, filePath,
)
case "rm":

err = expl.RemoveHostAndDatabase(ctx, user, pass, host, database, filePath)
fmt.Printf("conf %s %s %s %s %s %d %s %s\n", action, host, database, user, pass, port, protocol, filePath)
err = expl.RemoveHostAndDatabase(ctx,
expl.ConfFilePath(filePath),
expl.DBUser(user),
expl.DBPass(pass),
expl.DBHost(host),
expl.DBDatabase(database),
expl.DBPort(port),
expl.DBProtocol(protocol),
)

fmt.Printf("conf %s --host %s --database %s --user %s --pass %s --port %d --protocol %s --conf %s\n",
action, host, database, user, pass, port, protocol, filePath,
)
case "mapping":
err = expl.ReloadAllTableInfo(ctx, filePath)
fmt.Printf("conf %s\n", action)
Expand Down
178 changes: 129 additions & 49 deletions conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,57 +31,87 @@ type database struct {
Tables []string `yaml:"tables"`
}

type param struct {
Address string
User string
Password string
Database string
Port int
Protocol string
ConfFilePath string
}

var dbInfo *model.DBInfo

// setConfig write config
func setConfig(ctx context.Context, conf *config, filePath string) error {
type paramFunc func(pm *param) *param

filePath, err := getPath(filePath)
if err != nil {
return err
func DBUser(user string) paramFunc {
return func(pm *param) *param {
pm.User = user
return pm
}
}

buf, err := yaml.Marshal(conf)
if err != nil {
return ErrWrap(err, UserInputError)
func DBPass(pass string) paramFunc {
return func(pm *param) *param {
pm.Password = pass
return pm
}
}

err = ioutil.WriteFile(filePath, buf, os.ModePerm)
if err != nil {
return ErrWrap(err, UserInputError)
func DBHost(address string) paramFunc {
return func(pm *param) *param {
pm.Address = address
return pm
}

return nil
}

// getConfig get config
func getConfig(ctx context.Context, filePath string) (*config, error) {
// 外部からconfの中身を参照できるようにする
var c config
func DBDatabase(database string) paramFunc {
return func(pm *param) *param {
pm.Database = database
return pm
}
}

filePath, err := getPath(filePath)
if err != nil {
return nil, err
func DBPort(port int) paramFunc {
return func(pm *param) *param {
pm.Port = port
return pm
}
}

buf, err := ioutil.ReadFile(filePath)
if err != nil {
return nil, ErrWrap(err, UserInputError)
func DBProtocol(protocol string) paramFunc {
return func(pm *param) *param {
pm.Protocol = protocol
return pm
}
}

err = yaml.Unmarshal(buf, &c)
if err != nil {
return nil, ErrWrap(err, UserInputError)
func ConfFilePath(path string) paramFunc {
return func(pm *param) *param {
pm.ConfFilePath = path
return pm
}
}

return &c, nil
func getParam(pmfs ...paramFunc) *param {
pm := &param{
Address: "localhost",
Port: 3306,
Protocol: "tcp",
}
for _, pmf := range pmfs {
pm = pmf(pm)
}
return pm
}

func AddHostAndDatabase(ctx context.Context, user, pass, address, dbName string, port int, protocol, filePath string) error {
func AddHostAndDatabase(ctx context.Context, pmfs ...paramFunc) error {
pm := getParam(pmfs...)

conf := new(config)
if _, err := os.Stat(filePath); err == nil {
conf, err = getConfig(ctx, filePath)
if _, err := os.Stat(pm.ConfFilePath); err == nil {
conf, err = getConfig(ctx, pm.ConfFilePath)
if err != nil {
return err
}
Expand All @@ -98,7 +128,11 @@ func AddHostAndDatabase(ctx context.Context, user, pass, address, dbName string,
// add Host Info
var ho *host
for _, h := range conf.Hosts {
if h.User == user && h.Password == pass && h.Address == address {
if h.User == pm.User &&
h.Password == pm.Password &&
h.Address == pm.Address &&
h.Port == pm.Port &&
h.Protocol == pm.Protocol {
ho = h
break
}
Expand All @@ -107,11 +141,11 @@ func AddHostAndDatabase(ctx context.Context, user, pass, address, dbName string,
if ho == nil {
ho = &host{
Key: len(conf.Hosts) + 1,
User: user,
Password: pass,
Address: address,
Port: port,
Protocol: protocol,
User: pm.User,
Password: pm.Password,
Address: pm.Address,
Port: pm.Port,
Protocol: pm.Protocol,
}

conf.Hosts = append(conf.Hosts, ho)
Expand All @@ -123,7 +157,7 @@ func AddHostAndDatabase(ctx context.Context, user, pass, address, dbName string,

// add Database Info
for _, d := range conf.Databases {
if d.HostKey == hostKey && d.Name == dbName {
if d.HostKey == hostKey && d.Name == pm.Database {
db = d
break
}
Expand All @@ -132,34 +166,38 @@ func AddHostAndDatabase(ctx context.Context, user, pass, address, dbName string,
if db == nil {
db = &database{
HostKey: hostKey,
Name: dbName,
Name: pm.Database,
}

conf.Databases = append(conf.Databases, db)
}

return setConfig(ctx, conf, filePath)
return setConfig(ctx, conf, pm.ConfFilePath)
}

func RemoveHostAndDatabase(ctx context.Context, user, pass, address, dbName, filePath string) error {
func RemoveHostAndDatabase(ctx context.Context, pmfs ...paramFunc) error {
pm := getParam(pmfs...)

conf, err := getConfig(ctx, filePath)
conf, err := getConfig(ctx, pm.ConfFilePath)
if err != nil {
return err
}

// add Host Info
var ho *host
for _, h := range conf.Hosts {
if h.User == user && h.Password == pass && h.Address == address {
if h.User == pm.User &&
h.Password == pm.Password &&
h.Address == pm.Address &&
h.Port == pm.Port &&
h.Protocol == pm.Protocol {
ho = h
break
}
}

if ho == nil {
return ErrWrap(
fmt.Errorf("none data user:%s, pass:%s, address:%s", user, pass, address),
fmt.Errorf("none data parameter:%#v", pm),
UserInputError,
)
}
Expand All @@ -172,7 +210,7 @@ func RemoveHostAndDatabase(ctx context.Context, user, pass, address, dbName, fil

// add Database Info
for _, d := range conf.Databases {
if d.HostKey == hostKey && d.Name == dbName {
if d.HostKey == hostKey && d.Name == pm.Database {
db = d
continue
}
Expand All @@ -181,15 +219,13 @@ func RemoveHostAndDatabase(ctx context.Context, user, pass, address, dbName, fil

if db == nil {
return ErrWrap(
fmt.Errorf("none data user:%s, pass:%s, address:%s, database:%s",
user, pass, address, dbName,
),
fmt.Errorf("none database data parameter:%#v", pm),
UserInputError,
)
}
conf.Databases = dbs

return setConfig(ctx, conf, filePath)
return setConfig(ctx, conf, pm.ConfFilePath)
}

func ReloadAllTableInfo(ctx context.Context, filePath string) error {
Expand Down Expand Up @@ -294,3 +330,47 @@ func GetTableDBMap(ctx context.Context) model.TableDBMap {

return model.TableDBMap(tbMap)
}

// setConfig write config
func setConfig(ctx context.Context, conf *config, filePath string) error {

filePath, err := getPath(filePath)
if err != nil {
return err
}

buf, err := yaml.Marshal(conf)
if err != nil {
return ErrWrap(err, UserInputError)
}

err = ioutil.WriteFile(filePath, buf, os.ModePerm)
if err != nil {
return ErrWrap(err, UserInputError)
}

return nil
}

// getConfig get config
func getConfig(ctx context.Context, filePath string) (*config, error) {
// 外部からconfの中身を参照できるようにする
var c config

filePath, err := getPath(filePath)
if err != nil {
return nil, err
}

buf, err := ioutil.ReadFile(filePath)
if err != nil {
return nil, ErrWrap(err, UserInputError)
}

err = yaml.Unmarshal(buf, &c)
if err != nil {
return nil, ErrWrap(err, UserInputError)
}

return &c, nil
}

0 comments on commit 8b17a46

Please sign in to comment.