Skip to content

Commit

Permalink
Pass logger and config through CLI context (closes cosmos#725)
Browse files Browse the repository at this point in the history
  • Loading branch information
cwgoes committed Apr 5, 2018
1 parent 2ee3ca3 commit 74a2246
Show file tree
Hide file tree
Showing 12 changed files with 158 additions and 88 deletions.
40 changes: 31 additions & 9 deletions examples/basecoin/cmd/basecoind/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@ import (
"path/filepath"

"github.com/spf13/cobra"
"github.com/spf13/viper"

abci "github.com/tendermint/abci/types"
tcmd "github.com/tendermint/tendermint/cmd/tendermint/commands"
cfg "github.com/tendermint/tendermint/config"
"github.com/tendermint/tmlibs/cli"
tmflags "github.com/tendermint/tmlibs/cli/flags"
cmn "github.com/tendermint/tmlibs/common"
dbm "github.com/tendermint/tmlibs/db"
"github.com/tendermint/tmlibs/log"
Expand All @@ -21,9 +25,31 @@ import (

// basecoindCmd is the entry point for this binary
var (
context = server.NewContext(nil, nil)
basecoindCmd = &cobra.Command{
Use: "gaiad",
Short: "Gaia Daemon (server)",
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
if cmd.Name() == version.VersionCmd.Name() {
return nil
}
config, err := tcmd.ParseConfig()
if err != nil {
return err
}
logger := log.NewTMLogger(log.NewSyncWriter(os.Stdout))
logger, err = tmflags.ParseLogLevel(config.LogLevel, logger, cfg.DefaultLogLevel())
if err != nil {
return err
}
if viper.GetBool(cli.TraceFlag) {
logger = log.NewTracingLogger(logger)
}
logger = logger.With("module", "main")
context.Config = config
context.Logger = logger
return nil
},
}
)

Expand Down Expand Up @@ -76,16 +102,12 @@ func generateApp(rootDir string, logger log.Logger) (abci.Application, error) {
}

func main() {
// TODO: set logger through CLI
logger := log.NewTMLogger(log.NewSyncWriter(os.Stdout)).
With("module", "main")

basecoindCmd.AddCommand(
server.InitCmd(defaultOptions, logger),
server.StartCmd(generateApp, logger),
server.UnsafeResetAllCmd(logger),
server.ShowNodeIdCmd(logger),
server.ShowValidatorCmd(logger),
server.InitCmd(defaultOptions, context),
server.StartCmd(generateApp, context),
server.UnsafeResetAllCmd(context),
server.ShowNodeIdCmd(context),
server.ShowValidatorCmd(context),
version.VersionCmd,
)

Expand Down
40 changes: 31 additions & 9 deletions examples/democoin/cmd/democoind/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@ import (
"path/filepath"

"github.com/spf13/cobra"
"github.com/spf13/viper"

abci "github.com/tendermint/abci/types"
tcmd "github.com/tendermint/tendermint/cmd/tendermint/commands"
cfg "github.com/tendermint/tendermint/config"
"github.com/tendermint/tmlibs/cli"
tmflags "github.com/tendermint/tmlibs/cli/flags"
cmn "github.com/tendermint/tmlibs/common"
dbm "github.com/tendermint/tmlibs/db"
"github.com/tendermint/tmlibs/log"
Expand All @@ -21,9 +25,31 @@ import (

// democoindCmd is the entry point for this binary
var (
context = server.NewContext(nil, nil)
democoindCmd = &cobra.Command{
Use: "democoind",
Short: "Gaia Daemon (server)",
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
if cmd.Name() == version.VersionCmd.Name() {
return nil
}
config, err := tcmd.ParseConfig()
if err != nil {
return err
}
logger := log.NewTMLogger(log.NewSyncWriter(os.Stdout))
logger, err = tmflags.ParseLogLevel(config.LogLevel, logger, cfg.DefaultLogLevel())
if err != nil {
return err
}
if viper.GetBool(cli.TraceFlag) {
logger = log.NewTracingLogger(logger)
}
logger = logger.With("module", "main")
context.Config = config
context.Logger = logger
return nil
},
}
)

Expand Down Expand Up @@ -82,16 +108,12 @@ func generateApp(rootDir string, logger log.Logger) (abci.Application, error) {
}

func main() {
// TODO: set logger through CLI
logger := log.NewTMLogger(log.NewSyncWriter(os.Stdout)).
With("module", "main")

democoindCmd.AddCommand(
server.InitCmd(defaultOptions, logger),
server.StartCmd(generateApp, logger),
server.UnsafeResetAllCmd(logger),
server.ShowNodeIdCmd(logger),
server.ShowValidatorCmd(logger),
server.InitCmd(defaultOptions, context),
server.StartCmd(generateApp, context),
server.UnsafeResetAllCmd(context),
server.ShowNodeIdCmd(context),
server.ShowValidatorCmd(context),
version.VersionCmd,
)

Expand Down
35 changes: 29 additions & 6 deletions examples/gaia/gaiad/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@ import (
"os"

"github.com/spf13/cobra"
"github.com/spf13/viper"

abci "github.com/tendermint/abci/types"
tcmd "github.com/tendermint/tendermint/cmd/tendermint/commands"
cfg "github.com/tendermint/tendermint/config"
"github.com/tendermint/tmlibs/cli"
tmflags "github.com/tendermint/tmlibs/cli/flags"
cmn "github.com/tendermint/tmlibs/common"
"github.com/tendermint/tmlibs/log"

Expand All @@ -19,9 +23,31 @@ import (

// gaiadCmd is the entry point for this binary
var (
context = server.NewContext(nil, nil)
gaiadCmd = &cobra.Command{
Use: "gaiad",
Short: "Gaia Daemon (server)",
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
if cmd.Name() == version.VersionCmd.Name() {
return nil
}
config, err := tcmd.ParseConfig()
if err != nil {
return err
}
logger := log.NewTMLogger(log.NewSyncWriter(os.Stdout))
logger, err = tmflags.ParseLogLevel(config.LogLevel, logger, cfg.DefaultLogLevel())
if err != nil {
return err
}
if viper.GetBool(cli.TraceFlag) {
logger = log.NewTracingLogger(logger)
}
logger = logger.With("module", "main")
context.Config = config
context.Logger = logger
return nil
},
}
)

Expand Down Expand Up @@ -56,13 +82,10 @@ func generateApp(rootDir string, logger log.Logger) (abci.Application, error) {
}

func main() {
logger := log.NewTMLogger(log.NewSyncWriter(os.Stdout)).
With("module", "main")

gaiadCmd.AddCommand(
server.InitCmd(defaultOptions, logger),
server.StartCmd(generateApp, logger),
server.UnsafeResetAllCmd(logger),
server.InitCmd(defaultOptions, context),
server.StartCmd(generateApp, context),
server.UnsafeResetAllCmd(context),
version.VersionCmd,
)

Expand Down
15 changes: 15 additions & 0 deletions server/context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package server

import (
cfg "github.com/tendermint/tendermint/config"
"github.com/tendermint/tmlibs/log"
)

type Context struct {
Config *cfg.Config
Logger log.Logger
}

func NewContext(config *cfg.Config, logger log.Logger) *Context {
return &Context{config, logger}
}
26 changes: 10 additions & 16 deletions server/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,10 @@ import (

"github.com/spf13/cobra"

cmn "github.com/tendermint/tmlibs/common"
"github.com/tendermint/tmlibs/log"

tcmd "github.com/tendermint/tendermint/cmd/tendermint/commands"
cfg "github.com/tendermint/tendermint/config"
"github.com/tendermint/tendermint/p2p"
tmtypes "github.com/tendermint/tendermint/types"
cmn "github.com/tendermint/tmlibs/common"
)

type testnetInformation struct {
Expand All @@ -29,10 +26,10 @@ type testnetInformation struct {
// The application can pass in a function to generate
// proper state. And may want to use GenerateCoinKey
// to create default account(s).
func InitCmd(gen GenAppState, logger log.Logger) *cobra.Command {
func InitCmd(gen GenAppState, ctx *Context) *cobra.Command {
cmd := initCmd{
genAppState: gen,
logger: logger,
context: ctx,
}
cobraCmd := cobra.Command{
Use: "init",
Expand All @@ -50,7 +47,7 @@ type GenAppState func(args []string) (json.RawMessage, string, cmn.HexBytes, err

type initCmd struct {
genAppState GenAppState
logger log.Logger
context *Context
}

func (c initCmd) run(cmd *cobra.Command, args []string) error {
Expand All @@ -59,11 +56,8 @@ func (c initCmd) run(cmd *cobra.Command, args []string) error {

// Run the basic tendermint initialization,
// set up a default genesis with no app_options
config, err := tcmd.ParseConfig()
if err != nil {
return err
}
err = c.initTendermintFiles(config, &testnetInfo)
config := c.context.Config
err := c.initTendermintFiles(config, &testnetInfo)
if err != nil {
return err
}
Expand Down Expand Up @@ -109,17 +103,17 @@ func (c initCmd) initTendermintFiles(config *cfg.Config, info *testnetInformatio
var privValidator *tmtypes.PrivValidatorFS
if cmn.FileExists(privValFile) {
privValidator = tmtypes.LoadPrivValidatorFS(privValFile)
c.logger.Info("Found private validator", "path", privValFile)
c.context.Logger.Info("Found private validator", "path", privValFile)
} else {
privValidator = tmtypes.GenPrivValidatorFS(privValFile)
privValidator.Save()
c.logger.Info("Generated private validator", "path", privValFile)
c.context.Logger.Info("Generated private validator", "path", privValFile)
}

// genesis file
genFile := config.GenesisFile()
if cmn.FileExists(genFile) {
c.logger.Info("Found genesis file", "path", genFile)
c.context.Logger.Info("Found genesis file", "path", genFile)
} else {
genDoc := tmtypes.GenesisDoc{
ChainID: cmn.Fmt("test-chain-%v", cmn.RandStr(6)),
Expand All @@ -132,7 +126,7 @@ func (c initCmd) initTendermintFiles(config *cfg.Config, info *testnetInformatio
if err := genDoc.SaveAs(genFile); err != nil {
return err
}
c.logger.Info("Generated genesis file", "path", genFile)
c.context.Logger.Info("Generated genesis file", "path", genFile)
}

// reload the config file and find our validator info
Expand Down
8 changes: 6 additions & 2 deletions server/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@ import (
"github.com/tendermint/tmlibs/log"

"github.com/cosmos/cosmos-sdk/mock"
tcmd "github.com/tendermint/tendermint/cmd/tendermint/commands"
)

func TestInit(t *testing.T) {
defer setupViper(t)()

logger := log.NewNopLogger()
cmd := InitCmd(mock.GenInitOptions, logger)
err := cmd.RunE(nil, nil)
cfg, err := tcmd.ParseConfig()
require.Nil(t, err)
ctx := NewContext(cfg, logger)
cmd := InitCmd(mock.GenInitOptions, ctx)
err = cmd.RunE(nil, nil)
require.NoError(t, err)
}
14 changes: 5 additions & 9 deletions server/reset.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ import (
"github.com/spf13/cobra"

tcmd "github.com/tendermint/tendermint/cmd/tendermint/commands"
"github.com/tendermint/tmlibs/log"
)

// UnsafeResetAllCmd - extension of the tendermint command, resets initialization
func UnsafeResetAllCmd(logger log.Logger) *cobra.Command {
cmd := resetAll{logger}
func UnsafeResetAllCmd(ctx *Context) *cobra.Command {
cmd := resetAll{ctx}
return &cobra.Command{
Use: "unsafe_reset_all",
Short: "Reset all blockchain data",
Expand All @@ -18,14 +17,11 @@ func UnsafeResetAllCmd(logger log.Logger) *cobra.Command {
}

type resetAll struct {
logger log.Logger
context *Context
}

func (r resetAll) run(cmd *cobra.Command, args []string) error {
cfg, err := tcmd.ParseConfig()
if err != nil {
return err
}
tcmd.ResetAll(cfg.DBDir(), cfg.PrivValidatorFile(), r.logger)
cfg := r.context.Config
tcmd.ResetAll(cfg.DBDir(), cfg.PrivValidatorFile(), r.context.Logger)
return nil
}
13 changes: 4 additions & 9 deletions server/show_node_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@ import (

"github.com/spf13/cobra"

tcmd "github.com/tendermint/tendermint/cmd/tendermint/commands"
"github.com/tendermint/tendermint/p2p"
"github.com/tendermint/tmlibs/log"
)

// ShowNodeIdCmd - ported from Tendermint, dump node ID to stdout
func ShowNodeIdCmd(logger log.Logger) *cobra.Command {
cmd := showNodeId{logger}
func ShowNodeIdCmd(ctx *Context) *cobra.Command {
cmd := showNodeId{ctx}
return &cobra.Command{
Use: "show_node_id",
Short: "Show this node's ID",
Expand All @@ -21,14 +19,11 @@ func ShowNodeIdCmd(logger log.Logger) *cobra.Command {
}

type showNodeId struct {
logger log.Logger
context *Context
}

func (s showNodeId) run(cmd *cobra.Command, args []string) error {
cfg, err := tcmd.ParseConfig()
if err != nil {
return err
}
cfg := s.context.Config
nodeKey, err := p2p.LoadOrGenNodeKey(cfg.NodeKeyFile())
if err != nil {
return err
Expand Down
Loading

0 comments on commit 74a2246

Please sign in to comment.