-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathinteractive.rs
408 lines (381 loc) · 15.1 KB
/
interactive.rs
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
use std::fs;
use std::path::PathBuf;
use std::time::{Duration, Instant};
use ansi_term::Colour::Green;
use regex::Regex;
use rustyline::config::Configurer;
use rustyline::error::ReadlineError;
use rustyline::history::FileHistory;
use rustyline::{Cmd, CompletionType, Config, EditMode, Editor, KeyEvent};
use serde_json::json;
use ckb_signer::KeyStore;
use crate::plugin::PluginManager;
use crate::subcommands::{
AccountSubCommand, CliSubCommand, DAOSubCommand, DeploySubCommand, MockTxSubCommand,
MoleculeSubCommand, PluginSubCommand, RpcSubCommand, SudtSubCommand, TxSubCommand,
UtilSubCommand, WalletSubCommand,
};
use crate::utils::{
completer::CkbCompleter,
config::GlobalConfig,
genesis_info::GenesisInfo,
other::{check_alerts, get_genesis_info, get_network_type},
printer::{ColorWhen, OutputFormat, Printable},
rpc::{HttpRpcClient, RawHttpRpcClient},
};
const ENV_PATTERN: &str = r"\$\{\s*(?P<key>\S+)\s*\}";
/// Interactive command line
pub struct InteractiveEnv {
config: GlobalConfig,
config_file: PathBuf,
history_file: PathBuf,
parser: clap::App<'static>,
plugin_mgr: PluginManager,
key_store: KeyStore,
rpc_client: HttpRpcClient,
raw_rpc_client: RawHttpRpcClient,
genesis_info: Option<GenesisInfo>,
}
impl InteractiveEnv {
pub fn from_config(
ckb_cli_dir: PathBuf,
mut config: GlobalConfig,
plugin_mgr: PluginManager,
key_store: KeyStore,
) -> Result<InteractiveEnv, String> {
if !ckb_cli_dir.as_path().exists() {
fs::create_dir(&ckb_cli_dir).map_err(|err| err.to_string())?;
}
let mut history_file = ckb_cli_dir.clone();
history_file.push("history");
let mut config_file = ckb_cli_dir.clone();
config_file.push("config");
let mut env_file = ckb_cli_dir;
env_file.push("env_vars");
if env_file.as_path().exists() {
let file = fs::File::open(&env_file).map_err(|err| err.to_string())?;
let env_vars_json = serde_json::from_reader(file).unwrap_or(json!(null));
match env_vars_json {
serde_json::Value::Object(env_vars) => config.add_env_vars(env_vars),
_ => eprintln!("Parse environment variable file failed."),
}
}
let parser = crate::build_interactive();
let rpc_client = HttpRpcClient::new(config.get_url().to_string());
let raw_rpc_client = RawHttpRpcClient::new(config.get_url());
Ok(InteractiveEnv {
config,
config_file,
history_file,
parser,
plugin_mgr,
key_store,
rpc_client,
raw_rpc_client,
genesis_info: None,
})
}
pub fn start(&mut self) -> Result<(), String> {
self.print_logo();
self.config.print(true);
let env_regex = Regex::new(ENV_PATTERN).unwrap();
let prompt = {
#[cfg(unix)]
{
use ansi_term::Colour::Blue;
Blue.bold().paint("CKB> ").to_string()
}
#[cfg(not(unix))]
{
"CKB> ".to_string()
}
};
let rl_mode =
|rl: &mut Editor<CkbCompleter, FileHistory>, is_list: bool, is_emacs: bool| {
if is_list {
rl.set_completion_type(CompletionType::List)
} else {
rl.set_completion_type(CompletionType::Circular)
}
if is_emacs {
rl.set_edit_mode(EditMode::Emacs)
} else {
rl.set_edit_mode(EditMode::Vi)
}
};
let mut plugin_sub_cmds = Vec::new();
let mut parser = self.parser.clone();
for (cmd_name, plugin_name) in self.plugin_mgr.sub_commands() {
if let Some((_, config)) = self.plugin_mgr.plugins().get(plugin_name.as_str()) {
plugin_sub_cmds
.push((cmd_name.clone(), format!("[plugin] {}", config.description)));
}
}
for (cmd_name, description) in &plugin_sub_cmds {
parser = parser.subcommand(
// FIXME: when clap updated add `clap::AppSettings::DisableHelpFlags` back
clap::App::new(cmd_name.as_str()).about(description.as_str()),
);
}
let rl_config = Config::builder()
.history_ignore_space(true)
.max_history_size(1000)
.map_err(|err| err.to_string())?
.build();
let mut rl = Editor::with_config(rl_config).map_err(|err| err.to_string())?;
let helper = CkbCompleter::new(parser.clone());
rl.set_helper(Some(helper));
rl.bind_sequence(KeyEvent::alt('n'), Cmd::HistorySearchForward);
rl.bind_sequence(KeyEvent::alt('p'), Cmd::HistorySearchBackward);
if rl.load_history(&self.history_file).is_err() {
eprintln!("No previous history.");
}
let mut last_save_history = Instant::now();
loop {
rl_mode(
&mut rl,
self.config.completion_style(),
self.config.edit_style(),
);
match rl.readline(&prompt) {
Ok(line) => {
match self.handle_command(&parser, line.as_str(), &env_regex) {
Ok(true) => {
break;
}
Ok(false) => {}
Err(err) => {
eprintln!("{}", err);
}
}
let _ = rl.add_history_entry(line.as_str());
}
Err(ReadlineError::Interrupted) => {
println!("CTRL-C");
}
Err(ReadlineError::Eof) => {
println!("CTRL-D");
break;
}
Err(err) => {
eprintln!("Error: {:?}", err);
break;
}
}
if last_save_history.elapsed() >= Duration::from_secs(120) {
if let Err(err) = rl.save_history(&self.history_file) {
eprintln!("Save command history failed: {}", err);
break;
}
last_save_history = Instant::now();
}
}
if let Err(err) = rl.save_history(&self.history_file) {
eprintln!("Save command history failed: {}", err);
}
Ok(())
}
fn print_logo(&mut self) {
println!(
r#"
_ _ ______ _____ __ __ {} _____
| \ | | | ____| | __ \ \ \ / / {} / ____|
| \| | | |__ | |__) | \ \ / / {} | (___
| . ` | | __| | _ / \ \/ / {} \___ \
| |\ | | |____ | | \ \ \ / {} ____) |
|_| \_| |______| |_| \_\ \/ {} |_____/
"#,
Green.bold().paint(r#" ____ "#),
Green.bold().paint(r#" / __ \ "#),
Green.bold().paint(r#"| | | |"#),
Green.bold().paint(r#"| | | |"#),
Green.bold().paint(r#"| |__| |"#),
Green.bold().paint(r#" \____/ "#),
);
}
fn genesis_info(&mut self) -> Result<GenesisInfo, String> {
self.genesis_info = Some(get_genesis_info(&self.genesis_info, &mut self.rpc_client)?);
Ok(self.genesis_info.clone().unwrap())
}
fn handle_command(
&mut self,
parser: &clap::App,
line: &str,
env_regex: &Regex,
) -> Result<bool, String> {
let args = match shell_words::split(self.config.replace_cmd(env_regex, line).as_str()) {
Ok(args) => args,
Err(e) => return Err(e.to_string()),
};
if args.is_empty() {
return Ok(false);
}
let format = self.config.output_format();
let color = ColorWhen::new(self.config.color()).color();
let debug = self.config.debug();
let current_cmd_name = &args[0];
if self
.plugin_mgr
.sub_commands()
.contains_key(current_cmd_name.as_str())
{
let rest_args = line[current_cmd_name.len()..].to_string();
log::debug!("[call sub command]: {} {}", current_cmd_name, rest_args);
let resp = self
.plugin_mgr
.sub_command(current_cmd_name.as_str(), rest_args)?;
println!("{}", resp.render(format, color));
return Ok(false);
}
match parser.clone().try_get_matches_from(args) {
Ok(matches) => match matches.subcommand() {
("config", Some(m)) => {
if let Some(url) = m.value_of("url") {
self.config.set_url(url.to_string());
self.rpc_client = HttpRpcClient::new(self.config.get_url().to_string());
self.raw_rpc_client = RawHttpRpcClient::new(self.config.get_url());
self.config
.set_network(get_network_type(&mut self.rpc_client).ok());
self.genesis_info = None;
};
if m.is_present("color") {
self.config.switch_color();
}
if let Some(format) = m.value_of("output-format") {
let output_format =
OutputFormat::from_str(format).unwrap_or(OutputFormat::Yaml);
self.config.set_output_format(output_format);
}
if m.is_present("debug") {
self.config.switch_debug();
}
if m.is_present("edit_style") {
self.config.switch_edit_style();
}
if m.is_present("completion_style") {
self.config.switch_completion_style();
}
self.config.print(false);
self.config
.save(self.config_file.as_path())
.map_err(|err| format!("save config file failed: {:?}", err))?;
Ok(())
}
("set", Some(m)) => {
let key = m.value_of("key").unwrap().to_owned();
let value = m.value_of("value").unwrap().to_owned();
self.config.set(key, serde_json::Value::String(value));
Ok(())
}
("get", Some(m)) => {
let key = m.value_of("key");
println!("{}", self.config.get(key).render(format, color));
Ok(())
}
("info", _) => {
self.config.print(false);
Ok(())
}
("rpc", Some(sub_matches)) => {
check_alerts(&mut self.rpc_client);
let output = RpcSubCommand::new(&mut self.rpc_client, &mut self.raw_rpc_client)
.process(sub_matches, debug)?;
output.print(format, color);
Ok(())
}
("account", Some(sub_matches)) => {
let output = AccountSubCommand::new(&mut self.plugin_mgr, &mut self.key_store)
.process(sub_matches, debug)?;
output.print(format, color);
Ok(())
}
("mock-tx", Some(sub_matches)) => {
let genesis_info = self.genesis_info().ok();
let output = MockTxSubCommand::new(
&mut self.rpc_client,
&mut self.plugin_mgr,
genesis_info,
)
.process(sub_matches, debug)?;
output.print(format, color);
Ok(())
}
("tx", Some(sub_matches)) => {
let genesis_info = self.genesis_info().ok();
let output =
TxSubCommand::new(&mut self.rpc_client, &mut self.plugin_mgr, genesis_info)
.process(sub_matches, debug)?;
output.print(format, color);
Ok(())
}
("util", Some(sub_matches)) => {
let output = UtilSubCommand::new(&mut self.rpc_client, &mut self.plugin_mgr)
.process(sub_matches, debug)?;
output.print(format, color);
Ok(())
}
("plugin", Some(sub_matches)) => {
let output =
PluginSubCommand::new(&mut self.plugin_mgr).process(sub_matches, debug)?;
output.print(format, color);
Ok(())
}
("molecule", Some(sub_matches)) => {
let output = MoleculeSubCommand::new().process(sub_matches, debug)?;
output.print(format, color);
Ok(())
}
("wallet", Some(sub_matches)) => {
let genesis_info = self.genesis_info()?;
let output = WalletSubCommand::new(
&mut self.rpc_client,
&mut self.plugin_mgr,
Some(genesis_info),
)
.process(sub_matches, debug)?;
output.print(format, color);
Ok(())
}
("dao", Some(sub_matches)) => {
let genesis_info = self.genesis_info()?;
let output = DAOSubCommand::new(
&mut self.rpc_client,
&mut self.plugin_mgr,
genesis_info,
)
.process(sub_matches, debug)?;
output.print(format, color);
Ok(())
}
("sudt", Some(sub_matches)) => {
let genesis_info = self.genesis_info()?;
let output = SudtSubCommand::new(
&mut self.rpc_client,
&mut self.plugin_mgr,
genesis_info,
)
.process(sub_matches, debug)?;
output.print(format, color);
Ok(())
}
("deploy", Some(sub_matches)) => {
let genesis_info = self.genesis_info()?;
let output = DeploySubCommand::new(
&mut self.rpc_client,
&mut self.plugin_mgr,
genesis_info,
)
.process(sub_matches, debug)?;
output.print(format, color);
Ok(())
}
("exit", _) => {
return Ok(true);
}
_ => Ok(()),
},
Err(err) => Err(err.to_string()),
}
.map(|_| false)
}
}