use clap::{Parser, Subcommand};
#[derive(Debug, Parser)]
#[command(
name = "sqllog2db",
version,
about = "Parse DM database SQL logs and export to CSV/SQLite",
long_about = "A lightweight and efficient CLI tool for parsing DM database SQL logs (streaming) and exporting to CSV or SQLite.",
after_help = "\
EXAMPLES:
Export all records from SQL log files:
sqllog2db run -c config.toml
Override input paths from the command line:
sqllog2db run -c config.toml --input 'sqllogs/2025-*.log'
Generate a default configuration file:
sqllog2db init -o config.toml
Validate a configuration file:
sqllog2db validate -c config.toml"
)]
pub(crate) struct Cli {
#[arg(
short = 'v',
long = "verbose",
global = true,
conflicts_with = "quiet",
help = "Show per-file processing details on stderr."
)]
pub(crate) verbose: bool,
#[arg(short = 'q', long = "quiet", global = true, conflicts_with = "verbose")]
pub(crate) quiet: bool,
#[command(subcommand)]
pub(crate) command: Option<Commands>,
}
#[derive(Debug, Subcommand)]
pub(crate) enum Commands {
#[command(
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.",
after_help = "\
EXAMPLES:
Export using a custom configuration path:
sqllog2db run -c /path/to/config.toml
Pipe log data via stdin:
cat sqllogs/2025-01-15.log | sqllog2db run -c config.toml
Override input paths from CLI:
sqllog2db run -c config.toml --input 'sqllogs/*.log' --input archive.log
Configuration file sections: [csv] / [sqlite] for output, [filter] for filters (include, exclude, indicators, sql)."
)]
Run {
#[arg(
short = 'c',
long = "config",
default_value = "config.toml",
env = "SQLLOG2DB_CONFIG",
help = "TOML configuration file path. See [csv], [sqlite], [filter] sections."
)]
config: String,
#[arg(
short = 'i',
long = "input",
action = clap::ArgAction::Append,
help = "Input log paths. Repeat for multiple entries. Overrides config [sqllog].inputs."
)]
input: Option<Vec<String>>,
},
#[command(
long_about = "Generate a default TOML configuration file with all available options and their default values.",
after_help = "\
EXAMPLES:
Generate a config file, overwriting if it already exists:
sqllog2db init -o myconfig.toml --force"
)]
Init {
#[arg(
short = 'o',
long = "output",
default_value = "config.toml",
help = "Path for the generated default configuration file."
)]
output: String,
#[arg(short = 'f', long = "force")]
force: bool,
#[arg(short = 'i', long = "interactive")]
interactive: bool,
},
#[command(
long_about = "Validate a TOML configuration file for correctness. Checks that all required sections and fields are present and valid.",
after_help = "\
EXAMPLES:
Validate a configuration file:
sqllog2db validate -c config.toml
Validate in quiet mode (suppress non-error output):
sqllog2db validate -c config.toml --quiet"
)]
Validate {
#[arg(
short = 'c',
long = "config",
default_value = "config.toml",
env = "SQLLOG2DB_CONFIG",
help = "TOML configuration file path to validate."
)]
config: String,
},
#[command(
long_about = "Analyse DM database SQL log files and report slow SQL and high-frequency SQL patterns.",
after_help = "\
EXAMPLES:
Run statistics with default top-20:
sqllog2db stats -c config.toml
Limit output to top 5 per table:
sqllog2db stats -c config.toml --top 5
Filter records by time range:
sqllog2db stats -c config.toml --from \"2024-01-01\" --to \"2024-01-31\""
)]
Stats {
#[arg(
short = 'c',
long = "config",
default_value = "config.toml",
env = "SQLLOG2DB_CONFIG",
help = "TOML configuration file path. See [csv], [sqlite], [sqllog] sections."
)]
config: String,
#[arg(
long = "top",
value_parser = clap::value_parser!(u32).range(1..),
help = "Number of top records per table. Must be >= 1 (default: 20)."
)]
top: Option<u32>,
#[arg(
long = "from",
value_name = "DATETIME",
help = "Start of time range. Formats: \"YYYY-MM-DD\" or \"YYYY-MM-DD HH:MM:SS\". Overrides config [stats].from."
)]
from: Option<String>,
#[arg(
long = "to",
value_name = "DATETIME",
help = "End of time range. Formats: \"YYYY-MM-DD\" or \"YYYY-MM-DD HH:MM:SS\". Overrides config [stats].to."
)]
to: Option<String>,
},
#[command(
long_about = "Watch configured input directories for new .log files. Automatically triggers processing when new files appear. Press Ctrl+C to stop.",
after_help = "\
EXAMPLES:
Watch and process new log files automatically:
sqllog2db watch -c config.toml
Watch in quiet mode (suitable for cron/background):
sqllog2db watch -c config.toml --quiet"
)]
Watch {
#[arg(
short = 'c',
long = "config",
default_value = "config.toml",
env = "SQLLOG2DB_CONFIG",
help = "TOML configuration file path."
)]
config: String,
},
}