statping/types/configs/load.go

76 lines
1.9 KiB
Go
Raw Normal View History

2020-03-04 10:29:00 +00:00
package configs
import (
"github.com/statping/statping/types/errors"
2020-03-09 18:17:55 +00:00
"github.com/statping/statping/utils"
2020-04-24 02:26:09 +00:00
"gopkg.in/yaml.v2"
2020-04-24 03:48:21 +00:00
"os"
2020-03-04 10:29:00 +00:00
)
2020-04-16 17:32:54 +00:00
func LoadConfigFile(directory string) (*DbConfig, error) {
2020-04-16 09:57:00 +00:00
p := utils.Params
2020-04-16 17:32:54 +00:00
log.Infof("Attempting to read config file at: %s/config.yml ", directory)
2020-04-24 03:48:21 +00:00
p.SetConfigFile(directory + "/config.yml")
p.SetConfigType("yaml")
p.ReadInConfig()
2020-04-22 05:24:50 +00:00
2020-04-24 03:48:21 +00:00
db := new(DbConfig)
content, err := utils.OpenFile(directory + "/config.yml")
if err == nil {
if err := yaml.Unmarshal([]byte(content), &db); err != nil {
return nil, err
}
2020-04-22 05:24:50 +00:00
}
2020-04-16 17:32:54 +00:00
2020-04-24 03:48:21 +00:00
if os.Getenv("DB_CONN") == "sqlite" || os.Getenv("DB_CONN") == "sqlite3" {
db.DbConn = "sqlite3"
2020-04-24 02:26:09 +00:00
}
if db.DbConn != "" {
p.Set("DB_CONN", db.DbConn)
}
if db.DbHost != "" {
p.Set("DB_HOST", db.DbHost)
}
if db.DbPort != 0 {
p.Set("DB_PORT", db.DbPort)
}
if db.DbPass != "" {
2020-04-24 15:11:18 +00:00
p.Set("DB_PASS", db.DbPass)
2020-04-24 02:26:09 +00:00
}
if db.DbUser != "" {
p.Set("DB_USER", db.DbUser)
}
if db.DbData != "" {
p.Set("DB_DATABASE", db.DbData)
}
2020-04-24 03:48:21 +00:00
if db.Location != "" {
p.Set("LOCATION", db.Location)
}
if db.ApiSecret != "" {
p.Set("API_SECRET", db.ApiSecret)
}
2020-04-24 02:26:09 +00:00
2020-04-16 17:32:54 +00:00
configs := &DbConfig{
2020-04-16 09:57:00 +00:00
DbConn: p.GetString("DB_CONN"),
DbHost: p.GetString("DB_HOST"),
DbUser: p.GetString("DB_USER"),
DbPass: p.GetString("DB_PASS"),
DbData: p.GetString("DB_DATABASE"),
DbPort: p.GetInt("DB_PORT"),
Project: p.GetString("NAME"),
Description: p.GetString("DESCRIPTION"),
Domain: p.GetString("DOMAIN"),
Email: p.GetString("EMAIL"),
Username: p.GetString("ADMIN_USER"),
Password: p.GetString("ADMIN_PASS"),
2020-03-04 10:29:00 +00:00
Location: utils.Directory,
2020-04-16 09:57:00 +00:00
SqlFile: p.GetString("SQL_FILE"),
2020-03-04 10:29:00 +00:00
}
2020-04-20 15:17:22 +00:00
log.WithFields(utils.ToFields(configs)).Debugln("read config file: " + directory + "/config.yml")
2020-04-16 17:32:54 +00:00
2020-04-20 15:17:22 +00:00
if configs.DbConn == "" {
return configs, errors.New("Starting in setup mode")
}
2020-04-16 17:32:54 +00:00
return configs, nil
2020-03-04 10:29:00 +00:00
}