From 197b9760586f1f7865a6bc86f9a5dff4cf2400e4 Mon Sep 17 00:00:00 2001
From: Benjamin Bollen <ben@erisindustries.com>
Date: Fri, 13 May 2016 13:47:02 +0200
Subject: [PATCH] continue work on cobra; add serve command; do struct and some
 default flags

---
 cmd/eris-db.go                  | 43 ++++++++++++++++++++++++++++++---
 cmd/{erisdb => eris-db}/main.go |  0
 cmd/serve.go                    | 22 ++++++++++++++++-
 definitions/do.go               | 35 +++++++++++++++++++++++++++
 4 files changed, 95 insertions(+), 5 deletions(-)
 rename cmd/{erisdb => eris-db}/main.go (100%)
 create mode 100644 definitions/do.go

diff --git a/cmd/eris-db.go b/cmd/eris-db.go
index 543442a1..f46c5c8c 100644
--- a/cmd/eris-db.go
+++ b/cmd/eris-db.go
@@ -24,11 +24,15 @@ import (
 
   common "github.com/eris-ltd/common/go/common"
 
-  version "github.com/eris-ltd/eris-db/version"
+  defintion "github.com/eris-ltd/eris-db/defintions"
+  version   "github.com/eris-ltd/eris-db/version"
 )
 
 const VERSION = version.VERSION
 
+// Global Do struct
+var do *definitions.Do
+
 var ErisDbCmd = &cobra.Command {
   Use:   "eris-db"
   Short: "Eris-DB is the beating heart of the eris chain."
@@ -47,17 +51,48 @@ Complete documentation is available at https://docs.erisindustries.com
 }
 
 func Execute() {
+  InitErisDb()
+  AddGlobalFlags()
   AddCommands()
   ErisDbCmd.Execute()
 }
 
+func InitErisDb() {
+  // initialise an empty do struct for command execution
+	do = definitions.NowDo()
+}
+
 func AddCommands() {
   buildServeCommand()
   ErisDbCmd.AddCommand()
 }
 
 func AddGlobalFlags() {
-	ErisCMCmd.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")
-	ErisCMCmd.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")
-	ErisCMCmd.PersistentFlags().BoolVarP(&do.Output, "output", "o", defaultOutput(), "should eris-db provide an output of its job; default respects $ERIS_DB_OUTPUT")
+	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")
+}
+
+//------------------------------------------------------------------------------
+// Defaults
+
+func defaultVerbose() bool {
+  return setDefaultBool("ERIS_DB_VERBOSE", false)
+}
+
+func defaultDebug() bool {
+  return setDefaultBool("ERIS_DB_DEBUG", false)
+}
+
+func defaultOutput() bool {
+  return setDefaultBool("ERIS_DB_OUTPUT", true)
+}
+
+func setDefaultBool(envVar string, def bool) bool {
+	env := os.Getenv(envVar)
+	if env != "" {
+		i, _ := strconv.ParseBool(env)
+		return i
+	}
+	return def
 }
diff --git a/cmd/erisdb/main.go b/cmd/eris-db/main.go
similarity index 100%
rename from cmd/erisdb/main.go
rename to cmd/eris-db/main.go
diff --git a/cmd/serve.go b/cmd/serve.go
index d2e385b7..cde712cc 100644
--- a/cmd/serve.go
+++ b/cmd/serve.go
@@ -27,4 +27,24 @@ import (
   serve "github.com/eris-ltd/eris-db/serve"
 )
 
-var
+var ServeCmd = &cobra.Command {
+  Use:   "serve",
+  Short: "Eris-DB serve starts an eris-db node with client API enabled by default.",
+  Long:  `Eris-DB serve starts an eris-db node with client API enabled by default.
+The Eris-DB node is modularly configured for the consensus engine and application
+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
+  }
+}
+
+
+//------------------------------------------------------------------------------
+// functions
+
+func Serve(cmd *cobra.Command, args []string) {
+
+}
diff --git a/definitions/do.go b/definitions/do.go
new file mode 100644
index 00000000..6c7259d3
--- /dev/null
+++ b/definitions/do.go
@@ -0,0 +1,35 @@
+// Copyright 2015, 2016 Eris Industries (UK) Ltd.
+// This file is part of Eris-RT
+
+// Eris-RT is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+
+// Eris-RT is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License
+// along with Eris-RT.  If not, see <http://www.gnu.org/licenses/>.
+
+package definitions
+
+type Do struct {
+	Debug        bool
+	Verbose      bool
+	ChainId      string
+	// ChainType    string
+	// CSV          string
+	// AccountTypes []string
+	// Zip          bool
+	// Tarball      bool
+	Output       bool
+	// Accounts     []*Account
+	// Result       string
+}
+
+func NowDo() *Do {
+	return &Do{}
+}
-- 
GitLab