github.com/516108736/tendermint@v0.36.0/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() (*cfg.Config, error) {
    33  	conf := cfg.DefaultConfig()
    34  	err := viper.Unmarshal(conf)
    35  	if err != nil {
    36  		return nil, err
    37  	}
    38  	conf.SetRoot(conf.RootDir)
    39  	cfg.EnsureRoot(conf.RootDir)
    40  	if err := conf.ValidateBasic(); err != nil {
    41  		return nil, fmt.Errorf("error in config file: %v", err)
    42  	}
    43  	return conf, nil
    44  }
    45  
    46  // RootCmd is the root command for Tendermint core.
    47  var RootCmd = &cobra.Command{
    48  	Use:   "tendermint",
    49  	Short: "BFT state machine replication for applications in any programming languages",
    50  	PersistentPreRunE: func(cmd *cobra.Command, args []string) (err error) {
    51  		if cmd.Name() == VersionCmd.Name() {
    52  			return nil
    53  		}
    54  
    55  		config, err = ParseConfig()
    56  		if err != nil {
    57  			return err
    58  		}
    59  
    60  		if config.LogFormat == cfg.LogFormatJSON {
    61  			logger = log.NewTMJSONLogger(log.NewSyncWriter(os.Stdout))
    62  		}
    63  
    64  		logger, err = tmflags.ParseLogLevel(config.LogLevel, logger, cfg.DefaultLogLevel)
    65  		if err != nil {
    66  			return err
    67  		}
    68  
    69  		if viper.GetBool(cli.TraceFlag) {
    70  			logger = log.NewTracingLogger(logger)
    71  		}
    72  
    73  		logger = logger.With("module", "main")
    74  		return nil
    75  	},
    76  }
    77  
    78  // deprecateSnakeCase is a util function for 0.34.1. Should be removed in 0.35
    79  func deprecateSnakeCase(cmd *cobra.Command, args []string) {
    80  	if strings.Contains(cmd.CalledAs(), "_") {
    81  		fmt.Println("Deprecated: snake_case commands will be replaced by hyphen-case commands in the next major release")
    82  	}
    83  }