Repository: nuveo/prest
Pull request #139 (2017-05-10 22:53:09)

Prevents multiple startup when the pREST is used as a package.
Removes init functions that were started before the host system was able to load the settings.

codecov[bot]: # [Codecov](https://codecov.io/gh/nuveo/prest/pull/139?src=pr&el=h1) Report
> Merging [#139](https://codecov.io/gh/nuveo/prest/pull/139?src=pr&el=desc) into [master](https://codecov.io/gh/nuveo/prest/commit/c194003d2a1ac444980ea763278ba9821abe7140?src=pr&el=desc) will **increase** coverage by `0.16%`.
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/nuveo/prest/pull/139/graphs/tree.svg?height=150&width=650&token=eVD9urwIEv&src=pr)](https://codecov.io/gh/nuveo/prest/pull/139?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master     #139      +/-   ##
==========================================
+ Coverage   85.99%   86.16%   +0.16%     
==========================================
  Files          10       10              
  Lines         857      853       -4     
==========================================
- Hits          737      735       -2     
+ Misses         78       76       -2     
  Partials       42       42
```


| [Impacted Files](https://codecov.io/gh/nuveo/prest/pull/139?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [config/config.go](https://codecov.io/gh/nuveo/prest/pull/139?src=pr&el=tree#diff-Y29uZmlnL2NvbmZpZy5nbw==) | `76.92% <100%> (+1.92%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/nuveo/prest/pull/139?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/nuveo/prest/pull/139?src=pr&el=footer). Last update [c194003...d1a176e](https://codecov.io/gh/nuveo/prest/pull/139?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/cmd/create.go b/cmd/create.go
index b193372..ab73f83 100644
--- a/cmd/create.go
+++ b/cmd/create.go
@@ -32,7 +32,3 @@ var createCmd = &cobra.Command{
 		fmt.Println(migrationFile.DownFile.FileName)
 	},
 }
-
-func init() {
-	migrateCmd.AddCommand(createCmd)
-}
diff --git a/cmd/down.go b/cmd/down.go
index c663b09..3a6315d 100644
--- a/cmd/down.go
+++ b/cmd/down.go
@@ -26,8 +26,3 @@ var downCmd = &cobra.Command{
 		}
 	},
 }
-
-func init() {
-	// prest migrate down
-	migrateCmd.AddCommand(downCmd)
-}
diff --git a/cmd/goto.go b/cmd/goto.go
index addcdd1..fb77bcc 100644
--- a/cmd/goto.go
+++ b/cmd/goto.go
@@ -45,7 +45,3 @@ var gotoCmd = &cobra.Command{
 
 	},
 }
-
-func init() {
-	migrateCmd.AddCommand(gotoCmd)
-}
diff --git a/cmd/migrate.go b/cmd/migrate.go
index e478f5c..3e6e582 100644
--- a/cmd/migrate.go
+++ b/cmd/migrate.go
@@ -24,12 +24,6 @@ var migrateCmd = &cobra.Command{
 	Long:  `Execute migration operations`,
 }
 
-func init() {
-	RootCmd.AddCommand(migrateCmd)
-	migrateCmd.PersistentFlags().StringVar(&url, "url", driverURL(), "Database driver url")
-	migrateCmd.PersistentFlags().StringVar(&path, "path", config.PrestConf.MigrationsPath, "Migrations directory")
-}
-
 func driverURL() string {
 	return fmt.Sprintf("postgres://%s:%s@%s:%d/%s?sslmode=disable", config.PrestConf.PGUser, config.PrestConf.PGPass, config.PrestConf.PGHost, config.PrestConf.PGPort, config.PrestConf.PGDatabase)
 }
diff --git a/cmd/mversion.go b/cmd/mversion.go
index 2fbcfbf..11f841f 100644
--- a/cmd/mversion.go
+++ b/cmd/mversion.go
@@ -25,7 +25,3 @@ var mversionCmd = &cobra.Command{
 		fmt.Println(version)
 	},
 }
-
-func init() {
-	migrateCmd.AddCommand(mversionCmd)
-}
diff --git a/cmd/next.go b/cmd/next.go
index 8c3f5b9..ec7416f 100644
--- a/cmd/next.go
+++ b/cmd/next.go
@@ -35,7 +35,3 @@ var nextCmd = &cobra.Command{
 		}
 	},
 }
-
-func init() {
-	migrateCmd.AddCommand(nextCmd)
-}
diff --git a/cmd/redo.go b/cmd/redo.go
index 0c51472..a8b8635 100644
--- a/cmd/redo.go
+++ b/cmd/redo.go
@@ -27,7 +27,3 @@ var redoCmd = &cobra.Command{
 		}
 	},
 }
-
-func init() {
-	migrateCmd.AddCommand(redoCmd)
-}
diff --git a/cmd/reset.go b/cmd/reset.go
index dc97ca2..328dcd7 100644
--- a/cmd/reset.go
+++ b/cmd/reset.go
@@ -27,7 +27,3 @@ var resetCmd = &cobra.Command{
 		}
 	},
 }
-
-func init() {
-	migrateCmd.AddCommand(resetCmd)
-}
diff --git a/cmd/root.go b/cmd/root.go
index c4455a2..467b354 100644
--- a/cmd/root.go
+++ b/cmd/root.go
@@ -30,6 +30,20 @@ var RootCmd = &cobra.Command{
 // Execute adds all child commands to the root command sets flags appropriately.
 // This is called by main.main(). It only needs to happen once to the rootCmd.
 func Execute() {
+	config.Load()
+	migrateCmd.AddCommand(createCmd)
+	migrateCmd.AddCommand(downCmd)
+	migrateCmd.AddCommand(gotoCmd)
+	migrateCmd.AddCommand(mversionCmd)
+	migrateCmd.AddCommand(nextCmd)
+	migrateCmd.AddCommand(redoCmd)
+	migrateCmd.AddCommand(upCmd)
+	migrateCmd.AddCommand(resetCmd)
+	RootCmd.AddCommand(versionCmd)
+	RootCmd.AddCommand(migrateCmd)
+	migrateCmd.PersistentFlags().StringVar(&url, "url", driverURL(), "Database driver url")
+	migrateCmd.PersistentFlags().StringVar(&path, "path", config.PrestConf.MigrationsPath, "Migrations directory")
+
 	if err := RootCmd.Execute(); err != nil {
 		fmt.Println(err)
 		os.Exit(-1)
diff --git a/cmd/up.go b/cmd/up.go
index 20c5648..9f20e9e 100644
--- a/cmd/up.go
+++ b/cmd/up.go
@@ -26,8 +26,3 @@ var upCmd = &cobra.Command{
 		}
 	},
 }
-
-func init() {
-	// prest migrate up
-	migrateCmd.AddCommand(upCmd)
-}
diff --git a/cmd/version.go b/cmd/version.go
index c26ea5b..9593322 100644
--- a/cmd/version.go
+++ b/cmd/version.go
@@ -18,7 +18,3 @@ var versionCmd = &cobra.Command{
 		fmt.Println("Serve a RESTful API from any PostgreSQL database", helpers.PrestReleaseVersion())
 	},
 }
-
-func init() {
-	RootCmd.AddCommand(versionCmd)
-}
diff --git a/config/config.go b/config/config.go
index 3d91369..3fe427d 100644
--- a/config/config.go
+++ b/config/config.go
@@ -47,14 +47,6 @@ type Prest struct {
 // PrestConf config variable
 var PrestConf *Prest
 
-func init() {
-	load()
-}
-
-func LoadToTest() {
-	load()
-}
-
 func viperCfg() {
 	filePath := os.Getenv("PREST_CONF")
 	if filePath == "" {
@@ -113,8 +105,8 @@ func Parse(cfg *Prest) (err error) {
 	return
 }
 
-// load configuration
-func load() {
+// Load configuration
+func Load() {
 	viperCfg()
 	PrestConf = &Prest{}
 	Parse(PrestConf)


Test patch:
diff --git a/adapters/postgres/connection/conn_test.go b/adapters/postgres/connection/conn_test.go
index dcd25a7..371e26f 100644
--- a/adapters/postgres/connection/conn_test.go
+++ b/adapters/postgres/connection/conn_test.go
@@ -4,8 +4,13 @@ import (
 	"testing"
 
 	sqlmock "github.com/DATA-DOG/go-sqlmock"
+	config "github.com/nuveo/prest/config"
 )
 
+func init() {
+	config.Load()
+}
+
 func TestGet(t *testing.T) {
 	t.Log("Open connection")
 	db, err := Get()
diff --git a/adapters/postgres/queries_test.go b/adapters/postgres/queries_test.go
index ee6b884..e848419 100644
--- a/adapters/postgres/queries_test.go
+++ b/adapters/postgres/queries_test.go
@@ -12,6 +12,7 @@ import (
 )
 
 func TestMain(m *testing.M) {
+	config.Load()
 	createMockScripts(config.PrestConf.QueriesPath)
 	writeMockScripts(config.PrestConf.QueriesPath)
 
diff --git a/config/config_test.go b/config/config_test.go
index e40696e..afd0406 100644
--- a/config/config_test.go
+++ b/config/config_test.go
@@ -9,12 +9,12 @@ import (
 func TestLoad(t *testing.T) {
 	os.Setenv("PREST_CONF", "../testdata/prest.toml")
 
-	load()
+	Load()
 	if len(PrestConf.AccessConf.Tables) < 2 {
 		t.Errorf("expected > 2, got: %d", len(PrestConf.AccessConf.Tables))
 	}
 
-	load()
+	Load()
 	if !PrestConf.AccessConf.Restrict {
 		t.Error("expected true, but got false")
 	}
diff --git a/config/middlewares/middlewares_test.go b/config/middlewares/middlewares_test.go
index 3fdfb3f..836badc 100644
--- a/config/middlewares/middlewares_test.go
+++ b/config/middlewares/middlewares_test.go
@@ -10,12 +10,17 @@ import (
 	"testing"
 
 	"github.com/gorilla/mux"
+	"github.com/nuveo/prest/config"
 	"github.com/nuveo/prest/config/router"
 	"github.com/nuveo/prest/controllers"
 	"github.com/nuveo/prest/middlewares"
 	"github.com/urfave/negroni"
 )
 
+func init() {
+	config.Load()
+}
+
 func TestInitApp(t *testing.T) {
 	app = nil
 	initApp()
diff --git a/controllers/sql_test.go b/controllers/sql_test.go
index 36db517..d178749 100644
--- a/controllers/sql_test.go
+++ b/controllers/sql_test.go
@@ -15,7 +15,7 @@ import (
 
 func TestMain(m *testing.M) {
 	os.Setenv("PREST_CONF", "../testdata/prest.toml")
-	config.LoadToTest()
+	config.Load()
 
 	createMockScripts(config.PrestConf.QueriesPath)
 	writeMockScripts(config.PrestConf.QueriesPath)