This repository has been archived by the owner on Feb 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 83
/
config.go
215 lines (194 loc) · 5.37 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
package cmd
import (
"fmt"
"io/ioutil"
"os"
"path"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/simapp"
"github.com/cosmos/cosmos-sdk/types/module"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/strangelove-ventures/lens/client"
"gopkg.in/yaml.v2"
)
func configCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "config",
Aliases: []string{"cfg"},
Short: "manage configuration file",
}
cmd.AddCommand(
configInitCmd(),
)
return cmd
}
// Command for inititalizing an empty config at the --home location
func configInitCmd() *cobra.Command {
// TODO: add a `--chain` flag here to specify which chain to initialize
// this should reference standard configs in an `interchain` directory
// similar to the one in the relayer
cmd := &cobra.Command{
Use: "init",
Aliases: []string{"i"},
Short: "Creates a default home directory at path defined by --home",
RunE: func(cmd *cobra.Command, args []string) error {
home, err := cmd.Flags().GetString(flags.FlagHome)
if err != nil {
return err
}
debug, err := cmd.Flags().GetBool("debug")
if err != nil {
return err
}
cfgPath := path.Join(home, "config.yaml")
// If the config doesn't exist...
if _, err := os.Stat(cfgPath); os.IsNotExist(err) {
// And the config folder doesn't exist...
// And the home folder doesn't exist
if _, err := os.Stat(home); os.IsNotExist(err) {
// Create the home folder
if err = os.Mkdir(home, os.ModePerm); err != nil {
return err
}
}
}
// Then create the file...
f, err := os.Create(cfgPath)
if err != nil {
return err
}
defer f.Close()
// And write the default config to that location...
if _, err = f.Write(defaultConfig(path.Join(home, "keys"), debug)); err != nil {
return err
}
// And return no error...
return nil
},
}
return cmd
}
// Config represents the config file for the relayer
type Config struct {
DefaultChain string `yaml:"default_chain"`
Chains []*client.ChainClientConfig `yaml:"chains"`
}
// Called to initialize the relayer.Chain types on Config
func validateConfig(c *Config) error {
var found bool
for _, chain := range c.Chains {
if err := chain.Validate(); err != nil {
return err
}
if c.DefaultChain == chain.ChainID {
found = true
}
}
if !found {
return fmt.Errorf("default chain %s not found in chains", c.DefaultChain)
}
return nil
}
// MustYAML returns the yaml string representation of the Paths
func (c Config) MustYAML() []byte {
out, err := yaml.Marshal(c)
if err != nil {
panic(err)
}
return out
}
func defaultConfig(keyHome string, debug bool) []byte {
modules := []module.AppModuleBasic{}
for _, v := range simapp.ModuleBasics {
modules = append(modules, v)
}
cfg := Config{
DefaultChain: "cosmoshub-4",
Chains: []*client.ChainClientConfig{
{
Key: "default",
ChainID: "cosmoshub-4",
RPCAddr: "https://cosmoshub-4.technofractal.com:443",
GRPCAddr: "https://gprc.cosmoshub-4.technofractal.com:443",
AccountPrefix: "cosmos",
KeyringBackend: "test",
GasAdjustment: 1.2,
GasPrices: "0.01uatom",
KeyDirectory: keyHome,
Debug: debug,
Timeout: "20s",
OutputFormat: "json",
BroadcastMode: "block",
SignModeStr: "direct",
Modules: modules,
},
{
Key: "default",
ChainID: "osmosis-1",
RPCAddr: "https://osmosis-1.technofractal.com:443",
GRPCAddr: "https://grpc.osmosis-1.technofractal.com:443",
AccountPrefix: "osmo",
KeyringBackend: "test",
GasAdjustment: 1.2,
GasPrices: "0.01uosmo",
KeyDirectory: keyHome,
Debug: debug,
Timeout: "20s",
OutputFormat: "json",
BroadcastMode: "block",
SignModeStr: "direct",
Modules: modules,
},
},
}
return cfg.MustYAML()
}
// initConfig reads in config file and ENV variables if set.
func initConfig(cmd *cobra.Command) error {
home, err := cmd.PersistentFlags().GetString(flags.FlagHome)
if err != nil {
return err
}
config = &Config{}
cfgPath := path.Join(home, "config.yaml")
if _, err := os.Stat(cfgPath); err == nil {
viper.SetConfigFile(cfgPath)
if err := viper.ReadInConfig(); err == nil {
// read the config file bytes
file, err := ioutil.ReadFile(viper.ConfigFileUsed())
if err != nil {
fmt.Println("Error reading file:", err)
os.Exit(1)
}
// unmarshall them into the struct
if err = yaml.Unmarshal(file, config); err != nil {
fmt.Println("Error unmarshalling config:", err)
os.Exit(1)
}
// validate configuration
if err = validateConfig(config); err != nil {
fmt.Println("Error parsing chain config:", err)
os.Exit(1)
}
// instantiate chain client
// TODO: this is a bit of a hack, we should probably have a
// better way to inject modules into the client
modules := []module.AppModuleBasic{}
for _, v := range simapp.ModuleBasics {
modules = append(modules, v)
}
ctx := cmd.Context()
for _, chain := range config.Chains {
chain.Modules = modules
cl, err := client.NewChainClient(chain, os.Stdin, os.Stdout)
if err != nil {
fmt.Println("Error creating chain client:", err)
os.Exit(1)
}
client.SetChainClientOnContext(ctx, chain.ChainID, cl)
}
}
}
return nil
}