github.com/number571/tendermint@v0.34.11-gost/cmd/tendermint/commands/root.go (about)

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