/
root.go
96 lines (78 loc) · 2.23 KB
/
root.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package commands
import (
"fmt"
"os"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/dymensionxyz/dymint/config"
cfg "github.com/tendermint/tendermint/config"
"github.com/tendermint/tendermint/libs/cli"
tmflags "github.com/tendermint/tendermint/libs/cli/flags"
"github.com/tendermint/tendermint/libs/log"
)
var (
tmconfig = cfg.DefaultConfig()
dymconfig = config.DefaultNodeConfig
logger = log.NewTMLogger(log.NewSyncWriter(os.Stdout))
)
func init() {
registerFlagsRootCmd(RootCmd)
}
func registerFlagsRootCmd(cmd *cobra.Command) {
cmd.PersistentFlags().String("log_level", tmconfig.LogLevel, "log level")
}
// ParseConfig retrieves the default environment configuration,
// sets up the Dymint root and ensures that the root exists
func ParseConfig(cmd *cobra.Command) (*cfg.Config, error) {
conf := cfg.DefaultConfig()
err := viper.Unmarshal(conf)
if err != nil {
return nil, err
}
var home string
if os.Getenv("DYMINTHOME") != "" {
home = os.Getenv("DYMINTHOME")
} else {
home, err = cmd.Flags().GetString(cli.HomeFlag)
if err != nil {
return nil, err
}
}
conf.RootDir = home
conf.SetRoot(conf.RootDir)
cfg.EnsureRoot(conf.RootDir)
if err := conf.ValidateBasic(); err != nil {
return nil, fmt.Errorf("error in config file: %v", err)
}
cfg := config.DefaultConfig(home, "")
config.EnsureRoot(conf.RootDir, cfg)
return conf, nil
}
// RootCmd is the root command for Dymint core.
var RootCmd = &cobra.Command{
Use: "dymint",
Short: "ABCI-client implementation for dymension's autonomous rollapps",
PersistentPreRunE: func(cmd *cobra.Command, args []string) (err error) {
v := viper.GetViper()
// cmd.Flags() includes flags from this command and all persistent flags from the parent
if err := v.BindPFlags(cmd.Flags()); err != nil {
return err
}
tmconfig, err = ParseConfig(cmd)
if err != nil {
return err
}
if tmconfig.LogFormat == cfg.LogFormatJSON {
logger = log.NewTMJSONLogger(log.NewSyncWriter(os.Stdout))
}
logger, err = tmflags.ParseLogLevel(tmconfig.LogLevel, logger, cfg.DefaultLogLevel)
if err != nil {
return err
}
if viper.GetBool(cli.TraceFlag) {
logger = log.NewTracingLogger(logger)
}
logger = logger.With("module", "main")
return nil
},
}