postfix_log_parser/
cli.rs

1use std::env;
2use std::path::Path;
3use std::process;
4
5/// CLI配置结构体
6///
7/// 存储命令行界面的配置参数和选项
8#[derive(Debug)]
9pub struct CliConfig {
10    /// 输入日志文件路径
11    /// 要解析的Postfix日志文件的完整路径
12    pub input_file: String,
13
14    /// 输出格式类型
15    /// 指定解析结果的输出格式(当前主要支持JSON)
16    pub output_format: OutputFormat,
17}
18
19/// 输出格式枚举
20#[derive(Debug)]
21pub enum OutputFormat {
22    Json,
23    // 未来可以添加其他格式: Csv, Xml等
24}
25
26impl CliConfig {
27    /// 解析命令行参数
28    pub fn from_args() -> Self {
29        let args: Vec<String> = env::args().collect();
30
31        if args.len() != 2 {
32            Self::print_usage(&args[0]);
33            process::exit(1);
34        }
35
36        let input_file = args[1].clone();
37
38        // 验证输入文件是否存在
39        if !Path::new(&input_file).exists() {
40            eprintln!("❌ 错误: 日志文件不存在: {}", input_file);
41            process::exit(1);
42        }
43
44        Self {
45            input_file,
46            output_format: OutputFormat::Json, // 目前只支持JSON
47        }
48    }
49
50    /// 打印使用说明
51    fn print_usage(program_name: &str) {
52        eprintln!("📖 PostfixLogParser - 高性能Postfix日志解析工具");
53        eprintln!();
54        eprintln!("用法:");
55        eprintln!("  {} <日志文件路径>", program_name);
56        eprintln!();
57        eprintln!("示例:");
58        eprintln!("  {} /var/log/mail.log", program_name);
59        eprintln!("  {} logs/postfix.log", program_name);
60        eprintln!("  {} /path/to/mail.log", program_name);
61        eprintln!();
62        eprintln!("特性:");
63        eprintln!("  ✅ 支持完整的SMTPD事件解析");
64        eprintln!("  ✅ 高性能正则表达式优化");
65        eprintln!("  ✅ JSON格式输出");
66        eprintln!("  ✅ 详细的事件字段提取");
67        eprintln!();
68        eprintln!("输出: 结构化JSON格式的解析结果");
69    }
70}