You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

85 lines
2.1 KiB

7 years ago
7 years ago
7 years ago
  1. package commands
  2. import (
  3. "fmt"
  4. "os"
  5. "strings"
  6. "time"
  7. "github.com/spf13/cobra"
  8. "github.com/spf13/viper"
  9. cfg "github.com/tendermint/tendermint/config"
  10. "github.com/tendermint/tendermint/libs/cli"
  11. tmflags "github.com/tendermint/tendermint/libs/cli/flags"
  12. "github.com/tendermint/tendermint/libs/log"
  13. )
  14. var (
  15. config = cfg.DefaultConfig()
  16. logger = log.NewTMLogger(log.NewSyncWriter(os.Stdout))
  17. ctxTimeout = 4 * time.Second
  18. )
  19. func init() {
  20. registerFlagsRootCmd(RootCmd)
  21. }
  22. func registerFlagsRootCmd(cmd *cobra.Command) {
  23. cmd.PersistentFlags().String("log-level", config.LogLevel, "log level")
  24. }
  25. // ParseConfig retrieves the default environment configuration,
  26. // sets up the Tendermint root and ensures that the root exists
  27. func ParseConfig() (*cfg.Config, error) {
  28. conf := cfg.DefaultConfig()
  29. err := viper.Unmarshal(conf)
  30. if err != nil {
  31. return nil, err
  32. }
  33. conf.SetRoot(conf.RootDir)
  34. cfg.EnsureRoot(conf.RootDir)
  35. if err := conf.ValidateBasic(); err != nil {
  36. return nil, fmt.Errorf("error in config file: %v", err)
  37. }
  38. return conf, nil
  39. }
  40. // RootCmd is the root command for Tendermint core.
  41. var RootCmd = &cobra.Command{
  42. Use: "tendermint",
  43. Short: "BFT state machine replication for applications in any programming languages",
  44. PersistentPreRunE: func(cmd *cobra.Command, args []string) (err error) {
  45. if cmd.Name() == VersionCmd.Name() {
  46. return nil
  47. }
  48. config, err = ParseConfig()
  49. if err != nil {
  50. return err
  51. }
  52. if config.LogFormat == cfg.LogFormatJSON {
  53. logger = log.NewTMJSONLogger(log.NewSyncWriter(os.Stdout))
  54. }
  55. logger, err = tmflags.ParseLogLevel(config.LogLevel, logger, cfg.DefaultLogLevel)
  56. if err != nil {
  57. return err
  58. }
  59. if viper.GetBool(cli.TraceFlag) {
  60. logger = log.NewTracingLogger(logger)
  61. }
  62. logger = logger.With("module", "main")
  63. return nil
  64. },
  65. }
  66. // deprecateSnakeCase is a util function for 0.34.1. Should be removed in 0.35
  67. func deprecateSnakeCase(cmd *cobra.Command, args []string) {
  68. if strings.Contains(cmd.CalledAs(), "_") {
  69. fmt.Println("Deprecated: snake_case commands will be replaced by hyphen-case commands in the next major release")
  70. }
  71. }