-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
78 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,27 @@ | ||
package cons | ||
|
||
import ( | ||
"errors" | ||
"regexp" | ||
) | ||
|
||
const ( | ||
DbSchemeMySQL = "mysql" | ||
|
||
DsnInvalid = "dsn invalid" | ||
DsnInvalidNoSuitableResolver = "no suitable resolver" | ||
) | ||
|
||
var ( | ||
MySQL = newDatabaseType("mysql") | ||
) | ||
|
||
var ( | ||
Umami = newDatabase(MySQL, "umami") | ||
WalineBlog = newDatabase(MySQL, "waline_blog") | ||
|
||
// mysql://xxx:[email protected]:3306/xxx | ||
dsnReg = regexp.MustCompile(`([\w]+)://(.*):(.*)@(.*):([0-9]+)/([\w_]+)`) | ||
) | ||
|
||
type DatabaseType string | ||
|
@@ -23,3 +38,33 @@ type Database struct { | |
func newDatabase(dbType DatabaseType, name string) Database { | ||
return Database{dbType, name} | ||
} | ||
|
||
type BaseDbInfo struct { | ||
User string | ||
Password string | ||
Host string | ||
Port string | ||
} | ||
|
||
func ParseDbDsn(dsn string) (*BaseDbInfo, error) { | ||
if !dsnReg.MatchString(dsn) { | ||
return nil, errors.New("不含 dsn") | ||
} | ||
|
||
res := dsnReg.FindStringSubmatch(dsn) | ||
if len(res) < 5 { | ||
return nil, errors.New(DsnInvalid) | ||
} | ||
switch res[1] { | ||
case DbSchemeMySQL: | ||
return &BaseDbInfo{ | ||
User: res[2], | ||
Password: res[3], | ||
Host: res[4], | ||
Port: res[5], | ||
}, nil | ||
default: | ||
return nil, errors.New(DsnInvalidNoSuitableResolver) | ||
} | ||
return nil, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package cons | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func TestParseDbDsn(t *testing.T) { | ||
var dsn = "mysql://aaa:[email protected]:3306/xxx" | ||
info, err := ParseDbDsn(dsn) | ||
assert.Nil(t, err) | ||
assert.Equal(t, "aaa", info.User) | ||
assert.Equal(t, "bbb", info.Password) | ||
assert.Equal(t, "example.com", info.Host) | ||
assert.Equal(t, "3306", info.Port) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,9 @@ | ||
package db | ||
|
||
import ( | ||
"fmt" | ||
"github.com/alomerry/go-tools/static/cons" | ||
"testing" | ||
) | ||
|
||
func TestDumpDatabase(t *testing.T) { | ||
tool := NewDumpTool( | ||
MySQLDumpCmdParam("root", "alomerry.com", "13306", "e0t=ereFqvpibm}91Y:n"), | ||
SetDumpPath("/Users/alomerry/workspace/go-tools/output"), | ||
) | ||
fmt.Println(tool.DumpDbs(cons.WalineBlog)) | ||
} | ||
//func TestDumpDatabase(t *testing.T) { | ||
// tool := NewDumpTool( | ||
// MySQLDumpCmdParam("mysql://root:[email protected]:12345"), | ||
// SetDumpPath("/Users/alomerry/workspace/go-tools/output"), | ||
// ) | ||
// fmt.Println(tool.DumpDbs(cons.WalineBlog)) | ||
//} |