Add MySQL configuration.

This commit is contained in:
Aaron L 2016-09-08 23:04:58 -07:00
parent aadcf63e52
commit 81148d4beb
3 changed files with 74 additions and 3 deletions

View file

@ -16,6 +16,7 @@ type Config struct {
NoAutoTimestamps bool
Postgres PostgresConfig
MySQL MySQLConfig
}
// PostgresConfig configures a postgres database
@ -27,3 +28,13 @@ type PostgresConfig struct {
DBName string
SSLMode string
}
// MySQLConfig configures a mysql database
type MySQLConfig struct {
User string
Pass string
Host string
Port int
DBName string
SSLMode string
}

57
main.go
View file

@ -74,6 +74,9 @@ func main() {
viper.SetDefault("postgres.sslmode", "require")
viper.SetDefault("postgres.port", "5432")
viper.SetDefault("mysql.sslmode", "true")
viper.SetDefault("mysql.port", "3306")
viper.BindPFlags(rootCmd.PersistentFlags())
viper.AutomaticEnv()
@ -155,10 +158,17 @@ func preRun(cmd *cobra.Command, args []string) error {
SSLMode: viper.GetString("postgres.sslmode"),
}
// Set the default SSLMode value
// BUG: https://github.com/spf13/viper/issues/71
// Despite setting defaults, nested values don't get defaults
// Set them manually
if cmdConfig.Postgres.SSLMode == "" {
viper.Set("postgres.sslmode", "require")
cmdConfig.Postgres.SSLMode = viper.GetString("postgres.sslmode")
cmdConfig.Postgres.SSLMode = "require"
viper.Set("postgres.sslmode", cmdConfig.Postgres.SSLMode)
}
if cmdConfig.Postgres.Port == 0 {
cmdConfig.Postgres.Port = 5432
viper.Set("postgres.port", cmdConfig.Postgres.Port)
}
err = vala.BeginValidation().Validate(
@ -176,6 +186,47 @@ func preRun(cmd *cobra.Command, args []string) error {
return errors.New("postgres driver requires a postgres section in your config file")
}
if viper.IsSet("mysql.dbname") {
cmdConfig.MySQL = MySQLConfig{
User: viper.GetString("mysql.user"),
Pass: viper.GetString("mysql.pass"),
Host: viper.GetString("mysql.host"),
Port: viper.GetInt("mysql.port"),
DBName: viper.GetString("mysql.dbname"),
SSLMode: viper.GetString("mysql.sslmode"),
}
// MySQL doesn't have schemas, just databases
cmdConfig.Schema = cmdConfig.MySQL.DBName
// BUG: https://github.com/spf13/viper/issues/71
// Despite setting defaults, nested values don't get defaults
// Set them manually
if cmdConfig.MySQL.SSLMode == "" {
cmdConfig.MySQL.SSLMode = "true"
viper.Set("mysql.sslmode", cmdConfig.MySQL.SSLMode)
}
if cmdConfig.MySQL.Port == 0 {
cmdConfig.MySQL.Port = 3306
viper.Set("mysql.port", cmdConfig.MySQL.Port)
}
err = vala.BeginValidation().Validate(
vala.StringNotEmpty(cmdConfig.MySQL.User, "mysql.user"),
vala.StringNotEmpty(cmdConfig.MySQL.Host, "mysql.host"),
vala.Not(vala.Equals(cmdConfig.MySQL.Port, 0, "mysql.port")),
vala.StringNotEmpty(cmdConfig.MySQL.DBName, "mysql.dbname"),
vala.StringNotEmpty(cmdConfig.MySQL.SSLMode, "mysql.sslmode"),
).Check()
if err != nil {
return commandFailure(err.Error())
}
} else if driverName == "mysql" {
return errors.New("mysql driver requires a mysql section in your config file")
}
cmdState, err = New(cmdConfig)
return err
}

View file

@ -229,6 +229,15 @@ func (s *State) initDriver(driverName string) error {
s.Config.Postgres.Port,
s.Config.Postgres.SSLMode,
)
case "mysql":
s.Driver = drivers.NewMySQLDriver(
s.Config.MySQL.User,
s.Config.MySQL.Pass,
s.Config.MySQL.DBName,
s.Config.MySQL.Host,
s.Config.MySQL.Port,
s.Config.MySQL.SSLMode,
)
case "mock":
s.Driver = &drivers.MockDriver{}
}