From bce9f616facac5a4b54dd9f1c79dd393e23aabfe Mon Sep 17 00:00:00 2001 From: Hatter Jiang Date: Fri, 8 Sep 2023 23:25:50 +0800 Subject: [PATCH] feat: update subcmd --- src/cmd_decrypt.rs | 13 +++++++++++++ src/cmd_info.rs | 18 ++++++++++++++---- src/main.rs | 32 ++++++++++---------------------- 3 files changed, 37 insertions(+), 26 deletions(-) diff --git a/src/cmd_decrypt.rs b/src/cmd_decrypt.rs index ec4270f..365945c 100644 --- a/src/cmd_decrypt.rs +++ b/src/cmd_decrypt.rs @@ -3,6 +3,7 @@ use std::fs::File; use std::io::{Read, Write}; use std::path::PathBuf; use std::str::FromStr; +use clap::Args; use openpgp_card::crypto_data::Cryptogram; use openpgp_card::OpenPgp; @@ -19,6 +20,18 @@ use crate::spec::{TinyEncryptEnvelop, TinyEncryptEnvelopType, TinyEncryptMeta}; use crate::util::{decode_base64, decode_base64_url_no_pad, ENC_AES256_GCM_P256, simple_kdf, TINY_ENC_FILE_EXT}; use crate::wrap_key::WrapKey; +#[derive(Debug, Args)] +pub struct CmdDecrypt { + /// Files need to be decrypted + pub paths: Vec, + /// PIN + #[arg(long)] + pub pin: Option, + /// SLOT + #[arg(long)] + pub slot: Option, +} + pub fn decrypt(path: &PathBuf, pin: &Option, slot: &Option) -> XResult<()> { let path_display = format!("{}", path.display()); if !path_display.ends_with(TINY_ENC_FILE_EXT) { diff --git a/src/cmd_info.rs b/src/cmd_info.rs index 5f94cf8..92a8c67 100644 --- a/src/cmd_info.rs +++ b/src/cmd_info.rs @@ -4,18 +4,28 @@ use std::ops::Add; use std::path::PathBuf; use std::time::{Duration, SystemTime}; +use clap::Args; use rust_util::{iff, opt_result, success, XResult}; use rust_util::util_time::get_current_millis; use simpledateformat::format_human2; use crate::file; -pub fn info(path: PathBuf, raw_meta: bool) -> XResult<()> { - let path_display = format!("{}", path.display()); - let mut file_in = opt_result!(File::open(path), "Open file: {} failed: {}", &path_display); +#[derive(Debug, Args)] +pub struct CmdInfo { + /// File + pub path: PathBuf, + /// Show raw meta + #[arg(long, default_value_t = false)] + pub raw_meta: bool, +} + +pub fn info(cmd_info: &CmdInfo) -> XResult<()> { + let path_display = format!("{}", cmd_info.path.display()); + let mut file_in = opt_result!(File::open(&cmd_info.path), "Open file: {} failed: {}", &path_display); let meta = opt_result!(file::read_tiny_encrypt_meta_and_normalize(&mut file_in), "Read file: {}, failed: {}", &path_display); - if raw_meta { + if cmd_info.raw_meta { success!("Meta data:\n{}", serde_json::to_string_pretty(&meta).expect("SHOULD NOT HAPPEN")); return Ok(()); } diff --git a/src/main.rs b/src/main.rs index d4f1811..9a457e0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,6 +5,9 @@ use std::path::PathBuf; use clap::{Parser, Subcommand}; use rust_util::{failure, information, success, XResult}; +use crate::cmd_decrypt::CmdDecrypt; +use crate::cmd_info::CmdInfo; + mod util; mod config; mod spec; @@ -33,25 +36,10 @@ enum Commands { }, /// Decrypt file(s) #[command(arg_required_else_help = true, short_flag = 'd')] - Decrypt { - /// Files need to be decrypted - paths: Vec, - /// PIN - #[arg(long)] - pin: Option, - /// SLOT - #[arg(long)] - slot: Option, - }, + Decrypt(CmdDecrypt), /// Show file info #[command(arg_required_else_help = true, short_flag = 'I')] - Info { - /// File - path: PathBuf, - /// Show raw meta - #[arg(long, default_value_t = false)] - raw_meta: bool, - }, + Info(CmdInfo), } fn main() -> XResult<()> { @@ -61,17 +49,17 @@ fn main() -> XResult<()> { paths.iter().for_each(|f| information!("{:?}", f)); Ok(()) } - Commands::Decrypt { paths, pin, slot } => { - for path in &paths { - match cmd_decrypt::decrypt(path, &pin, &slot) { + Commands::Decrypt(cmd_decrypt) => { + for path in &cmd_decrypt.paths { + match cmd_decrypt::decrypt(path, &cmd_decrypt.pin, &cmd_decrypt.slot) { Ok(_) => success!("Decrypt {} succeed", path.to_str().unwrap_or("N/A")), Err(e) => failure!("Decrypt {} failed: {}", path.to_str().unwrap_or("N/A"), e), } } Ok(()) } - Commands::Info { path, raw_meta } => { - cmd_info::info(path, raw_meta) + Commands::Info(command_info) => { + cmd_info::info(&command_info) } } } \ No newline at end of file