Cleaned up config

This commit is contained in:
Patrick O'brien 2016-03-21 13:52:36 +10:00
parent af1201e86e
commit e62b411547
2 changed files with 38 additions and 75 deletions

View file

@ -7,48 +7,24 @@ import (
"github.com/BurntSushi/toml"
)
// cfg holds the configuration file data from config.toml
var cfg = struct {
Postgres struct {
User string
Pass string
Host string
Port int
DBName string
}
}{}
type PostgresCfg struct {
User string `toml:"user"`
Pass string `toml:"pass"`
Host string `toml:"host"`
Port int `toml:"port"`
DBName string `toml:"dbname"`
}
// testCfg holds the test configuration file data from config.toml
var testCfg = struct {
Found bool
Postgres struct {
// Test details for template test file generation
TestUser string
TestPass string
TestHost string
TestPort int
TestDBName string
}
}{}
type Config struct {
Postgres PostgresCfg `toml:"postgres"`
TestPostgres *PostgresCfg `toml:"postgres_test"`
}
var cfg *Config
// LoadConfigFile loads the toml config file into the cfg object
func LoadConfigFile(filename string) {
var tmpCfg = struct {
Postgres struct {
User string `toml:"user"`
Pass string `toml:"pass"`
Host string `toml:"host"`
Port int `toml:"port"`
DBName string `toml:"dbname"`
// Test details for template test file generation
TestUser string `toml:"test_user"`
TestPass string `toml:"test_pass"`
TestHost string `toml:"test_host"`
TestPort int `toml:"test_port"`
TestDBName string `toml:"test_dbname"`
} `toml:"postgres"`
}{}
_, err := toml.DecodeFile(filename, &tmpCfg)
_, err := toml.DecodeFile(filename, &cfg)
if os.IsNotExist(err) {
fmt.Printf("Failed to find the toml configuration file %s: %s", filename, err)
@ -59,31 +35,17 @@ func LoadConfigFile(filename string) {
fmt.Println("Failed to decode toml configuration file:", err)
}
cfg.Postgres.User = tmpCfg.Postgres.User
cfg.Postgres.Pass = tmpCfg.Postgres.Pass
cfg.Postgres.Host = tmpCfg.Postgres.Host
cfg.Postgres.Port = tmpCfg.Postgres.Port
cfg.Postgres.DBName = tmpCfg.Postgres.DBName
testCfg.Postgres.TestUser = tmpCfg.Postgres.TestUser
testCfg.Postgres.TestPass = tmpCfg.Postgres.TestPass
testCfg.Postgres.TestHost = tmpCfg.Postgres.TestHost
testCfg.Postgres.TestPort = tmpCfg.Postgres.TestPort
testCfg.Postgres.TestDBName = tmpCfg.Postgres.TestDBName
// If all test cfg variables are present set found flag to true
if testCfg.Postgres.TestUser != "" && testCfg.Postgres.TestPass != "" &&
testCfg.Postgres.TestHost != "" && testCfg.Postgres.TestPort != 0 &&
testCfg.Postgres.TestDBName != "" {
testCfg.Found = true
}
// If any of the test cfg variables are not present then test TestPostgres to nil
//
// As a safety precaution, set found to false if
// the dbname is the same as the cfg dbname. This will prevent the test
// from erasing the production database tables if someone accidently
// configures the config.toml incorrectly.
if testCfg.Postgres.TestDBName == cfg.Postgres.DBName {
testCfg.Found = false
testCfg.Postgres.TestDBName = ""
if cfg.TestPostgres != nil {
if cfg.TestPostgres.User == "" || cfg.TestPostgres.Pass == "" ||
cfg.TestPostgres.Host == "" || cfg.TestPostgres.Port == 0 ||
cfg.TestPostgres.DBName == "" || cfg.Postgres.DBName == cfg.TestPostgres.DBName {
cfg.TestPostgres = nil
}
}
}

View file

@ -12,7 +12,7 @@ func TestLoadConfig(t *testing.T) {
file, _ := ioutil.TempFile(os.TempDir(), "sqlboilercfgtest")
defer os.Remove(file.Name())
if cfg.Postgres.DBName != "" || testCfg.Postgres.TestDBName != "" {
if cfg != nil {
t.Errorf("Expected cfgs to be empty for the time being.")
}
@ -26,34 +26,35 @@ func TestLoadConfig(t *testing.T) {
file.WriteString(fContents)
LoadConfigFile(file.Name())
if cfg.Postgres.Host != "localhost" || cfg.Postgres.Port != 5432 ||
if cfg.TestPostgres != nil || cfg.Postgres.Host != "localhost" ||
cfg.Postgres.User != "user" || cfg.Postgres.Pass != "pass" ||
cfg.Postgres.DBName != "mydb" || testCfg.Found == true {
cfg.Postgres.DBName != "mydb" || cfg.Postgres.Port != 5432 {
t.Errorf("Config failed to load properly, got: %#v", cfg.Postgres)
}
fContents = `
test_host="localhost"
test_port=5432
test_user="testuser"
test_pass="testpass"`
[postgres_test]
host="localhost"
port=5432
user="testuser"
pass="testpass"`
file.WriteString(fContents)
LoadConfigFile(file.Name())
if testCfg.Postgres.TestHost != "localhost" || testCfg.Postgres.TestPort != 5432 ||
testCfg.Postgres.TestUser != "testuser" || testCfg.Postgres.TestPass != "testpass" ||
testCfg.Postgres.TestDBName != "" || testCfg.Found == true {
t.Errorf("Test config failed to load properly, got: %#v", testCfg.Postgres)
if cfg.TestPostgres != nil {
t.Errorf("Test config failed to load properly, got: %#v", cfg.Postgres)
}
fContents = `
test_dbname="testmydb"`
dbname="testmydb"`
file.WriteString(fContents)
LoadConfigFile(file.Name())
if testCfg.Postgres.TestDBName != "testmydb" || testCfg.Found != true {
t.Errorf("Test config failed to load properly, got: %#v", testCfg.Postgres)
if cfg.TestPostgres.DBName != "testmydb" || cfg.TestPostgres.Host != "localhost" ||
cfg.TestPostgres.User != "testuser" || cfg.TestPostgres.Pass != "testpass" ||
cfg.TestPostgres.Port != 5432 {
t.Errorf("Test config failed to load properly, got: %#v", cfg.Postgres)
}
}