Extract loading config to a shared file

This commit is contained in:
Eric Meyer 2017-11-09 13:34:58 -06:00
parent c0f737868b
commit 84205a21ea
4 changed files with 27 additions and 27 deletions

View File

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"github.com/8thlight/vulcanizedb/cmd"
"github.com/8thlight/vulcanizedb/pkg/config" "github.com/8thlight/vulcanizedb/pkg/config"
do "gopkg.in/godo.v2" do "gopkg.in/godo.v2"
) )
@ -17,14 +18,6 @@ func parseEnvironment(context *do.Context) string {
return environment return environment
} }
func loadConfig(environment string) config.Config {
cfg, err := config.NewConfig(environment)
if err != nil {
log.Fatalf("Error loading config\n%v", err)
}
return *cfg
}
func tasks(p *do.Project) { func tasks(p *do.Project) {
p.Task("run", nil, func(context *do.Context) { p.Task("run", nil, func(context *do.Context) {
@ -45,7 +38,7 @@ func tasks(p *do.Project) {
p.Task("migrate", nil, func(context *do.Context) { p.Task("migrate", nil, func(context *do.Context) {
environment := parseEnvironment(context) environment := parseEnvironment(context)
cfg := loadConfig(environment) cfg := cmd.LoadConfig(environment)
connectString := config.DbConnectionString(cfg.Database) connectString := config.DbConnectionString(cfg.Database)
migrate := fmt.Sprintf("migrate -database '%s' -path ./db/migrations up", connectString) migrate := fmt.Sprintf("migrate -database '%s' -path ./db/migrations up", connectString)
dumpSchema := fmt.Sprintf("pg_dump -O -s %s > ./db/schema.sql", cfg.Database.Name) dumpSchema := fmt.Sprintf("pg_dump -O -s %s > ./db/schema.sql", cfg.Database.Name)
@ -55,7 +48,7 @@ func tasks(p *do.Project) {
p.Task("rollback", nil, func(context *do.Context) { p.Task("rollback", nil, func(context *do.Context) {
environment := parseEnvironment(context) environment := parseEnvironment(context)
cfg := loadConfig(environment) cfg := cmd.LoadConfig(environment)
connectString := config.DbConnectionString(cfg.Database) connectString := config.DbConnectionString(cfg.Database)
migrate := fmt.Sprintf("migrate -database '%s' -path ./db/migrations down 1", connectString) migrate := fmt.Sprintf("migrate -database '%s' -path ./db/migrations down 1", connectString)
dumpSchema := fmt.Sprintf("pg_dump -O -s %s > ./db/schema.sql", cfg.Database.Name) dumpSchema := fmt.Sprintf("pg_dump -O -s %s > ./db/schema.sql", cfg.Database.Name)

View File

@ -2,11 +2,10 @@ package main
import ( import (
"flag" "flag"
"log"
"fmt" "fmt"
"github.com/8thlight/vulcanizedb/pkg/config" "github.com/8thlight/vulcanizedb/cmd"
"github.com/8thlight/vulcanizedb/pkg/geth" "github.com/8thlight/vulcanizedb/pkg/geth"
"github.com/8thlight/vulcanizedb/pkg/history" "github.com/8thlight/vulcanizedb/pkg/history"
"github.com/8thlight/vulcanizedb/pkg/repositories" "github.com/8thlight/vulcanizedb/pkg/repositories"
@ -16,13 +15,10 @@ func main() {
environment := flag.String("environment", "", "Environment name") environment := flag.String("environment", "", "Environment name")
startingBlockNumber := flag.Int("starting-number", -1, "First block to fill from") startingBlockNumber := flag.Int("starting-number", -1, "First block to fill from")
flag.Parse() flag.Parse()
cfg, err := config.NewConfig(*environment) config := cmd.LoadConfig(*environment)
if err != nil {
log.Fatalf("Error loading config\n%v", err)
}
blockchain := geth.NewGethBlockchain(cfg.Client.IPCPath) blockchain := geth.NewGethBlockchain(config.Client.IPCPath)
repository := repositories.NewPostgres(cfg.Database) repository := repositories.NewPostgres(config.Database)
numberOfBlocksCreated := history.PopulateBlocks(blockchain, repository, int64(*startingBlockNumber)) numberOfBlocksCreated := history.PopulateBlocks(blockchain, repository, int64(*startingBlockNumber))
fmt.Printf("Populated %d blocks", numberOfBlocksCreated) fmt.Printf("Populated %d blocks", numberOfBlocksCreated)
} }

View File

@ -2,12 +2,11 @@ package main
import ( import (
"fmt" "fmt"
"log"
"flag" "flag"
"github.com/8thlight/vulcanizedb/cmd"
"github.com/8thlight/vulcanizedb/pkg/blockchain_listener" "github.com/8thlight/vulcanizedb/pkg/blockchain_listener"
"github.com/8thlight/vulcanizedb/pkg/config"
"github.com/8thlight/vulcanizedb/pkg/core" "github.com/8thlight/vulcanizedb/pkg/core"
"github.com/8thlight/vulcanizedb/pkg/geth" "github.com/8thlight/vulcanizedb/pkg/geth"
"github.com/8thlight/vulcanizedb/pkg/observers" "github.com/8thlight/vulcanizedb/pkg/observers"
@ -17,15 +16,12 @@ import (
func main() { func main() {
environment := flag.String("environment", "", "Environment name") environment := flag.String("environment", "", "Environment name")
flag.Parse() flag.Parse()
cfg, err := config.NewConfig(*environment) config := cmd.LoadConfig(*environment)
if err != nil {
log.Fatalf("Error loading config\n%v", err)
}
fmt.Println("Client Path ", cfg.Client.IPCPath) fmt.Println("Client Path ", config.Client.IPCPath)
blockchain := geth.NewGethBlockchain(cfg.Client.IPCPath) blockchain := geth.NewGethBlockchain(config.Client.IPCPath)
loggingObserver := observers.BlockchainLoggingObserver{} loggingObserver := observers.BlockchainLoggingObserver{}
repository := repositories.NewPostgres(cfg.Database) repository := repositories.NewPostgres(config.Database)
dbObserver := observers.NewBlockchainDbObserver(repository) dbObserver := observers.NewBlockchainDbObserver(repository)
listener := blockchain_listener.NewBlockchainListener(blockchain, []core.BlockchainObserver{ listener := blockchain_listener.NewBlockchainListener(blockchain, []core.BlockchainObserver{
loggingObserver, loggingObserver,

15
cmd/utils.go Normal file
View File

@ -0,0 +1,15 @@
package cmd
import (
"log"
"github.com/8thlight/vulcanizedb/pkg/config"
)
func LoadConfig(environment string) config.Config {
cfg, err := config.NewConfig(environment)
if err != nil {
log.Fatalf("Error loading config\n%v", err)
}
return *cfg
}