feat: works

This commit is contained in:
2021-01-01 22:16:11 +08:00
parent 601f11efe6
commit 24150ca0f1
16 changed files with 555 additions and 82 deletions

View File

@@ -0,0 +1,24 @@
use std::fs;
use std::fs::File;
use clap::{App, Arg, ArgMatches, SubCommand};
use rust_util::failure;
use crate::{cmd::{Command, CommandError}, util::{JsonKeyPair, make_key_pair}};
pub struct CommandImpl;
impl Command for CommandImpl {
fn name(&self) -> &str { "genkey" }
fn subcommand<'a>(&self) -> App<'a, 'a> {
SubCommand::with_name(self.name()).about("Generate key pair subcommand")
.arg(Arg::with_name("output").long("output").short("o").required(true).takes_value(true).help("Key pair output"))
}
fn run(&self, _arg_matches: &ArgMatches, sub_arg_matches: &ArgMatches) -> CommandError {
let output = sub_arg_matches.value_of("output").unwrap();
if let Ok(_) = File::open(output) {
failure!("Output file exists: {}", output);
return Ok(());
}
let (pri_key, pub_key) = make_key_pair();
let json_key_pair = JsonKeyPair::from(pri_key, pub_key);
fs::write(output, json_key_pair.to_json()?.as_bytes())?;
Ok(())
}
}