From 16be7fd14edef3f6d71e7a256b4708b669e24bd6 Mon Sep 17 00:00:00 2001
From: Benjamin Bollen <ben@erisindustries.com>
Date: Mon, 16 May 2016 16:43:42 +0200
Subject: [PATCH] further mold cobra and viper structure for e-db

---
 cmd/eris-db.go    | 32 +++++++++++++++++++++-----------
 cmd/serve.go      | 25 +++++++++++++++++++------
 definitions/do.go | 27 ++++++++++++++++++++++-----
 3 files changed, 62 insertions(+), 22 deletions(-)

diff --git a/cmd/eris-db.go b/cmd/eris-db.go
index ada29c0e..e14f927f 100644
--- a/cmd/eris-db.go
+++ b/cmd/eris-db.go
@@ -17,14 +17,16 @@
 package commands
 
 import (
-  "fmt"
+  // "fmt"
   "os"
+  "strconv"
+  "strings"
 
   cobra "github.com/spf13/cobra"
 
-  common "github.com/eris-ltd/common/go/common"
+  // common "github.com/eris-ltd/common/go/common"
 
-  defintion "github.com/eris-ltd/eris-db/defintions"
+  definitions "github.com/eris-ltd/eris-db/definitions"
   version   "github.com/eris-ltd/eris-db/version"
 )
 
@@ -34,8 +36,8 @@ const VERSION = version.VERSION
 var do *definitions.Do
 
 var ErisDbCmd = &cobra.Command {
-  Use:   "eris-db"
-  Short: "Eris-DB is the beating heart of the eris chain."
+  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
 a modular consensus engine and application manager to run a chain to suit
 your needs.
@@ -44,7 +46,7 @@ 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]) {
+  PersistentPreRun: func(cmd *cobra.Command, args []string) {
     // TODO: [ben] set up eris logger after glide resolution of logrus
   },
   Run: func(cmd *cobra.Command, args []string) { cmd.Help() },
@@ -60,11 +62,12 @@ func Execute() {
 func InitErisDb() {
   // initialise an empty do struct for command execution
 	do = definitions.NowDo()
-}
-
-func AddCommands() {
-  buildServeCommand()
-  ErisDbCmd.AddCommand()
+  // 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() {
@@ -73,6 +76,13 @@ func AddGlobalFlags() {
 	ErisDbCmd.PersistentFlags().BoolVarP(&do.Output, "output", "o", defaultOutput(), "should eris-db provide an output of its execution; default respects $ERIS_DB_OUTPUT")
 }
 
+func AddCommands() {
+  buildServeCommand()
+  ErisDbCmd.AddCommand(ServeCmd)
+}
+
+
+
 //------------------------------------------------------------------------------
 // Defaults
 
diff --git a/cmd/serve.go b/cmd/serve.go
index cde712cc..6758fef0 100644
--- a/cmd/serve.go
+++ b/cmd/serve.go
@@ -18,13 +18,13 @@ package commands
 
 import (
   "fmt"
-  "os"
+  // "os"
 
   cobra "github.com/spf13/cobra"
 
-  common "github.com/eris-ltd/common/go/common"
+  // common "github.com/eris-ltd/common/go/common"
 
-  serve "github.com/eris-ltd/eris-db/serve"
+  // definitions "github.com/eris-ltd/eris-db/definitions"
 )
 
 var ServeCmd = &cobra.Command {
@@ -36,15 +36,28 @@ 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) {
-
+    fmt.Println("pre-run serving")
     // TODO: [ben] log marmotty welcome
-  }
+  },
+  Run: func(cmd *cobra.Command, args []string) {
+    serve()
+  },
+}
+
+// build the serve subcommand
+func buildServeCommand() {
+  addServeFlags()
+}
+
+func addServeFlags() {
+  fmt.Println("Adding Serve flags")
 }
 
 
 //------------------------------------------------------------------------------
 // functions
 
-func Serve(cmd *cobra.Command, args []string) {
+func serve() {
 
+  fmt.Println("Served")
 }
diff --git a/definitions/do.go b/definitions/do.go
index 6c7259d3..453ef8c2 100644
--- a/definitions/do.go
+++ b/definitions/do.go
@@ -16,20 +16,37 @@
 
 package definitions
 
+import (
+  viper "github.com/spf13/viper"
+)
+
 type Do struct {
-	Debug        bool
-	Verbose      bool
-	ChainId      string
+  // Persistent flags not reflected in the configuration files
+  // only set through command line flags or environment variables
+	Debug        bool     // ERIS_DB_DEBUG
+	Verbose      bool     // ERIS_DB_VERBOSE
+	Output       bool     // ERIS_DB_OUTPUT
+  // Capital configuration options explicitly extracted from the Viper config
+	ChainId      string   // has to be set to non-empty string,
+                        // uniquely identifying the chain.
 	// ChainType    string
 	// CSV          string
 	// AccountTypes []string
 	// Zip          bool
 	// Tarball      bool
-	Output       bool
+	Config       *viper.Viper
 	// Accounts     []*Account
 	// Result       string
 }
 
 func NowDo() *Do {
-	return &Do{}
+	do := new(Do)
+	do.Debug = false
+	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.ChainId = ""
+	do.Config = viper.New()
+	return do
 }
-- 
GitLab