Skip to content

Commit

Permalink
refactor: Move key-info to util sub-command (support output old address)
Browse files Browse the repository at this point in the history
  • Loading branch information
TheWaWaR committed Jul 24, 2019
1 parent 86cdcad commit 39b8c9d
Show file tree
Hide file tree
Showing 8 changed files with 275 additions and 178 deletions.
5 changes: 5 additions & 0 deletions ckb-sdk/src/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,11 @@ mod old_addr {
}

impl Address {
pub fn new_default(hash: H160) -> Address {
let format = AddressFormat::P2PH;
Address { format, hash }
}

pub fn hash(&self) -> &H160 {
&self.hash
}
Expand Down
227 changes: 118 additions & 109 deletions src/interactive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use serde_json::json;

use crate::subcommands::{
AccountSubCommand, CliSubCommand, IndexController, IndexRequest, MockTxSubCommand,
RpcSubCommand, WalletSubCommand,
RpcSubCommand, UtilSubCommand, WalletSubCommand,
};
use crate::utils::{
completer::CkbCompleter,
Expand Down Expand Up @@ -231,120 +231,129 @@ impl InteractiveEnv {
let format = self.config.output_format();
let color = ColorWhen::new(self.config.color()).color();
match self.parser.clone().get_matches_from_safe(args) {
Ok(matches) => match matches.subcommand() {
("config", Some(m)) => {
m.value_of("url").and_then(|url| {
let index_sender = self.index_controller.sender();
Request::call(index_sender, IndexRequest::UpdateUrl(url.to_string()));
self.config.set_url(url.to_string());
self.rpc_client = HttpRpcClient::from_uri(self.config.get_url());
self.genesis_info = None;
Some(())
});
if m.is_present("color") {
self.config.switch_color();
}
Ok(matches) => {
match matches.subcommand() {
("config", Some(m)) => {
m.value_of("url").and_then(|url| {
let index_sender = self.index_controller.sender();
Request::call(index_sender, IndexRequest::UpdateUrl(url.to_string()));
self.config.set_url(url.to_string());
self.rpc_client = HttpRpcClient::from_uri(self.config.get_url());
self.genesis_info = None;
Some(())
});
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 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("debug") {
self.config.switch_debug();
}

if m.is_present("edit_style") {
self.config.switch_edit_style();
}
if m.is_present("edit_style") {
self.config.switch_edit_style();
}

if m.is_present("completion_style") {
self.config.switch_completion_style();
}
if m.is_present("completion_style") {
self.config.switch_completion_style();
}

self.config.print();
let mut file = fs::File::create(self.config_file.as_path())
.map_err(|err| format!("open config error: {:?}", err))?;
let content = serde_json::to_string_pretty(&json!({
"url": self.config.get_url().to_string(),
"color": self.config.color(),
"debug": self.config.debug(),
"output_format": self.config.output_format().to_string(),
"completion_style": self.config.completion_style(),
"edit_style": self.config.edit_style(),
}))
.unwrap();
file.write_all(content.as_bytes())
.map_err(|err| format!("save config error: {:?}", 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();
Ok(())
}
("rpc", Some(sub_matches)) => {
check_alerts(&mut self.rpc_client);
let output = RpcSubCommand::new(&mut self.rpc_client).process(
&sub_matches,
format,
color,
)?;
println!("{}", output);
Ok(())
}
("account", Some(sub_matches)) => {
let genesis_info = self.genesis_info().ok();
let output = AccountSubCommand::new(
&mut self.rpc_client,
&mut self.key_store,
genesis_info,
)
.process(&sub_matches, format, color)?;
println!("{}", output);
Ok(())
}
("mock-tx", Some(sub_matches)) => {
let genesis_info = self.genesis_info().ok();
let output = MockTxSubCommand::new(
&mut self.rpc_client,
&mut self.key_store,
genesis_info,
)
.process(&sub_matches, format, color)?;
println!("{}", output);
Ok(())
}
("wallet", Some(sub_matches)) => {
let genesis_info = self.genesis_info()?;
let output = WalletSubCommand::new(
&mut self.rpc_client,
&mut self.key_store,
Some(genesis_info),
self.index_dir.clone(),
self.index_controller.clone(),
true,
)
.process(&sub_matches, format, color)?;
println!("{}", output);
Ok(())
}
("exit", _) => {
return Ok(true);
self.config.print();
let mut file = fs::File::create(self.config_file.as_path())
.map_err(|err| format!("open config error: {:?}", err))?;
let content = serde_json::to_string_pretty(&json!({
"url": self.config.get_url().to_string(),
"color": self.config.color(),
"debug": self.config.debug(),
"output_format": self.config.output_format().to_string(),
"completion_style": self.config.completion_style(),
"edit_style": self.config.edit_style(),
}))
.unwrap();
file.write_all(content.as_bytes())
.map_err(|err| format!("save config error: {:?}", 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();
Ok(())
}
("rpc", Some(sub_matches)) => {
check_alerts(&mut self.rpc_client);
let output = RpcSubCommand::new(&mut self.rpc_client).process(
&sub_matches,
format,
color,
)?;
println!("{}", output);
Ok(())
}
("account", Some(sub_matches)) => {
let genesis_info = self.genesis_info().ok();
let output = AccountSubCommand::new(
&mut self.rpc_client,
&mut self.key_store,
genesis_info,
)
.process(&sub_matches, format, color)?;
println!("{}", output);
Ok(())
}
("mock-tx", Some(sub_matches)) => {
let genesis_info = self.genesis_info().ok();
let output = MockTxSubCommand::new(
&mut self.rpc_client,
&mut self.key_store,
genesis_info,
)
.process(&sub_matches, format, color)?;
println!("{}", output);
Ok(())
}
("util", Some(sub_matches)) => {
let genesis_info = self.genesis_info().ok();
let output = UtilSubCommand::new(&mut self.rpc_client, genesis_info)
.process(&sub_matches, format, color)?;
println!("{}", output);
Ok(())
}
("wallet", Some(sub_matches)) => {
let genesis_info = self.genesis_info()?;
let output = WalletSubCommand::new(
&mut self.rpc_client,
&mut self.key_store,
Some(genesis_info),
self.index_dir.clone(),
self.index_controller.clone(),
true,
)
.process(&sub_matches, format, color)?;
println!("{}", output);
Ok(())
}
("exit", _) => {
return Ok(true);
}
_ => Ok(()),
}
_ => Ok(()),
},
}
Err(err) => Err(err.to_string()),
}
.map(|_| false)
Expand Down
7 changes: 6 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use subcommands::TuiSubCommand;
use interactive::InteractiveEnv;
use subcommands::{
start_index_thread, AccountSubCommand, CliSubCommand, IndexThreadState, MockTxSubCommand,
RpcSubCommand, WalletSubCommand,
RpcSubCommand, UtilSubCommand, WalletSubCommand,
};
use utils::{
arg_parser::{ArgParser, UrlParser},
Expand Down Expand Up @@ -114,6 +114,9 @@ fn main() -> Result<(), io::Error> {
color,
)
}),
("util", Some(sub_matches)) => {
UtilSubCommand::new(&mut rpc_client, None).process(&sub_matches, output_format, color)
}
("wallet", Some(sub_matches)) => get_key_store(&ckb_cli_dir).and_then(|mut key_store| {
WalletSubCommand::new(
&mut rpc_client,
Expand Down Expand Up @@ -196,6 +199,7 @@ pub fn build_cli<'a>(version_short: &'a str, version_long: &'a str) -> App<'a, '
.subcommand(RpcSubCommand::subcommand())
.subcommand(AccountSubCommand::subcommand("account"))
.subcommand(MockTxSubCommand::subcommand("mock-tx"))
.subcommand(UtilSubCommand::subcommand("util"))
.subcommand(WalletSubCommand::subcommand())
.arg(
Arg::with_name("url")
Expand Down Expand Up @@ -287,5 +291,6 @@ pub fn build_interactive() -> App<'static, 'static> {
.subcommand(RpcSubCommand::subcommand())
.subcommand(AccountSubCommand::subcommand("account"))
.subcommand(MockTxSubCommand::subcommand("mock-tx"))
.subcommand(UtilSubCommand::subcommand("util"))
.subcommand(WalletSubCommand::subcommand())
}
2 changes: 2 additions & 0 deletions src/subcommands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pub mod mock_tx;
pub mod rpc;
#[cfg(unix)]
pub mod tui;
pub mod util;
pub mod wallet;

#[cfg(unix)]
Expand All @@ -11,6 +12,7 @@ pub use self::tui::TuiSubCommand;
pub use account::AccountSubCommand;
pub use mock_tx::MockTxSubCommand;
pub use rpc::RpcSubCommand;
pub use util::UtilSubCommand;
pub use wallet::{
start_index_thread, IndexController, IndexRequest, IndexResponse, IndexThreadState,
WalletSubCommand,
Expand Down
Loading

0 comments on commit 39b8c9d

Please sign in to comment.