//! use std::{ env, fs::File, path::{Path, PathBuf}, process::ExitCode, }; use keyfork_shard::openpgp::{combine, discover_certs, openpgp::Cert, parse_messages}; type Result> = std::result::Result; fn validate( shard: impl AsRef, key_discovery: Option<&str>, ) -> Result<(File, Vec)> { let key_discovery = key_discovery.map(PathBuf::from); key_discovery.as_ref().map(std::fs::metadata).transpose()?; // Load certs from path let certs = key_discovery .map(discover_certs) .transpose()? .unwrap_or(vec![]); Ok((File::open(shard)?, certs)) } fn run() -> Result<()> { let mut args = env::args(); let program_name = args.next().expect("program name"); let args = args.collect::>(); let (messages_file, cert_list) = match args.as_slice() { [shard, key_discovery] => validate(shard, Some(key_discovery))?, [shard] => validate(shard, None)?, _ => panic!("Usage: {program_name} [key_discovery]"), }; let mut encrypted_messages = parse_messages(messages_file)?; let encrypted_metadata = encrypted_messages .pop_front() .expect("any pgp encrypted message"); let mut bytes = vec![]; combine( cert_list, &encrypted_metadata, encrypted_messages.into(), &mut bytes, )?; print!("{}", smex::encode(&bytes)); Ok(()) } fn main() -> ExitCode { let result = run(); if let Err(e) = result { eprintln!("Error: {e}"); let mut source = e.source(); while let Some(new_error) = source.take() { eprintln!("Source: {new_error}"); source = new_error.source(); } return ExitCode::FAILURE; } ExitCode::SUCCESS }