use std::path::PathBuf;
use crate::errors::{WorkflowError, WorkflowResult};
use clap::Parser;
#[derive(Debug, Clone, Default, Parser)]
#[command(
name = "unshadow",
about = "Decrypt shadow files",
version,
after_help = "Exit codes: 0 success; 1 operation failed; 2 invalid usage or input; \
3 authentication failure (wrong password or corrupted file)."
)]
pub struct DecryptionCliArgs {
#[arg(value_name = "FILE")]
pub input_files: Vec<String>,
#[arg(long = "output-dir", short = 'o', value_name = "DIR")]
pub output_dir: Option<PathBuf>,
#[arg(long = "password-file", value_name = "FILE")]
pub password_file: Option<PathBuf>,
#[arg(long = "force", short = 'f')]
pub force: bool,
#[arg(long = "quiet", short = 'q')]
pub quiet: bool,
}
pub fn get_cli_args(args: Vec<String>) -> WorkflowResult<DecryptionCliArgs> {
let cli_args = DecryptionCliArgs::try_parse_from(args).map_err(|e| {
if e.kind() == clap::error::ErrorKind::DisplayHelp
|| e.kind() == clap::error::ErrorKind::DisplayVersion
{
eprintln!("{}", e);
std::process::exit(0);
}
WorkflowError::UserInput(e.to_string())
})?;
if cli_args.input_files.is_empty() {
return Err(WorkflowError::UserInput(
"No input files provided".to_string(),
));
}
Ok(cli_args)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_parse_cli_args_with_files() {
let args = vec![
"unshadow".to_string(),
"file1.txt".to_string(),
"file2.txt".to_string(),
];
let cli_args = get_cli_args(args).unwrap();
assert_eq!(
cli_args.input_files,
vec!["file1.txt".to_string(), "file2.txt".to_string()]
);
}
#[test]
fn test_parse_cli_args_with_single_file() {
let args = vec!["unshadow".to_string(), "file1.txt".to_string()];
let cli_args = get_cli_args(args).unwrap();
assert_eq!(cli_args.input_files, vec!["file1.txt".to_string()]);
}
#[test]
fn test_parse_cli_args_no_files() {
let args = vec!["unshadow".to_string()];
let result = get_cli_args(args);
assert!(result.is_err());
if let Err(WorkflowError::UserInput(msg)) = result {
assert_eq!(msg, "No input files provided");
} else {
panic!("Expected UserInput error");
}
}
#[test]
fn test_parse_cli_args_help() {
}
}