-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.go
More file actions
189 lines (164 loc) · 4.71 KB
/
cli.go
File metadata and controls
189 lines (164 loc) · 4.71 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
178
179
180
181
182
183
184
185
186
187
188
189
package cli
import (
"context"
"github.com/LumeraProtocol/supernode/v2/supernode/config"
"log"
"os"
"path/filepath"
"strings"
"github.com/BurntSushi/toml"
"github.com/spf13/pflag"
snkeyring "github.com/LumeraProtocol/supernode/v2/pkg/keyring"
"github.com/LumeraProtocol/supernode/v2/pkg/net/credentials/alts/conn"
"github.com/LumeraProtocol/supernode/v2/sdk/adapters/lumera"
sdkcfg "github.com/LumeraProtocol/supernode/v2/sdk/config"
sdklog "github.com/LumeraProtocol/supernode/v2/sdk/log"
sdknet "github.com/LumeraProtocol/supernode/v2/sdk/net"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
)
const (
defaultConfigFileName = "config.toml"
)
type CLI struct {
opts CLIOptions
cfg *CLIConfig
kr keyring.Keyring
sdkConfig sdkcfg.Config
lumeraClient lumera.Client
snClient sdknet.SupernodeClient
}
func (c *CLI) parseCLIOptions() {
pflag.StringVar(&c.opts.ConfigPath, "config", "", "Path to config file")
pflag.StringVar(&c.opts.GrpcEndpoint, "grpc_endpoint", "", "Supernode gRPC endpoint")
pflag.StringVar(&c.opts.SupernodeAddr, "address", "", "Supernode Lumera address")
pflag.Parse()
args := pflag.Args()
if len(args) > 0 {
c.opts.Command = args[0]
c.opts.CommandArgs = args[1:]
}
}
func NewCLI() *CLI {
cli := &CLI{}
return cli
}
func processConfigPath(path string) string {
// expand environment variables if any
path = os.ExpandEnv(path)
// replaces ~ with the user's home directory
if strings.HasPrefix(path, "~") {
home, err := os.UserHomeDir()
if err != nil {
log.Fatalf("Unable to resolve home directory: %v", err)
}
path = filepath.Join(home, path[1:])
}
// check if path defines directory
if info, err := os.Stat(path); err == nil && info.IsDir() {
path = filepath.Join(path, defaultConfigFileName)
}
path = filepath.Clean(path)
return path
}
// detectConfigPath resolves the configuration file path based on:
// 1. CLI argument (--config) if provided
// 2. SNCLI_CONFIG_PATH environment variable
// 3. Default to ./config.toml
func (c *CLI) detectConfigPath() string {
if c.opts.ConfigPath != "" {
return processConfigPath(c.opts.ConfigPath)
}
if envPath := os.Getenv("SNCLI_CONFIG_PATH"); envPath != "" {
return processConfigPath(envPath)
}
return defaultConfigFileName
}
func (c *CLI) loadCLIConfig() {
path := c.detectConfigPath()
_, err := toml.DecodeFile(path, &c.cfg)
if err != nil {
log.Fatalf("Failed to load config from %s: %v", path, err)
}
}
func (c *CLI) validateCLIConfig() {
if c.opts.GrpcEndpoint != "" {
c.cfg.Supernode.GRPCEndpoint = c.opts.GrpcEndpoint
}
if c.opts.SupernodeAddr != "" {
c.cfg.Supernode.Address = c.opts.SupernodeAddr
}
}
func (c *CLI) Initialize() {
// Parse command-line options
c.parseCLIOptions()
// Load options from toml configuration file
c.loadCLIConfig()
// Validate configuration & override with CLI options if provided
c.validateCLIConfig()
// Initialize Supernode SDK
snkeyring.InitSDKConfig()
// Initialize keyring
var err error
c.kr, err = snkeyring.InitKeyring(config.KeyringConfig{
Backend: c.cfg.Keyring.Backend,
Dir: c.cfg.Keyring.Dir,
})
if err != nil {
log.Fatalf("Keyring init failed: %v", err)
}
// Create Lumera client adapter
c.sdkConfig = sdkcfg.NewConfig(
sdkcfg.AccountConfig{
LocalCosmosAddress: c.cfg.Keyring.LocalAddress,
KeyName: c.cfg.Keyring.KeyName,
Keyring: c.kr,
},
sdkcfg.LumeraConfig{
GRPCAddr: c.cfg.Lumera.GRPCAddr,
ChainID: c.cfg.Lumera.ChainID,
},
)
c.lumeraClient, err = lumera.NewAdapter(context.Background(), lumera.ConfigParams{
GRPCAddr: c.sdkConfig.Lumera.GRPCAddr,
ChainID: c.sdkConfig.Lumera.ChainID,
KeyName: c.sdkConfig.Account.KeyName,
Keyring: c.kr,
}, sdklog.NewNoopLogger())
if err != nil {
log.Fatalf("Lumera client init failed: %v", err)
}
conn.RegisterALTSRecordProtocols()
}
func (c *CLI) Finalize() {
conn.UnregisterALTSRecordProtocols()
if c.snClient != nil {
c.snClient.Close(context.Background())
}
}
func (c *CLI) snClientInit() {
if c.snClient != nil {
return // Already initialized
}
if c.cfg.Supernode.Address == "" || c.cfg.Supernode.GRPCEndpoint == "" {
log.Fatal("Supernode address and gRPC endpoint must be configured")
}
supernode := lumera.Supernode{
CosmosAddress: c.cfg.Supernode.Address,
GrpcEndpoint: c.cfg.Supernode.GRPCEndpoint,
}
clientFactory := sdknet.NewClientFactory(
context.Background(),
sdklog.NewNoopLogger(),
c.kr,
c.lumeraClient,
sdknet.FactoryConfig{
LocalCosmosAddress: c.cfg.Keyring.LocalAddress,
PeerType: 1, // Simplenode
},
)
var err error
c.snClient, err = clientFactory.CreateClient(context.Background(), supernode)
if err != nil {
log.Fatalf("Supernode client init failed: %v", err)
}
}