Skip to main content

dm_database_sqllog2db/cli/
opts.rs

1use clap::{Parser, Subcommand};
2
3/// SQL log exporter tool for DM database
4#[derive(Debug, Parser)]
5#[command(
6    name = "sqllog2db",
7    version,
8    about = "Parse DM database SQL logs and export to CSV/SQLite",
9    long_about = "A lightweight and efficient CLI tool for parsing DM database SQL logs (streaming) and exporting to CSV or SQLite.",
10    after_help = "\
11EXAMPLES:
12    Export all records from SQL log files:
13        sqllog2db run -c config.toml
14
15    Override input paths from the command line:
16        sqllog2db run -c config.toml --input 'sqllogs/2025-*.log'
17
18    Generate a default configuration file:
19        sqllog2db init -o config.toml
20
21    Validate a configuration file:
22        sqllog2db validate -c config.toml"
23)]
24pub(crate) struct Cli {
25    /// Show per-file processing details
26    #[arg(
27        short = 'v',
28        long = "verbose",
29        global = true,
30        conflicts_with = "quiet",
31        help = "Show per-file processing details on stderr."
32    )]
33    pub(crate) verbose: bool,
34
35    /// Suppress non-error output
36    #[arg(short = 'q', long = "quiet", global = true, conflicts_with = "verbose")]
37    pub(crate) quiet: bool,
38
39    #[command(subcommand)]
40    pub(crate) command: Option<Commands>,
41}
42
43#[derive(Debug, Subcommand)]
44pub(crate) enum Commands {
45    /// Run the log export task
46    #[command(
47        long_about = "Run the log export task. Parses DM database SQL log files and exports them to CSV or SQLite based on the configuration file.",
48        after_help = "\
49EXAMPLES:
50    Export using a custom configuration path:
51        sqllog2db run -c /path/to/config.toml
52
53    Pipe log data via stdin:
54        cat sqllogs/2025-01-15.log | sqllog2db run -c config.toml
55
56    Override input paths from CLI:
57        sqllog2db run -c config.toml --input 'sqllogs/*.log' --input archive.log
58
59Configuration file sections: [csv] / [sqlite] for output, [filter] for filters (include, exclude, indicators, sql)."
60    )]
61    Run {
62        /// TOML configuration file path
63        #[arg(
64            short = 'c',
65            long = "config",
66            default_value = "config.toml",
67            env = "SQLLOG2DB_CONFIG",
68            help = "TOML configuration file path. See [csv], [sqlite], [filter] sections."
69        )]
70        config: String,
71        /// Input log paths. Repeat for multiple entries. Overrides config \[sqllog\].inputs.
72        #[arg(
73            short = 'i',
74            long = "input",
75            action = clap::ArgAction::Append,
76            help = "Input log paths. Repeat for multiple entries. Overrides config [sqllog].inputs."
77        )]
78        input: Option<Vec<String>>,
79    },
80    /// Generate a default configuration file
81    #[command(
82        long_about = "Generate a default TOML configuration file with all available options and their default values.",
83        after_help = "\
84EXAMPLES:
85    Generate a config file, overwriting if it already exists:
86        sqllog2db init -o myconfig.toml --force"
87    )]
88    Init {
89        /// Path for the generated default configuration file
90        #[arg(
91            short = 'o',
92            long = "output",
93            default_value = "config.toml",
94            help = "Path for the generated default configuration file."
95        )]
96        output: String,
97        /// Force overwrite if file exists
98        #[arg(short = 'f', long = "force")]
99        force: bool,
100        /// Start interactive configuration wizard
101        #[arg(short = 'i', long = "interactive")]
102        interactive: bool,
103    },
104    /// Validate a configuration file
105    #[command(
106        long_about = "Validate a TOML configuration file for correctness. Checks that all required sections and fields are present and valid.",
107        after_help = "\
108EXAMPLES:
109    Validate a configuration file:
110        sqllog2db validate -c config.toml
111
112    Validate in quiet mode (suppress non-error output):
113        sqllog2db validate -c config.toml --quiet"
114    )]
115    Validate {
116        /// TOML configuration file path to validate
117        #[arg(
118            short = 'c',
119            long = "config",
120            default_value = "config.toml",
121            env = "SQLLOG2DB_CONFIG",
122            help = "TOML configuration file path to validate."
123        )]
124        config: String,
125    },
126    /// Run statistical analysis on SQL log files
127    #[command(
128        long_about = "Analyse DM database SQL log files and report slow SQL and high-frequency SQL patterns.",
129        after_help = "\
130EXAMPLES:
131    Run statistics with default top-20:
132        sqllog2db stats -c config.toml
133
134    Limit output to top 5 per table:
135        sqllog2db stats -c config.toml --top 5
136
137    Filter records by time range:
138        sqllog2db stats -c config.toml --from \"2024-01-01\" --to \"2024-01-31\""
139    )]
140    Stats {
141        /// TOML configuration file path
142        #[arg(
143            short = 'c',
144            long = "config",
145            default_value = "config.toml",
146            env = "SQLLOG2DB_CONFIG",
147            help = "TOML configuration file path. See [csv], [sqlite], [sqllog] sections."
148        )]
149        config: String,
150        /// Number of top records to display per table (default: 20)
151        #[arg(
152            long = "top",
153            value_parser = clap::value_parser!(u32).range(1..),
154            help = "Number of top records per table. Must be >= 1 (default: 20)."
155        )]
156        top: Option<u32>,
157        /// Start of time range (overrides config `[stats].from`)
158        #[arg(
159            long = "from",
160            value_name = "DATETIME",
161            help = "Start of time range. Formats: \"YYYY-MM-DD\" or \"YYYY-MM-DD HH:MM:SS\". Overrides config [stats].from."
162        )]
163        from: Option<String>,
164        /// End of time range (overrides config `[stats].to`)
165        #[arg(
166            long = "to",
167            value_name = "DATETIME",
168            help = "End of time range. Formats: \"YYYY-MM-DD\" or \"YYYY-MM-DD HH:MM:SS\". Overrides config [stats].to."
169        )]
170        to: Option<String>,
171    },
172    /// Watch directory for new .log files and process them automatically
173    #[command(
174        long_about = "Watch configured input directories for new .log files. Automatically triggers processing when new files appear. Press Ctrl+C to stop.",
175        after_help = "\
176EXAMPLES:
177    Watch and process new log files automatically:
178        sqllog2db watch -c config.toml
179
180    Watch in quiet mode (suitable for cron/background):
181        sqllog2db watch -c config.toml --quiet"
182    )]
183    Watch {
184        /// TOML configuration file path
185        #[arg(
186            short = 'c',
187            long = "config",
188            default_value = "config.toml",
189            env = "SQLLOG2DB_CONFIG",
190            help = "TOML configuration file path."
191        )]
192        config: String,
193    },
194}