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 7 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
23 changes: 23 additions & 0 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.

ENHANCEMENTS:
- 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
- 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
3 changes: 3 additions & 0 deletions cmd/basecli/commands/cmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ func init() {

// runDemo is an example of how to make a tx
func doSendTx(cmd *cobra.Command, args []string) error {
if err := commands.RequireInit(cmd); err != nil {
Copy link
Member

Choose a reason for hiding this comment

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

Lets wrap these, as discussed

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done and done. :)

return err
}

// load data from json or flags
tx := new(btypes.SendTx)
Expand Down
5 changes: 5 additions & 0 deletions 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 @@ -19,6 +20,10 @@ var AccountQueryCmd = &cobra.Command{
}

func doAccountQuery(cmd *cobra.Command, args []string) error {
if err := lcmd.RequireInit(cmd); err != nil {
return err
}

addr, err := proofcmd.ParseHexKey(args, "address")
if err != nil {
return err
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
28 changes: 14 additions & 14 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading