shadow_crypt_shell/decryption/
cli.rs1use crate::errors::{WorkflowError, WorkflowResult};
2use clap::{Arg, Command};
3
4#[derive(Debug, Clone)]
6pub struct DecryptionCliArgs {
7 pub input_files: Vec<String>,
8}
9
10pub fn get_cli_args(args: Vec<String>) -> WorkflowResult<DecryptionCliArgs> {
12 let cmd = || {
13 Command::new("unshadow").about("Decrypt shadow files").arg(
14 Arg::new("files")
15 .help("Input files to decrypt")
16 .required(false)
17 .num_args(1..)
18 .value_name("FILE"),
19 )
20 };
21 let matches = cmd()
22 .try_get_matches_from(&args)
23 .map_err(|e| WorkflowError::UserInput(e.to_string()))?;
24 let input_files: Vec<String> = matches
25 .get_many::<String>("files")
26 .unwrap_or_default()
27 .map(|s| s.to_string())
28 .collect();
29
30 if input_files.is_empty() {
31 return Err(WorkflowError::UserInput(
32 "No input files provided".to_string(),
33 ));
34 }
35
36 Ok(DecryptionCliArgs { input_files })
37}
38
39#[cfg(test)]
40mod tests {
41 use super::*;
42
43 #[test]
44 fn test_parse_cli_args_with_files() {
45 let args = vec![
46 "unshadow".to_string(),
47 "file1.txt".to_string(),
48 "file2.txt".to_string(),
49 ];
50 let cli_args = get_cli_args(args).unwrap();
51 assert_eq!(
52 cli_args.input_files,
53 vec!["file1.txt".to_string(), "file2.txt".to_string()]
54 );
55 }
56
57 #[test]
58 fn test_parse_cli_args_with_single_file() {
59 let args = vec!["unshadow".to_string(), "file1.txt".to_string()];
60 let cli_args = get_cli_args(args).unwrap();
61 assert_eq!(cli_args.input_files, vec!["file1.txt".to_string()]);
62 }
63
64 #[test]
65 fn test_parse_cli_args_no_files() {
66 let args = vec!["unshadow".to_string()];
67 let result = get_cli_args(args);
68 assert!(result.is_err());
69 if let Err(WorkflowError::UserInput(msg)) = result {
70 assert_eq!(msg, "No input files provided");
71 } else {
72 panic!("Expected UserInput error");
73 }
74 }
75
76 #[test]
77 fn test_parse_cli_args_help() {
78 let args = vec!["unshadow".to_string(), "--help".to_string()];
79 let result = get_cli_args(args);
80 assert!(result.is_err());
81 }
83}