-
Notifications
You must be signed in to change notification settings - Fork 12
/
config.go
142 lines (124 loc) · 4.01 KB
/
config.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package cli
import (
"fmt"
"path/filepath"
"strings"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/server"
"github.com/cosmos/cosmos-sdk/server/config"
"github.com/mitchellh/mapstructure"
"github.com/spf13/cobra"
"github.com/spf13/viper"
tmcfg "github.com/tendermint/tendermint/config"
fxcfg "github.com/functionx/fx-core/v3/server/config"
)
func RunConfigCmd(cmd *cobra.Command, args []string) error {
serverCtx := server.GetServerContextFromCmd(cmd)
clientCtx := client.GetClientContextFromCmd(cmd)
configName := filepath.Join(serverCtx.Config.RootDir, "config", args[0])
cfg, err := newConfig(serverCtx.Viper, configName)
if err != nil {
return err
}
// is len(args) == 1, get config file content
if len(args) == 1 {
return cfg.output(clientCtx)
}
// 2. is len(args) == 2, get config key value
if len(args) == 2 {
return output(clientCtx, serverCtx.Viper.Get(args[1]))
}
serverCtx.Viper.Set(args[1], args[2])
return cfg.save()
}
type cmdConfig interface {
save() error
output(ctx client.Context) error
}
var (
_ cmdConfig = &appTomlConfig{}
_ cmdConfig = &configTomlConfig{}
)
type appTomlConfig struct {
v *viper.Viper
config *fxcfg.Config
configName string
}
func (a *appTomlConfig) output(ctx client.Context) error {
return output(ctx, a.config)
}
func (a *appTomlConfig) save() error {
if err := a.v.Unmarshal(a.config, func(decoderConfig *mapstructure.DecoderConfig) {
decoderConfig.ZeroFields = true
}); err != nil {
return err
}
config.WriteConfigFile(a.configName, a.config)
return nil
}
type configTomlConfig struct {
v *viper.Viper
config *tmcfg.Config
configName string
}
func (c *configTomlConfig) output(ctx client.Context) error {
type outputConfig struct {
tmcfg.BaseConfig `mapstructure:",squash"`
RPC tmcfg.RPCConfig `mapstructure:"rpc"`
P2P tmcfg.P2PConfig `mapstructure:"p2p"`
Mempool tmcfg.MempoolConfig `mapstructure:"mempool"`
StateSync tmcfg.StateSyncConfig `mapstructure:"statesync"`
FastSync tmcfg.FastSyncConfig `mapstructure:"fastsync"`
Consensus tmcfg.ConsensusConfig `mapstructure:"consensus"`
TxIndex tmcfg.TxIndexConfig `mapstructure:"tx_index"`
Instrumentation tmcfg.InstrumentationConfig `mapstructure:"instrumentation"`
}
return output(ctx, outputConfig{
BaseConfig: c.config.BaseConfig,
RPC: *c.config.RPC,
P2P: *c.config.P2P,
Mempool: *c.config.Mempool,
StateSync: *c.config.StateSync,
FastSync: *c.config.FastSync,
Consensus: *c.config.Consensus,
TxIndex: *c.config.TxIndex,
Instrumentation: *c.config.Instrumentation,
})
}
func (c *configTomlConfig) save() error {
if err := c.v.Unmarshal(c.config, func(decoderConfig *mapstructure.DecoderConfig) {
decoderConfig.ZeroFields = true
}); err != nil {
return err
}
tmcfg.WriteConfigFile(c.configName, c.config)
return nil
}
func newConfig(v *viper.Viper, configName string) (cmdConfig, error) {
if strings.HasSuffix(configName, "app.toml") {
configData := fxcfg.Config{}
if err := v.Unmarshal(&configData); err != nil {
return nil, err
}
return &appTomlConfig{config: &configData, v: v, configName: configName}, nil
} else if strings.HasSuffix(configName, "config.toml") {
configData := tmcfg.Config{}
if err := v.Unmarshal(&configData); err != nil {
return nil, err
}
return &configTomlConfig{config: &configData, v: v, configName: configName}, nil
} else {
return nil, fmt.Errorf("invalid config file: %s, (support: %v)", configName, strings.Join([]string{"app.toml", "config.toml"}, "/"))
}
}
func output(ctx client.Context, content interface{}) error {
var mapData map[string]interface{}
if err := mapstructure.Decode(content, &mapData); err != nil {
var data interface{}
if err := mapstructure.Decode(content, &data); err != nil {
return err
}
return PrintOutput(ctx, data)
}
return PrintOutput(ctx, mapData)
}