diff --git a/Gododir/main.go b/Gododir/main.go index b20b69be..edb5a530 100644 --- a/Gododir/main.go +++ b/Gododir/main.go @@ -5,6 +5,7 @@ import ( "fmt" + "github.com/8thlight/vulcanizedb/cmd" "github.com/8thlight/vulcanizedb/pkg/config" do "gopkg.in/godo.v2" ) @@ -17,14 +18,6 @@ func parseEnvironment(context *do.Context) string { 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) { 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) { environment := parseEnvironment(context) - cfg := loadConfig(environment) + cfg := cmd.LoadConfig(environment) connectString := config.DbConnectionString(cfg.Database) 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) @@ -55,7 +48,7 @@ func tasks(p *do.Project) { p.Task("rollback", nil, func(context *do.Context) { environment := parseEnvironment(context) - cfg := loadConfig(environment) + cfg := cmd.LoadConfig(environment) connectString := config.DbConnectionString(cfg.Database) 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) diff --git a/cmd/populate_blocks/main.go b/cmd/populate_blocks/main.go index 8d34d2ef..23c3d8da 100644 --- a/cmd/populate_blocks/main.go +++ b/cmd/populate_blocks/main.go @@ -2,11 +2,10 @@ package main import ( "flag" - "log" "fmt" - "github.com/8thlight/vulcanizedb/pkg/config" + "github.com/8thlight/vulcanizedb/cmd" "github.com/8thlight/vulcanizedb/pkg/geth" "github.com/8thlight/vulcanizedb/pkg/history" "github.com/8thlight/vulcanizedb/pkg/repositories" @@ -16,13 +15,10 @@ func main() { environment := flag.String("environment", "", "Environment name") startingBlockNumber := flag.Int("starting-number", -1, "First block to fill from") flag.Parse() - cfg, err := config.NewConfig(*environment) - if err != nil { - log.Fatalf("Error loading config\n%v", err) - } + config := cmd.LoadConfig(*environment) - blockchain := geth.NewGethBlockchain(cfg.Client.IPCPath) - repository := repositories.NewPostgres(cfg.Database) + blockchain := geth.NewGethBlockchain(config.Client.IPCPath) + repository := repositories.NewPostgres(config.Database) numberOfBlocksCreated := history.PopulateBlocks(blockchain, repository, int64(*startingBlockNumber)) fmt.Printf("Populated %d blocks", numberOfBlocksCreated) } diff --git a/cmd/run/main.go b/cmd/run/main.go index a0225e06..80e8a295 100644 --- a/cmd/run/main.go +++ b/cmd/run/main.go @@ -2,12 +2,11 @@ package main import ( "fmt" - "log" "flag" + "github.com/8thlight/vulcanizedb/cmd" "github.com/8thlight/vulcanizedb/pkg/blockchain_listener" - "github.com/8thlight/vulcanizedb/pkg/config" "github.com/8thlight/vulcanizedb/pkg/core" "github.com/8thlight/vulcanizedb/pkg/geth" "github.com/8thlight/vulcanizedb/pkg/observers" @@ -17,15 +16,12 @@ import ( func main() { environment := flag.String("environment", "", "Environment name") flag.Parse() - cfg, err := config.NewConfig(*environment) - if err != nil { - log.Fatalf("Error loading config\n%v", err) - } + config := cmd.LoadConfig(*environment) - fmt.Println("Client Path ", cfg.Client.IPCPath) - blockchain := geth.NewGethBlockchain(cfg.Client.IPCPath) + fmt.Println("Client Path ", config.Client.IPCPath) + blockchain := geth.NewGethBlockchain(config.Client.IPCPath) loggingObserver := observers.BlockchainLoggingObserver{} - repository := repositories.NewPostgres(cfg.Database) + repository := repositories.NewPostgres(config.Database) dbObserver := observers.NewBlockchainDbObserver(repository) listener := blockchain_listener.NewBlockchainListener(blockchain, []core.BlockchainObserver{ loggingObserver, diff --git a/cmd/utils.go b/cmd/utils.go new file mode 100644 index 00000000..0a7a0a0e --- /dev/null +++ b/cmd/utils.go @@ -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 +}