Skip to content

Commit

Permalink
Merge pull request #32 from shogo82148/support-unix-domain-socket
Browse files Browse the repository at this point in the history
support unix domain socket
  • Loading branch information
shogo82148 committed Sep 2, 2021
2 parents dfbda8b + a5f62a3 commit 2c1dfe9
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
17 changes: 17 additions & 0 deletions cmd/schemalex-deploy/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

type config struct {
version bool
socket string
host string
user string
password string
Expand All @@ -26,6 +27,7 @@ type config struct {
func loadConfig() (*config, error) {
var cfn config
var version bool
var socket string
var host, username, password, database string
var port int
var approve bool
Expand All @@ -37,12 +39,18 @@ func loadConfig() (*config, error) {
schemalex -version
`, getVersion())
}

// options that are compatible with the mysql(1)
// https://dev.mysql.com/doc/refman/8.0/en/mysql-command-options.html
flag.StringVar(&socket, "socket", "", "the unix domain socket path for the database")
flag.StringVar(&host, "host", "", "the host name of the database")
flag.IntVar(&port, "port", 3306, "the port number")
flag.StringVar(&username, "user", "", "username")
flag.StringVar(&password, "password", "", "password")
flag.StringVar(&database, "database", "", "the database name")
flag.BoolVar(&version, "version", false, "show the version")

// for schemalex-deploy
flag.BoolVar(&approve, "auto-approve", false, "skips interactive approval of plan before deploying")
flag.Parse()

Expand All @@ -59,6 +67,9 @@ schemalex -version
return nil, err
}
if client, ok := cnfFile["client"]; ok {
if v, ok := client["socket"]; ok {
cfn.socket = v
}
if v, ok := client["host"]; ok {
cfn.host = v
}
Expand All @@ -78,6 +89,9 @@ schemalex -version

// load configure from the environment values
// https://dev.mysql.com/doc/refman/8.0/en/environment-variables.html
if v := os.Getenv("MYSQL_UNIX_PORT"); v != "" {
cfn.socket = v
}
if v := os.Getenv("MYSQL_HOST"); v != "" {
cfn.host = v
}
Expand All @@ -101,6 +115,9 @@ schemalex -version
}
}

if socket != "" {
cfn.socket = socket
}
if host != "" {
cfn.host = host
}
Expand Down
8 changes: 7 additions & 1 deletion cmd/schemalex-deploy/schemalex-deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,13 @@ func _main() error {
defer stop()

config := mysql.NewConfig()
config.Addr = net.JoinHostPort(cfn.host, strconv.Itoa(cfn.port))
if cfn.socket != "" {
config.Net = "unix"
config.Addr = cfn.socket
} else {
config.Net = "tcp"
config.Addr = net.JoinHostPort(cfn.host, strconv.Itoa(cfn.port))
}
config.User = cfn.user
config.Passwd = cfn.password
config.DBName = cfn.database
Expand Down

0 comments on commit 2c1dfe9

Please sign in to comment.