-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchains.go
More file actions
177 lines (149 loc) · 4.21 KB
/
chains.go
File metadata and controls
177 lines (149 loc) · 4.21 KB
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
package cmd
import (
"fmt"
"strings"
"github.com/pelletier/go-toml/v2"
"github.com/spf13/cobra"
"github.com/bandprotocol/falcon/relayer"
)
// ChainsCmd returns a command that manages chain configurations.
func ChainsCmd(appCreator relayer.AppCreator, defaultHome string) *cobra.Command {
cmd := &cobra.Command{
Use: "chains",
Aliases: []string{"ch"},
Short: "Manage chain configurations",
}
cmd.AddCommand(
chainsAddCmd(appCreator, defaultHome),
chainsDeleteCmd(appCreator, defaultHome),
chainsListCmd(appCreator, defaultHome),
chainsShowCmd(appCreator, defaultHome),
)
return cmd
}
// chainsAddCmd returns a command that adds a new chain configuration.
func chainsAddCmd(appCreator relayer.AppCreator, defaultHome string) *cobra.Command {
cmd := &cobra.Command{
Use: "add [chain_name] [chain_config_path]",
Aliases: []string{"a"},
Short: "Add a new chain to the configuration file by passing a configuration file",
Args: withUsage(cobra.ExactArgs(2)),
Example: strings.TrimSpace(`
ch a evm chains/eth.toml
chains add evm chains/eth.toml`),
RunE: func(cmd *cobra.Command, args []string) error {
app, err := createApp(cmd, appCreator, defaultHome)
if err != nil {
return err
}
defer syncLog(app.GetLog())
if err := app.Init(cmd.Context()); err != nil {
return err
}
chainName := args[0]
filePath := args[1]
return app.AddChainConfig(chainName, filePath)
},
}
return cmd
}
// chainsDeleteCmd returns a command that deletes a chain configuration.
func chainsDeleteCmd(appCreator relayer.AppCreator, defaultHome string) *cobra.Command {
cmd := &cobra.Command{
Use: "delete [chain_name]",
Aliases: []string{"d"},
Short: "Remove chain from config based on chain_name",
Args: withUsage(cobra.ExactArgs(1)),
Example: strings.TrimSpace(`
ch delete eth
chains delete eth`),
RunE: func(cmd *cobra.Command, args []string) error {
app, err := createApp(cmd, appCreator, defaultHome)
if err != nil {
return err
}
defer syncLog(app.GetLog())
if err := app.Init(cmd.Context()); err != nil {
return err
}
chainName := args[0]
return app.DeleteChainConfig(chainName)
},
}
return cmd
}
// chainsListCmd returns a command that lists all chains that are currently configured.
// NOTE: this command only show the list of registered chainIDs, to see the configuration
// please see `chains show`.
func chainsListCmd(appCreator relayer.AppCreator, defaultHome string) *cobra.Command {
cmd := &cobra.Command{
Use: "list",
Aliases: []string{"l"},
Short: "Return list of chain names that are currently configured",
Args: withUsage(cobra.NoArgs),
Example: strings.TrimSpace(`
ch list
chains list`),
RunE: func(cmd *cobra.Command, args []string) error {
app, err := createApp(cmd, appCreator, defaultHome)
if err != nil {
return err
}
defer syncLog(app.GetLog())
if err := app.Init(cmd.Context()); err != nil {
return err
}
cfg := app.GetConfig()
if cfg == nil {
return fmt.Errorf("config is not initialized")
}
i := 1
for chainName, chainProviderConfig := range cfg.TargetChains {
fmt.Fprintf(
cmd.OutOrStdout(),
"%d: %s -> type(%v)\n",
i,
chainName,
chainProviderConfig.GetChainType(),
)
i++
}
return nil
},
}
return cmd
}
// chainsShowCmd returns a command that shows a chain's configuration data.
func chainsShowCmd(appCreator relayer.AppCreator, defaultHome string) *cobra.Command {
cmd := &cobra.Command{
Use: "show [chain_name]",
Aliases: []string{"s"},
Short: "Return chain's configuration data",
Args: withUsage(cobra.ExactArgs(1)),
Example: strings.TrimSpace(`
ch s eth
chains show eth --yaml`),
RunE: func(cmd *cobra.Command, args []string) error {
app, err := createApp(cmd, appCreator, defaultHome)
if err != nil {
return err
}
defer syncLog(app.GetLog())
if err := app.Init(cmd.Context()); err != nil {
return err
}
chainName := args[0]
chainConfig, err := app.GetChainConfig(chainName)
if err != nil {
return err
}
b, err := toml.Marshal(chainConfig)
if err != nil {
return err
}
fmt.Fprintln(cmd.OutOrStdout(), string(b))
return nil
},
}
return cmd
}