Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve cli flags #142

Merged
merged 8 commits into from
Jun 26, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,28 @@
# Changelog

## 0.6.1 (TBD)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Delightful!


Make lots of small cli fixes that arose when people were using the tools for
the testnet.

IMPROVEMENTS:
- basecoin
- `basecoin start` supports all flags that `tendermint node` does, such as
`--rpc.laddr`, `--p2p.seeds`, and `--p2p.skip_upnp`
- fully supports `--log_level` and `--trace` for logger configuration
- basecli
- `basecli query account` accepts hex account address with or without `0x`
prefix
- `basecli init` is more intelligent and only complains if there really was
a connected chain, not just random files
- support `localhost:46657` or `http://localhost:46657` format for nodes,
not just `tcp://localhost:46657`
- gives error message when running commands on an unitialized chain, rather
than some unintelligable panic
- Add `--genesis` to init
- Example: `basecli init --node=localhost:46657 --genesis=$HOME/.basecoin/genesis.json`


## 0.6.0 (June 22, 2017)

Make the basecli command the only way to use client-side, to enforce best
Expand Down Expand Up @@ -31,7 +54,7 @@ BREAKING CHANGES:
- app
- Implements ABCI handshake by proxying merkleeyes.Info()

ENHANCEMENTS:
IMPROVEMENTS:
- `basecoin init` support `--chain-id`
- intergrates tendermint 0.10.0 (not the rc-2, but the real thing)
- commands return error code (1) on failure for easier script testing
Expand Down Expand Up @@ -60,7 +83,7 @@ BUG FIXES:
BREAKING CHANGES:
- only those related to the tendermint 0.9 -> 0.10 upgrade

ENHANCEMENTS:
IMPROVEMENTS:
- basecoin cli
- integrates tendermint 0.10.0 and unifies cli (init, unsafe_reset_all, ...)
- integrate viper, all command line flags can also be defined in environmental variables or config.toml
Expand Down
3 changes: 1 addition & 2 deletions cmd/basecli/commands/cmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
var SendTxCmd = &cobra.Command{
Use: "send",
Short: "send tokens from one account to another",
RunE: doSendTx,
RunE: commands.RequireInit(doSendTx),
}

//nolint
Expand All @@ -47,7 +47,6 @@ func init() {

// runDemo is an example of how to make a tx
func doSendTx(cmd *cobra.Command, args []string) error {

// load data from json or flags
tx := new(btypes.SendTx)
found, err := txcmd.LoadJSON(tx)
Expand Down
3 changes: 2 additions & 1 deletion cmd/basecli/commands/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

wire "github.com/tendermint/go-wire"
lc "github.com/tendermint/light-client"
lcmd "github.com/tendermint/light-client/commands"
proofcmd "github.com/tendermint/light-client/commands/proofs"
"github.com/tendermint/light-client/proofs"

Expand All @@ -15,7 +16,7 @@ import (
var AccountQueryCmd = &cobra.Command{
Use: "account [address]",
Short: "Get details of an account, with proof",
RunE: doAccountQuery,
RunE: lcmd.RequireInit(doAccountQuery),
}

func doAccountQuery(cmd *cobra.Command, args []string) error {
Expand Down
4 changes: 3 additions & 1 deletion cmd/basecoin/commands/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"path"

"github.com/spf13/cobra"

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

//commands
Expand Down Expand Up @@ -45,7 +47,7 @@ func setupFile(path, data string, perm os.FileMode) (int, error) {

func initCmd(cmd *cobra.Command, args []string) error {
// this will ensure that config.toml is there if not yet created, and create dir
cfg, err := getTendermintConfig()
cfg, err := tcmd.ParseConfig()
if err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/basecoin/commands/reset.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package commands
import (
"github.com/spf13/cobra"

tmcmd "github.com/tendermint/tendermint/cmd/tendermint/commands"
tcmd "github.com/tendermint/tendermint/cmd/tendermint/commands"
)

var UnsafeResetAllCmd = &cobra.Command{
Expand All @@ -13,10 +13,10 @@ var UnsafeResetAllCmd = &cobra.Command{
}

func unsafeResetAllCmd(cmd *cobra.Command, args []string) error {
cfg, err := getTendermintConfig()
cfg, err := tcmd.ParseConfig()
if err != nil {
return err
}
tmcmd.ResetAll(cfg.DBDir(), cfg.PrivValidatorFile(), logger)
tcmd.ResetAll(cfg.DBDir(), cfg.PrivValidatorFile(), logger)
return nil
}
30 changes: 30 additions & 0 deletions cmd/basecoin/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,39 @@ package commands
import (
"os"

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

"github.com/tendermint/tmlibs/cli"
tmflags "github.com/tendermint/tmlibs/cli/flags"
"github.com/tendermint/tmlibs/log"
)

const (
defaultLogLevel = "error"
FlagLogLevel = "log_level"
)

var (
logger = log.NewTMLogger(log.NewSyncWriter(os.Stdout)).With("module", "main")
)

var RootCmd = &cobra.Command{
Use: "basecoin",
Short: "A cryptocurrency framework in Golang based on Tendermint-Core",
PersistentPreRunE: func(cmd *cobra.Command, args []string) (err error) {
level := viper.GetString(FlagLogLevel)
logger, err = tmflags.ParseLogLevel(level, logger, defaultLogLevel)
if err != nil {
return err
}
if viper.GetBool(cli.TraceFlag) {
logger = log.NewTracingLogger(logger)
}
return nil
},
}

func init() {
RootCmd.PersistentFlags().String(FlagLogLevel, defaultLogLevel, "Log level")
}
64 changes: 23 additions & 41 deletions cmd/basecoin/commands/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ import (
"github.com/tendermint/abci/server"
eyes "github.com/tendermint/merkleeyes/client"
"github.com/tendermint/tmlibs/cli"
cliflags "github.com/tendermint/tmlibs/cli/flags"
cmn "github.com/tendermint/tmlibs/common"

"github.com/tendermint/tendermint/config"
tcmd "github.com/tendermint/tendermint/cmd/tendermint/commands"
"github.com/tendermint/tendermint/node"
"github.com/tendermint/tendermint/proxy"
"github.com/tendermint/tendermint/types"
Expand All @@ -29,38 +28,36 @@ var StartCmd = &cobra.Command{
RunE: startCmd,
}

//flags
var (
addrFlag string
eyesFlag string
dirFlag string
withoutTendermintFlag bool
)

// TODO: move to config file
const EyesCacheSize = 10000

func init() {
//nolint
const (
FlagAddress = "address"
FlagEyes = "eyes"
FlagWithoutTendermint = "without-tendermint"
)

flags := []Flag2Register{
{&addrFlag, "address", "tcp://0.0.0.0:46658", "Listen address"},
{&eyesFlag, "eyes", "local", "MerkleEyes address, or 'local' for embedded"},
{&dirFlag, "dir", ".", "Root directory"},
{&withoutTendermintFlag, "without-tendermint", false, "Run Tendermint in-process with the App"},
}
RegisterFlags(StartCmd, flags)
func init() {
flags := StartCmd.Flags()
flags.String(FlagAddress, "tcp://0.0.0.0:46658", "Listen address")
flags.String(FlagEyes, "local", "MerkleEyes address, or 'local' for embedded")
flags.Bool(FlagWithoutTendermint, false, "Only run basecoin abci app, assume external tendermint process")
// add all standard 'tendermint node' flags
tcmd.AddNodeFlags(StartCmd)
}

func startCmd(cmd *cobra.Command, args []string) error {
rootDir := viper.GetString(cli.HomeFlag)
meyes := viper.GetString(FlagEyes)

// Connect to MerkleEyes
var eyesCli *eyes.Client
if eyesFlag == "local" {
if meyes == "local" {
eyesCli = eyes.NewLocalClient(path.Join(rootDir, "data", "merkleeyes.db"), EyesCacheSize)
} else {
var err error
eyesCli, err = eyes.NewClient(eyesFlag)
eyesCli, err = eyes.NewClient(meyes)
if err != nil {
return errors.Errorf("Error connecting to MerkleEyes: %v\n", err)
}
Expand Down Expand Up @@ -94,7 +91,7 @@ func startCmd(cmd *cobra.Command, args []string) error {
}

chainID := basecoinApp.GetState().GetChainID()
if withoutTendermintFlag {
if viper.GetBool(FlagWithoutTendermint) {
logger.Info("Starting Basecoin without Tendermint", "chain_id", chainID)
// run just the abci app/server
return startBasecoinABCI(basecoinApp)
Expand All @@ -107,7 +104,8 @@ func startCmd(cmd *cobra.Command, args []string) error {

func startBasecoinABCI(basecoinApp *app.Basecoin) error {
// Start the ABCI listener
svr, err := server.NewServer(addrFlag, "socket", basecoinApp)
addr := viper.GetString(FlagAddress)
svr, err := server.NewServer(addr, "socket", basecoinApp)
if err != nil {
return errors.Errorf("Error creating listener: %v\n", err)
}
Expand All @@ -122,31 +120,15 @@ func startBasecoinABCI(basecoinApp *app.Basecoin) error {
return nil
}

func getTendermintConfig() (*config.Config, error) {
cfg := config.DefaultConfig()
err := viper.Unmarshal(cfg)
if err != nil {
return nil, err
}
cfg.SetRoot(cfg.RootDir)
config.EnsureRoot(cfg.RootDir)
return cfg, nil
}

func startTendermint(dir string, basecoinApp *app.Basecoin) error {
cfg, err := getTendermintConfig()
if err != nil {
return err
}

tmLogger, err := cliflags.ParseLogLevel(cfg.LogLevel, logger, config.DefaultConfig().LogLevel)
cfg, err := tcmd.ParseConfig()
if err != nil {
return err
}

// Create & start tendermint node
privValidator := types.LoadOrGenPrivValidator(cfg.PrivValidatorFile(), tmLogger)
n := node.NewNode(cfg, privValidator, proxy.NewLocalClientCreator(basecoinApp), tmLogger.With("module", "node"))
privValidator := types.LoadOrGenPrivValidator(cfg.PrivValidatorFile(), logger)
n := node.NewNode(cfg, privValidator, proxy.NewLocalClientCreator(basecoinApp), logger.With("module", "node"))

_, err = n.Start()
if err != nil {
Expand Down
11 changes: 3 additions & 8 deletions cmd/basecoin/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,22 @@ package main
import (
"os"

"github.com/spf13/cobra"

"github.com/tendermint/basecoin/cmd/basecoin/commands"
"github.com/tendermint/tmlibs/cli"
)

func main() {
var RootCmd = &cobra.Command{
Use: "basecoin",
Short: "A cryptocurrency framework in Golang based on Tendermint-Core",
}
rt := commands.RootCmd

RootCmd.AddCommand(
rt.AddCommand(
commands.InitCmd,
commands.StartCmd,
commands.RelayCmd,
commands.UnsafeResetAllCmd,
commands.VersionCmd,
)

cmd := cli.PrepareMainCmd(RootCmd, "BC", os.ExpandEnv("$HOME/.basecoin"))
cmd := cli.PrepareMainCmd(rt, "BC", os.ExpandEnv("$HOME/.basecoin"))
if err := cmd.Execute(); err != nil {
os.Exit(1)
}
Expand Down
Loading