sqlboiler/cmds/config.go

52 lines
1.3 KiB
Go
Raw Normal View History

package cmds
import (
"fmt"
"os"
"github.com/BurntSushi/toml"
)
2016-03-21 04:52:36 +01:00
type PostgresCfg struct {
User string `toml:"user"`
Pass string `toml:"pass"`
Host string `toml:"host"`
Port int `toml:"port"`
DBName string `toml:"dbname"`
}
2016-03-21 04:52:36 +01:00
type Config struct {
Postgres PostgresCfg `toml:"postgres"`
TestPostgres *PostgresCfg `toml:"postgres_test"`
}
2016-03-21 04:52:36 +01:00
var cfg *Config
2016-03-21 04:52:36 +01:00
// LoadConfigFile loads the toml config file into the cfg object
func LoadConfigFile(filename string) {
_, err := toml.DecodeFile(filename, &cfg)
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)
}
2016-03-21 04:52:36 +01:00
// 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.
2016-03-21 04:52:36 +01:00
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
}
}
}