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
use crate::{
account_commands::AccountCommand, client_proxy::ClientProxy, counters::COUNTER_CLIENT_ERRORS,
dev_commands::DevCommand, info_commands::InfoCommand, query_commands::QueryCommand,
transfer_commands::TransferCommand,
};
use anyhow::Error;
use diem_types::{account_address::AccountAddress, transaction::authenticator::AuthenticationKey};
use std::{collections::HashMap, sync::Arc};
pub fn report_error(msg: &str, e: Error) {
println!("[ERROR] {}: {}", msg, e);
COUNTER_CLIENT_ERRORS.inc();
}
pub fn blocking_cmd(cmd: &str) -> bool {
cmd.ends_with('b')
}
pub fn debug_format_cmd(cmd: &str) -> bool {
cmd.ends_with('?')
}
pub fn is_address(data: &str) -> bool {
hex::decode(data).map_or(false, |vec| vec.len() == AccountAddress::LENGTH)
}
pub fn is_authentication_key(data: &str) -> bool {
hex::decode(data).map_or(false, |vec| vec.len() == AuthenticationKey::LENGTH)
}
pub fn get_commands(
include_dev: bool,
) -> (
Vec<Arc<dyn Command>>,
HashMap<&'static str, Arc<dyn Command>>,
) {
let mut commands: Vec<Arc<dyn Command>> = vec![
Arc::new(AccountCommand {}),
Arc::new(QueryCommand {}),
Arc::new(TransferCommand {}),
Arc::new(InfoCommand {}),
];
if include_dev {
commands.push(Arc::new(DevCommand {}));
}
let mut alias_to_cmd = HashMap::new();
for command in &commands {
for alias in command.get_aliases() {
alias_to_cmd.insert(alias, Arc::clone(command));
}
}
(commands, alias_to_cmd)
}
pub fn parse_cmd(cmd_str: &str) -> Vec<&str> {
cmd_str.split_ascii_whitespace().collect()
}
pub fn print_subcommand_help(parent_command: &str, commands: &[Box<dyn Command>]) {
println!(
"usage: {} <arg>\n\nUse the following args for this command:\n",
parent_command
);
for cmd in commands {
println!(
"{} {}\n\t{}",
cmd.get_aliases().join(" | "),
cmd.get_params_help(),
cmd.get_description()
);
}
println!("\n");
}
pub fn subcommand_execute(
parent_command_name: &str,
commands: Vec<Box<dyn Command>>,
client: &mut ClientProxy,
params: &[&str],
) {
let mut commands_map = HashMap::new();
for (i, cmd) in commands.iter().enumerate() {
for alias in cmd.get_aliases() {
if commands_map.insert(alias, i) != None {
panic!("Duplicate alias {}", alias);
}
}
}
if params.is_empty() {
print_subcommand_help(parent_command_name, &commands);
return;
}
match commands_map.get(¶ms[0]) {
Some(&idx) => commands[idx].execute(client, params),
_ => print_subcommand_help(parent_command_name, &commands),
}
}
pub trait Command {
fn get_aliases(&self) -> Vec<&'static str>;
fn get_params_help(&self) -> &'static str {
""
}
fn get_description(&self) -> &'static str;
fn execute(&self, client: &mut ClientProxy, params: &[&str]);
}