Add hash as CLI function (#1995)

* Add hash as CLI function

* Use streaming SHA3.
This commit is contained in:
Gav Wood 2016-09-23 15:28:09 +02:00 committed by GitHub
parent aae6d19df9
commit 84ba75f7cb
4 changed files with 23 additions and 2 deletions

View File

@ -32,6 +32,8 @@ usage! {
cmd_snapshot: bool,
cmd_restore: bool,
cmd_ui: bool,
cmd_tools: bool,
cmd_hash: bool,
// Arguments
arg_pid_file: String,
@ -441,6 +443,8 @@ mod tests {
cmd_snapshot: false,
cmd_restore: false,
cmd_ui: false,
cmd_tools: false,
cmd_hash: false,
// Arguments
arg_pid_file: "".into(),

View File

@ -14,6 +14,7 @@ Usage:
parity signer new-token [options]
parity snapshot <file> [options]
parity restore [ <file> ] [options]
parity tools hash <file>
Operating Options:
--mode MODE Set the operating mode. MODE can be one of:
@ -283,4 +284,3 @@ Miscellaneous Options:
--no-color Don't use terminal color codes in output. (default: {flag_no_color})
-v --version Show information about version.
-h --help Show this screen.

View File

@ -51,6 +51,7 @@ pub enum Cmd {
Blockchain(BlockchainCmd),
SignerToken(String),
Snapshot(SnapshotCommand),
Hash(Option<String>),
}
#[derive(Debug, PartialEq)]
@ -94,8 +95,10 @@ impl Configuration {
let cmd = if self.args.flag_version {
Cmd::Version
} else if self.args.cmd_signer {
} else if self.args.cmd_signer && self.args.cmd_new_token {
Cmd::SignerToken(dirs.signer)
} else if self.args.cmd_tools && self.args.cmd_hash {
Cmd::Hash(self.args.arg_file)
} else if self.args.cmd_account {
let account_cmd = if self.args.cmd_new {
let new_acc = NewAccount {

View File

@ -111,10 +111,23 @@ mod boot;
mod stratum;
use std::{process, env};
use std::io::BufReader;
use std::fs::File;
use util::sha3::sha3;
use cli::Args;
use configuration::{Cmd, Configuration};
use deprecated::find_deprecated;
fn print_hash_of(maybe_file: Option<String>) -> Result<String, String> {
if let Some(file) = maybe_file {
let mut f = BufReader::new(try!(File::open(&file).map_err(|_| "Unable to open file".to_owned())));
let hash = try!(sha3(&mut f).map_err(|_| "Unable to read from file".to_owned()));
Ok(hash.hex())
} else {
Err("Streaming from standard input not yet supported. Specify a file.".to_owned())
}
}
fn execute(command: Cmd) -> Result<String, String> {
match command {
Cmd::Run(run_cmd) => {
@ -122,6 +135,7 @@ fn execute(command: Cmd) -> Result<String, String> {
Ok("".into())
},
Cmd::Version => Ok(Args::print_version()),
Cmd::Hash(maybe_file) => print_hash_of(maybe_file),
Cmd::Account(account_cmd) => account::execute(account_cmd),
Cmd::ImportPresaleWallet(presale_cmd) => presale::execute(presale_cmd),
Cmd::Blockchain(blockchain_cmd) => blockchain::execute(blockchain_cmd),