From bf0ed1e9b68d3e2aad8a5500b99b73535f184c0b Mon Sep 17 00:00:00 2001
From: Benjamin Bollen <ben@erisindustries.com>
Date: Wed, 25 May 2016 12:15:08 +0200
Subject: [PATCH] use eris-logger; move configuration loading to defintions/Do

---
 cmd/eris-db.go    | 26 +++++++++++---------------
 cmd/serve.go      | 36 ++++++++++++++++--------------------
 definitions/do.go | 16 ++++++++++++++--
 3 files changed, 41 insertions(+), 37 deletions(-)

diff --git a/cmd/eris-db.go b/cmd/eris-db.go
index e14f927f..e68ed0b6 100644
--- a/cmd/eris-db.go
+++ b/cmd/eris-db.go
@@ -17,17 +17,16 @@
 package commands
 
 import (
-  // "fmt"
   "os"
   "strconv"
   "strings"
 
   cobra "github.com/spf13/cobra"
 
-  // common "github.com/eris-ltd/common/go/common"
+  log         "github.com/eris-ltd/eris-logger"
 
   definitions "github.com/eris-ltd/eris-db/definitions"
-  version   "github.com/eris-ltd/eris-db/version"
+  version     "github.com/eris-ltd/eris-db/version"
 )
 
 const VERSION = version.VERSION
@@ -37,8 +36,8 @@ var do *definitions.Do
 
 var ErisDbCmd = &cobra.Command {
   Use:   "eris-db",
-  Short: "Eris-DB is the beating heart of the eris chain.",
-  Long:  `Eris-DB is the beating heart of the eris chain.  Eris-DB combines
+  Short: "Eris-DB is the heart of the eris chain.",
+  Long:  `Eris-DB is the heart of the eris chain.  Eris-DB combines
 a modular consensus engine and application manager to run a chain to suit
 your needs.
 
@@ -47,7 +46,13 @@ Made with <3 by Eris Industries.
 Complete documentation is available at https://docs.erisindustries.com
 ` + "\nVERSION:\n " + VERSION,
   PersistentPreRun: func(cmd *cobra.Command, args []string) {
-    // TODO: [ben] set up eris logger after glide resolution of logrus
+
+    log.SetLevel(log.WarnLevel)
+    if do.Verbose {
+      log.SetLevel(log.InfoLevel)
+    } else if do.Debug {
+      log.SetLevel(log.DebugLevel)
+    }
   },
   Run: func(cmd *cobra.Command, args []string) { cmd.Help() },
 }
@@ -62,18 +67,11 @@ func Execute() {
 func InitErisDb() {
   // initialise an empty do struct for command execution
 	do = definitions.NowDo()
-  // name of the configuration file without extension
-  do.Config.SetConfigName("config")
-  do.Config.SetConfigType("yaml")
-  // look for configuration file in the working directory
-  do.Config.AddConfigPath(".")
-  // but let's move the location of the configuration to
 }
 
 func AddGlobalFlags() {
 	ErisDbCmd.PersistentFlags().BoolVarP(&do.Verbose, "verbose", "v", defaultVerbose(), "verbose output; more output than no output flags; less output than debug level; default respects $ERIS_DB_VERBOSE")
 	ErisDbCmd.PersistentFlags().BoolVarP(&do.Debug, "debug", "d", defaultDebug(), "debug level output; the most output available for eris-db; if it is too chatty use verbose flag; default respects $ERIS_DB_DEBUG")
-	ErisDbCmd.PersistentFlags().BoolVarP(&do.Output, "output", "o", defaultOutput(), "should eris-db provide an output of its execution; default respects $ERIS_DB_OUTPUT")
 }
 
 func AddCommands() {
@@ -81,8 +79,6 @@ func AddCommands() {
   ErisDbCmd.AddCommand(ServeCmd)
 }
 
-
-
 //------------------------------------------------------------------------------
 // Defaults
 
diff --git a/cmd/serve.go b/cmd/serve.go
index be84ef88..0bb45e4a 100644
--- a/cmd/serve.go
+++ b/cmd/serve.go
@@ -17,14 +17,11 @@
 package commands
 
 import (
-  "fmt"
-  // "os"
+  "os"
 
   cobra "github.com/spf13/cobra"
 
-  // common "github.com/eris-ltd/common/go/common"
-
-  // definitions "github.com/eris-ltd/eris-db/definitions"
+  log "github.com/eris-ltd/eris-logger"
 )
 
 var ServeCmd = &cobra.Command {
@@ -35,9 +32,6 @@ The Eris-DB node is modularly configured for the consensus engine and applicatio
 manager.  The client API can be disabled.`,
   Example: `$ eris-db serve -- will start the Eris-DB node based on the configuration file in the current working directory,
 $ eris-db serve myChainId --work-dir=/path/to/config -- will start the Eris-DB node based on the configuration file provided and assert the chain id matches.`,
-  PreRun: func(cmd *cobra.Command, args []string) {
-    // TODO: [ben] log marmotty welcome
-  },
   Run: func(cmd *cobra.Command, args []string) {
     serve()
   },
@@ -49,28 +43,30 @@ func buildServeCommand() {
 }
 
 func addServeFlags() {
-  fmt.Println("Adding Serve flags")
   ServeCmd.PersistentFlags().StringVarP(&do.WorkDir, "work-dir", "w",
     defaultWorkDir(), "specify the working directory for the chain to run.  If omitted, and no path set in $ERIS_DB_WORKDIR, the current working directory is taken.")
 }
 
-
 //------------------------------------------------------------------------------
 // functions
 
 func serve() {
-  //
-  // load config from
-  loadConfig()
-  fmt.Printf("Served from %s \n", do.WorkDir)
+  // load configuration from a single location to avoid a wrong configuration
+  // file is loaded.
+  if err := do.ReadConfig(do.WorkDir, "server_config", "toml"); err != nil {
+    log.Fatalf("Fatal error reading server_config.toml : %s \n work directory: %s \n",
+      err, do.WorkDir)
+    os.Exit(1)
+  }
+  // load chain_id for assertion
+  if do.ChainId = do.Config.GetString("chain.chain_id"); do.ChainId == "" {
+    log.Fatalf("Failed to read non-empty string for ChainId from config.")
+    os.Exit(1)
+  }
+
+  log.Info("Eris-DB serve initializing ", do.ChainId, " from ", do.WorkDir)
 }
 
-//------------------------------------------------------------------------------
-// Viper configuration
-
-func loadConfig(conf *viper.Viper, path string) {
-  conf.
-}
 
 //------------------------------------------------------------------------------
 // Defaults
diff --git a/definitions/do.go b/definitions/do.go
index cce92dde..5ecc2f1c 100644
--- a/definitions/do.go
+++ b/definitions/do.go
@@ -25,7 +25,6 @@ type Do struct {
   // only set through command line flags or environment variables
 	Debug        bool     // ERIS_DB_DEBUG
 	Verbose      bool     // ERIS_DB_VERBOSE
-	Output       bool     // ERIS_DB_OUTPUT
   WorkDir      string
   // Capital configuration options explicitly extracted from the Viper config
 	ChainId      string   // has to be set to non-empty string,
@@ -46,9 +45,22 @@ func NowDo() *Do {
 	do.Verbose = false
 	// the default value for output is set to true in cmd/eris-db.go;
 	// avoid double setting it here though
-	do.Output = false
   do.WorkDir = ""
   do.ChainId = ""
 	do.Config = viper.New()
 	return do
 }
+
+// ReadConfig uses Viper to set the configuration file name, file format
+// where Eris-DB currently only uses `toml`.
+// The search directory is explicitly limited to a single location to
+// minimise the chance of loading the wrong configuration file.
+func (d *Do) ReadConfig(directory string, name string, configType string) error {
+  // name of the configuration file without extension
+  d.Config.SetConfigName(name)
+  // Eris-DB currently only uses "toml"
+  d.Config.SetConfigType(configType)
+  // look for configuration file in the working directory
+  d.Config.AddConfigPath(directory)
+  return d.Config.ReadInConfig()
+}
-- 
GitLab