Update to use TOML config

This commit is contained in:
Matt Krump
2017-11-03 11:53:31 -05:00
committed by Eric Meyer
parent 0262a99321
commit e9efc2ff82
10 changed files with 201 additions and 60 deletions
+5
View File
@@ -0,0 +1,5 @@
package config
type Client struct {
IPCPath string
}
+41
View File
@@ -1,5 +1,46 @@
package config
import (
"log"
"os"
"fmt"
"path/filepath"
"path"
"runtime"
"github.com/BurntSushi/toml"
)
type Config struct {
Database Database
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
}
+13
View File
@@ -0,0 +1,13 @@
package config_test
import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"testing"
)
func TestConfig(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Config Suite")
}
+23
View File
@@ -0,0 +1,23 @@
package config_test
import (
"path/filepath"
"github.com/8thlight/vulcanizedb/config"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("Loading the config", func() {
It("reads the private config using the environment", func() {
privateConfig := config.NewConfig("private")
Expect(privateConfig.Database.Hostname).To(Equal("localhost"))
Expect(privateConfig.Database.Name).To(Equal("vulcanize_private"))
Expect(privateConfig.Database.Port).To(Equal(5432))
expandedPath := filepath.Join(config.ProjectRoot(), "test_data_dir/geth.ipc")
Expect(privateConfig.Client.IPCPath).To(Equal(expandedPath))
})
})
+7
View File
@@ -0,0 +1,7 @@
[database]
name = "vulcanize_private"
hostname = "localhost"
port = 5432
[client]
ipcPath = "test_data_dir/geth.ipc"
-11
View File
@@ -1,11 +0,0 @@
package config
func Private() Config {
return Config{
Database: Database{
Name: "vulcanize_private",
Hostname: "localhost",
Port: 5432,
},
}
}
-11
View File
@@ -1,11 +0,0 @@
package config
func Public() Config {
return Config{
Database: Database{
Name: "vulcanize_public",
Hostname: "localhost",
Port: 5432,
},
}
}