2017-11-01 15:17:01 +00:00
|
|
|
package config
|
|
|
|
|
2017-11-02 22:37:27 +00:00
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"path"
|
|
|
|
"runtime"
|
|
|
|
|
|
|
|
"github.com/BurntSushi/toml"
|
|
|
|
)
|
|
|
|
|
2017-11-01 15:17:01 +00:00
|
|
|
type Config struct {
|
|
|
|
Database Database
|
2017-11-02 22:37:27 +00:00
|
|
|
Client Client
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewConfig(environment string) Config {
|
|
|
|
filenameWithExtension := fmt.Sprintf("%s.toml", environment)
|
|
|
|
absolutePath := filepath.Join(ProjectRoot(), "config", "environments", filenameWithExtension)
|
|
|
|
config := parseConfigFile(absolutePath)
|
|
|
|
config.Client.IPCPath = filepath.Join(ProjectRoot(), config.Client.IPCPath)
|
|
|
|
return config
|
|
|
|
}
|
|
|
|
|
|
|
|
func ProjectRoot() string {
|
|
|
|
var _, filename, _, _ = runtime.Caller(0)
|
|
|
|
return path.Join(path.Dir(filename), "../")
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseConfigFile(configfile string) Config {
|
|
|
|
var cfg Config
|
|
|
|
_, err := os.Stat(configfile)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Config file is missing: ", configfile)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := toml.DecodeFile(configfile, &cfg); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
return cfg
|
2017-11-01 15:17:01 +00:00
|
|
|
}
|