Moved config init to main func & add config_test
* Split cfg object into testCfg and cfg * Added tests for loading toml config from file
This commit is contained in:
parent
e69573f68e
commit
af1201e86e
3 changed files with 127 additions and 17 deletions
|
@ -10,32 +10,80 @@ import (
|
|||
// cfg holds the configuration file data from config.toml
|
||||
var cfg = 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 string `toml:"test_port"`
|
||||
TestDBName string `toml:"test_dbname"`
|
||||
} `toml:"postgres"`
|
||||
User string
|
||||
Pass string
|
||||
Host string
|
||||
Port int
|
||||
DBName string
|
||||
}
|
||||
}{}
|
||||
|
||||
// init reads the config.toml configuration file into the cfg variable
|
||||
func init() {
|
||||
_, err := toml.DecodeFile("config.toml", &cfg)
|
||||
if err == nil {
|
||||
return
|
||||
// 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
|
||||
}
|
||||
}{}
|
||||
|
||||
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)
|
||||
|
||||
if os.IsNotExist(err) {
|
||||
fmt.Printf("Failed to find the toml configuration file %s: %s", filename, err)
|
||||
return
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
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
|
||||
}
|
||||
|
||||
// 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 = ""
|
||||
}
|
||||
}
|
||||
|
|
59
cmds/config_test.go
Normal file
59
cmds/config_test.go
Normal file
|
@ -0,0 +1,59 @@
|
|||
package cmds
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestLoadConfig(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
file, _ := ioutil.TempFile(os.TempDir(), "sqlboilercfgtest")
|
||||
defer os.Remove(file.Name())
|
||||
|
||||
if cfg.Postgres.DBName != "" || testCfg.Postgres.TestDBName != "" {
|
||||
t.Errorf("Expected cfgs to be empty for the time being.")
|
||||
}
|
||||
|
||||
fContents := `[postgres]
|
||||
host="localhost"
|
||||
port=5432
|
||||
user="user"
|
||||
pass="pass"
|
||||
dbname="mydb"`
|
||||
|
||||
file.WriteString(fContents)
|
||||
LoadConfigFile(file.Name())
|
||||
|
||||
if cfg.Postgres.Host != "localhost" || cfg.Postgres.Port != 5432 ||
|
||||
cfg.Postgres.User != "user" || cfg.Postgres.Pass != "pass" ||
|
||||
cfg.Postgres.DBName != "mydb" || testCfg.Found == true {
|
||||
t.Errorf("Config failed to load properly, got: %#v", cfg.Postgres)
|
||||
}
|
||||
|
||||
fContents = `
|
||||
test_host="localhost"
|
||||
test_port=5432
|
||||
test_user="testuser"
|
||||
test_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)
|
||||
}
|
||||
|
||||
fContents = `
|
||||
test_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)
|
||||
}
|
||||
}
|
3
main.go
3
main.go
|
@ -13,6 +13,9 @@ import (
|
|||
)
|
||||
|
||||
func main() {
|
||||
// Load the config.toml file
|
||||
cmds.LoadConfigFile("config.toml")
|
||||
|
||||
// Execute SQLBoiler
|
||||
if err := cmds.SQLBoiler.Execute(); err != nil {
|
||||
fmt.Printf("Failed to execute SQLBoiler: %s", err)
|
||||
|
|
Loading…
Add table
Reference in a new issue