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)
|
2017-11-06 18:53:43 +00:00
|
|
|
absolutePath := filepath.Join(ProjectRoot(), "pkg", "config", "environments", filenameWithExtension)
|
2017-11-02 22:37:27 +00:00
|
|
|
config := parseConfigFile(absolutePath)
|
2017-11-07 15:39:44 +00:00
|
|
|
if !filepath.IsAbs(config.Client.IPCPath) {
|
|
|
|
config.Client.IPCPath = filepath.Join(ProjectRoot(), config.Client.IPCPath)
|
|
|
|
}
|
2017-11-02 22:37:27 +00:00
|
|
|
return config
|
|
|
|
}
|
|
|
|
|
|
|
|
func ProjectRoot() string {
|
|
|
|
var _, filename, _, _ = runtime.Caller(0)
|
2017-11-06 18:53:43 +00:00
|
|
|
return path.Join(path.Dir(filename), "..", "..")
|
2017-11-02 22:37:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|