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