feat: update subcmd
This commit is contained in:
@@ -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<PathBuf>,
|
||||
/// PIN
|
||||
#[arg(long)]
|
||||
pub pin: Option<String>,
|
||||
/// SLOT
|
||||
#[arg(long)]
|
||||
pub slot: Option<String>,
|
||||
}
|
||||
|
||||
pub fn decrypt(path: &PathBuf, pin: &Option<String>, slot: &Option<String>) -> XResult<()> {
|
||||
let path_display = format!("{}", path.display());
|
||||
if !path_display.ends_with(TINY_ENC_FILE_EXT) {
|
||||
|
||||
@@ -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(());
|
||||
}
|
||||
|
||||
32
src/main.rs
32
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<PathBuf>,
|
||||
/// PIN
|
||||
#[arg(long)]
|
||||
pin: Option<String>,
|
||||
/// SLOT
|
||||
#[arg(long)]
|
||||
slot: Option<String>,
|
||||
},
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user