From f212ae6322ce7e6742f3df02de46414b9fb88cc9 Mon Sep 17 00:00:00 2001 From: Nicolas Ochem Date: Fri, 28 Jul 2017 10:07:38 -0700 Subject: [PATCH] fix #6052. honor --no-color for signer command (#6100) * fix #6052. honor --no-color for signer command * replace call to paint closure with the body, apply to generate_new_token too --- parity/configuration.rs | 10 +++++++--- parity/main.rs | 2 +- parity/run.rs | 12 ++++++------ parity/signer.rs | 30 +++++++++++++++++++----------- 4 files changed, 33 insertions(+), 21 deletions(-) diff --git a/parity/configuration.rs b/parity/configuration.rs index f1399e47c..d05f26f53 100644 --- a/parity/configuration.rs +++ b/parity/configuration.rs @@ -57,7 +57,7 @@ pub enum Cmd { Account(AccountCmd), ImportPresaleWallet(ImportWallet), Blockchain(BlockchainCmd), - SignerToken(WsConfiguration, UiConfiguration), + SignerToken(WsConfiguration, UiConfiguration, LogConfig), SignerSign { id: Option, pwfile: Option, @@ -149,7 +149,7 @@ impl Configuration { let authfile = ::signer::codes_path(&ws_conf.signer_path); if self.args.cmd_new_token { - Cmd::SignerToken(ws_conf, ui_conf) + Cmd::SignerToken(ws_conf, ui_conf, logger_config.clone()) } else if self.args.cmd_sign { let pwfile = self.args.flag_password.get(0).map(|pwfile| { PathBuf::from(pwfile) @@ -1285,7 +1285,11 @@ mod tests { interface: "127.0.0.1".into(), port: 8180, hosts: Some(vec![]), - })); + }, LogConfig { + color: true, + mode: None, + file: None, + } )); } #[test] diff --git a/parity/main.rs b/parity/main.rs index 311ce8b35..48e752bf2 100644 --- a/parity/main.rs +++ b/parity/main.rs @@ -164,7 +164,7 @@ fn execute(command: Execute, can_restart: bool) -> Result account::execute(account_cmd).map(|s| PostExecutionAction::Print(s)), Cmd::ImportPresaleWallet(presale_cmd) => presale::execute(presale_cmd).map(|s| PostExecutionAction::Print(s)), Cmd::Blockchain(blockchain_cmd) => blockchain::execute(blockchain_cmd).map(|_| PostExecutionAction::Quit), - Cmd::SignerToken(ws_conf, ui_conf) => signer::execute(ws_conf, ui_conf).map(|s| PostExecutionAction::Print(s)), + Cmd::SignerToken(ws_conf, ui_conf, logger_config) => signer::execute(ws_conf, ui_conf, logger_config).map(|s| PostExecutionAction::Print(s)), Cmd::SignerSign { id, pwfile, port, authfile } => rpc_cli::signer_sign(id, pwfile, port, authfile).map(|s| PostExecutionAction::Print(s)), Cmd::SignerList { port, authfile } => rpc_cli::signer_list(port, authfile).map(|s| PostExecutionAction::Print(s)), Cmd::SignerReject { id, port, authfile } => rpc_cli::signer_reject(id, port, authfile).map(|s| PostExecutionAction::Print(s)), diff --git a/parity/run.rs b/parity/run.rs index 7c835aa3f..c08294a42 100644 --- a/parity/run.rs +++ b/parity/run.rs @@ -118,12 +118,12 @@ pub struct RunCmd { pub whisper: ::whisper::Config } -pub fn open_ui(ws_conf: &rpc::WsConfiguration, ui_conf: &rpc::UiConfiguration) -> Result<(), String> { +pub fn open_ui(ws_conf: &rpc::WsConfiguration, ui_conf: &rpc::UiConfiguration, logger_config: &LogConfig) -> Result<(), String> { if !ui_conf.enabled { return Err("Cannot use UI command with UI turned off.".into()) } - let token = signer::generate_token_and_url(ws_conf, ui_conf)?; + let token = signer::generate_token_and_url(ws_conf, ui_conf, logger_config)?; // Open a browser url::open(&token.url); // Print a message @@ -282,7 +282,7 @@ fn execute_light(cmd: RunCmd, can_restart: bool, logger: Arc) -> let rpc_stats = Arc::new(informant::RpcStats::default()); // the dapps server - let signer_service = Arc::new(signer::new_service(&cmd.ws_conf, &cmd.ui_conf)); + let signer_service = Arc::new(signer::new_service(&cmd.ws_conf, &cmd.ui_conf, &cmd.logger_config)); let dapps_deps = { let contract_client = Arc::new(::dapps::LightRegistrar { client: service.client().clone(), @@ -378,7 +378,7 @@ pub fn execute(cmd: RunCmd, can_restart: bool, logger: Arc) -> R // Check if Parity is already running let addr = format!("{}:{}", cmd.ui_conf.interface, cmd.ui_conf.port); if !TcpListener::bind(&addr as &str).is_ok() { - return open_ui(&cmd.ws_conf, &cmd.ui_conf).map(|_| (false, None)); + return open_ui(&cmd.ws_conf, &cmd.ui_conf, &cmd.logger_config).map(|_| (false, None)); } } @@ -658,7 +658,7 @@ pub fn execute(cmd: RunCmd, can_restart: bool, logger: Arc) -> R false => Some(account_provider.clone()) }; - let signer_service = Arc::new(signer::new_service(&cmd.ws_conf, &cmd.ui_conf)); + let signer_service = Arc::new(signer::new_service(&cmd.ws_conf, &cmd.ui_conf, &cmd.logger_config)); // the dapps server let dapps_deps = { @@ -791,7 +791,7 @@ pub fn execute(cmd: RunCmd, can_restart: bool, logger: Arc) -> R // start ui if cmd.ui { - open_ui(&cmd.ws_conf, &cmd.ui_conf)?; + open_ui(&cmd.ws_conf, &cmd.ui_conf, &cmd.logger_config)?; } if let Some(dapp) = cmd.dapp { diff --git a/parity/signer.rs b/parity/signer.rs index 7a5e90341..8bdf18bd0 100644 --- a/parity/signer.rs +++ b/parity/signer.rs @@ -17,13 +17,13 @@ use std::io; use std::path::{Path, PathBuf}; -use ansi_term::Colour; +use ansi_term::Colour::White; +use ethcore_logger::Config as LogConfig; use rpc; use rpc_apis; use parity_rpc; use path::restrict_permissions_owner; - pub const CODES_FILENAME: &'static str = "authcodes"; pub struct NewToken { @@ -32,12 +32,13 @@ pub struct NewToken { pub message: String, } -pub fn new_service(ws_conf: &rpc::WsConfiguration, ui_conf: &rpc::UiConfiguration) -> rpc_apis::SignerService { +pub fn new_service(ws_conf: &rpc::WsConfiguration, ui_conf: &rpc::UiConfiguration, logger_config: &LogConfig) -> rpc_apis::SignerService { let signer_path = ws_conf.signer_path.clone(); + let logger_config_color = logger_config.color; let signer_enabled = ui_conf.enabled; rpc_apis::SignerService::new(move || { - generate_new_token(&signer_path).map_err(|e| format!("{:?}", e)) + generate_new_token(&signer_path, logger_config_color).map_err(|e| format!("{:?}", e)) }, signer_enabled) } @@ -48,13 +49,14 @@ pub fn codes_path(path: &Path) -> PathBuf { p } -pub fn execute(ws_conf: rpc::WsConfiguration, ui_conf: rpc::UiConfiguration) -> Result { - Ok(generate_token_and_url(&ws_conf, &ui_conf)?.message) +pub fn execute(ws_conf: rpc::WsConfiguration, ui_conf: rpc::UiConfiguration, logger_config: LogConfig) -> Result { + Ok(generate_token_and_url(&ws_conf, &ui_conf, &logger_config)?.message) } -pub fn generate_token_and_url(ws_conf: &rpc::WsConfiguration, ui_conf: &rpc::UiConfiguration) -> Result { - let code = generate_new_token(&ws_conf.signer_path).map_err(|err| format!("Error generating token: {:?}", err))?; +pub fn generate_token_and_url(ws_conf: &rpc::WsConfiguration, ui_conf: &rpc::UiConfiguration, logger_config: &LogConfig) -> Result { + let code = generate_new_token(&ws_conf.signer_path, logger_config.color).map_err(|err| format!("Error generating token: {:?}", err))?; let auth_url = format!("http://{}:{}/#/auth?token={}", ui_conf.interface, ui_conf.port, code); + // And print in to the console Ok(NewToken { token: code.clone(), @@ -65,18 +67,24 @@ Open: {} to authorize your browser. Or use the generated token: {}"#, - Colour::White.bold().paint(auth_url), + match logger_config.color { + true => format!("{}", White.bold().paint(auth_url)), + false => auth_url + }, code ) }) } -fn generate_new_token(path: &Path) -> io::Result { +fn generate_new_token(path: &Path, logger_config_color: bool) -> io::Result { let path = codes_path(path); let mut codes = parity_rpc::AuthCodes::from_file(&path)?; codes.clear_garbage(); let code = codes.generate_new()?; codes.to_file(&path)?; - trace!("New key code created: {}", Colour::White.bold().paint(&code[..])); + trace!("New key code created: {}", match logger_config_color { + true => format!("{}", White.bold().paint(&code[..])), + false => format!("{}", &code[..]) + }); Ok(code) }