/
config.go
228 lines (189 loc) · 5.94 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package cmd
import (
"bytes"
"encoding/json"
"fmt"
"os"
"path/filepath"
"text/template"
tmcli "github.com/tendermint/tendermint/libs/cli"
"github.com/spf13/cobra"
"github.com/cosmos/cosmos-sdk/client"
scconfig "github.com/cosmos/cosmos-sdk/client/config"
"github.com/cosmos/cosmos-sdk/client/flags"
viper "github.com/spf13/viper"
)
type JunoCustomClient struct {
scconfig.ClientConfig
Gas string `mapstructure:"gas" json:"gas"`
GasPrices string `mapstructure:"gas-prices" json:"gas-prices"`
GasAdjustment string `mapstructure:"gas-adjustment" json:"gas-adjustment"`
Fees string `mapstructure:"fees" json:"fees"`
FeeAccount string `mapstructure:"fee-account" json:"fee-account"`
Note string `mapstructure:"note" json:"note"`
}
// ConfigCmd returns a CLI command to interactively create an application CLI
// config file.
func ConfigCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "config <key> [value]",
Short: "Create or query an application CLI configuration file",
RunE: runConfigCmd,
Args: cobra.RangeArgs(0, 2),
}
return cmd
}
func runConfigCmd(cmd *cobra.Command, args []string) error {
clientCtx := client.GetClientContextFromCmd(cmd)
configPath := filepath.Join(clientCtx.HomeDir, "config")
conf, err := getClientConfig(configPath, clientCtx.Viper)
if err != nil {
return fmt.Errorf("couldn't get client config: %v", err)
}
jcc := JunoCustomClient{
*conf,
os.Getenv("JUNOD_GAS"),
os.Getenv("JUNOD_GAS_PRICES"),
os.Getenv("JUNOD_GAS_ADJUSTMENT"),
os.Getenv("JUNOD_FEES"),
os.Getenv("JUNOD_FEE_ACCOUNT"),
os.Getenv("JUNOD_NOTE"),
}
switch len(args) {
case 0:
s, err := json.MarshalIndent(jcc, "", "\t")
if err != nil {
return err
}
cmd.Println(string(s))
case 1:
// it's a get
key := args[0]
switch key {
case flags.FlagChainID:
cmd.Println(conf.ChainID)
case flags.FlagKeyringBackend:
cmd.Println(conf.KeyringBackend)
case tmcli.OutputFlag:
cmd.Println(conf.Output)
case flags.FlagNode:
cmd.Println(conf.Node)
case flags.FlagBroadcastMode:
cmd.Println(conf.BroadcastMode)
// Custom flags
case flags.FlagGas:
cmd.Println(jcc.Gas)
case flags.FlagGasPrices:
cmd.Println(jcc.GasPrices)
case flags.FlagGasAdjustment:
cmd.Println(jcc.GasAdjustment)
case flags.FlagFees:
cmd.Println(jcc.Fees)
case flags.FlagFeeAccount:
cmd.Println(jcc.FeeAccount)
case flags.FlagNote:
cmd.Println(jcc.Note)
default:
err := errUnknownConfigKey(key)
return fmt.Errorf("couldn't get the value for the key: %v, error: %v", key, err)
}
case 2:
// it's set
key, value := args[0], args[1]
switch key {
case flags.FlagChainID:
jcc.ChainID = value
case flags.FlagKeyringBackend:
jcc.KeyringBackend = value
case tmcli.OutputFlag:
jcc.Output = value
case flags.FlagNode:
jcc.Node = value
case flags.FlagBroadcastMode:
jcc.BroadcastMode = value
case flags.FlagGas:
jcc.Gas = value
case flags.FlagGasPrices:
jcc.GasPrices = value
jcc.Fees = "" // resets since we can only use 1 at a time
case flags.FlagGasAdjustment:
jcc.GasAdjustment = value
case flags.FlagFees:
jcc.Fees = value
jcc.GasPrices = "" // resets since we can only use 1 at a time
case flags.FlagFeeAccount:
jcc.FeeAccount = value
case flags.FlagNote:
jcc.Note = value
default:
return errUnknownConfigKey(key)
}
confFile := filepath.Join(configPath, "client.toml")
if err := writeConfigToFile(confFile, &jcc); err != nil {
return fmt.Errorf("could not write client config to the file: %v", err)
}
default:
panic("cound not execute config command")
}
return nil
}
const defaultConfigTemplate = `# This is a TOML config file.
# For more information, see https://github.com/toml-lang/toml
###############################################################################
### Client Configuration ###
###############################################################################
# The network chain ID
chain-id = "{{ .ChainID }}"
# The keyring's backend, where the keys are stored (os|file|kwallet|pass|test|memory)
keyring-backend = "{{ .KeyringBackend }}"
# CLI output format (text|json)
output = "{{ .Output }}"
# <host>:<port> to Tendermint RPC interface for this chain
node = "{{ .Node }}"
# Transaction broadcasting mode (sync|async|block)
broadcast-mode = "{{ .BroadcastMode }}"
###############################################################################
### Juno Tx Configuration ###
###############################################################################
# Amount of gas per transaction
gas = "{{ .Gas }}"
# Price per unit of gas (ex: 0.005ujuno)
gas-prices = "{{ .GasPrices }}"
gas-adjustment = "{{ .GasAdjustment }}"
# Fees to use instead of set gas prices
fees = "{{ .Fees }}"
fee-account = "{{ .FeeAccount }}"
# Memo to include in your Transactions
note = "{{ .Note }}"
`
// writeConfigToFile parses defaultConfigTemplate, renders config using the template and writes it to
// configFilePath.
func writeConfigToFile(configFilePath string, config *JunoCustomClient) error {
var buffer bytes.Buffer
tmpl := template.New("clientConfigFileTemplate")
configTemplate, err := tmpl.Parse(defaultConfigTemplate)
if err != nil {
return err
}
if err := configTemplate.Execute(&buffer, config); err != nil {
return err
}
return os.WriteFile(configFilePath, buffer.Bytes(), 0o600)
}
// getClientConfig reads values from client.toml file and unmarshalls them into ClientConfig
func getClientConfig(configPath string, v *viper.Viper) (*scconfig.ClientConfig, error) {
v.AddConfigPath(configPath)
v.SetConfigName("client")
v.SetConfigType("toml")
if err := v.ReadInConfig(); err != nil {
return nil, err
}
conf := new(scconfig.ClientConfig)
if err := v.Unmarshal(conf); err != nil {
return nil, err
}
return conf, nil
}
func errUnknownConfigKey(key string) error {
return fmt.Errorf("unknown configuration key: %q", key)
}