use std::path::PathBuf;
use bestool_psql::{ExportOptions, QueryOptions, default_audit_dir, export_audit_entries};
use clap::Parser;
use lloggs::{LoggingArgs, PreArgs, WorkerGuard};
use miette::{Result, miette};
use tracing::debug;
#[derive(Debug, Clone, Parser)]
pub struct Args {
#[command(flatten)]
logging: LoggingArgs,
#[arg(long, value_name = "PATH", help = help_audit_path())]
pub audit_path: Option<PathBuf>,
#[arg(short = 'n', long, default_value = "100")]
pub limit: Option<usize>,
#[arg(long)]
pub first: bool,
#[arg(long)]
pub since: Option<String>,
#[arg(long)]
pub until: Option<String>,
#[arg(long)]
pub orphans: bool,
}
fn help_audit_path() -> String {
format!(
"Path to audit database directory (default: {})",
default_audit_dir()
)
}
fn get_args() -> Result<(Args, WorkerGuard)> {
let log_guard = PreArgs::parse().setup().map_err(|err| miette!("{err}"))?;
debug!("parsing arguments");
let args = Args::parse();
let log_guard = match log_guard {
Some(g) => g,
None => args
.logging
.setup(|v| match v {
0 => "bestool_psql=info",
1 => "info,bestool_psql=debug",
2 => "debug",
3 => "debug,bestool_psql=trace",
_ => "trace",
})
.map_err(|err| miette!("{err}"))?,
};
debug!(?args, "got arguments");
Ok((args, log_guard))
}
#[tokio::main]
async fn main() -> Result<()> {
let (args, _guard) = get_args()?;
let options = ExportOptions {
audit_path: args.audit_path,
query_options: QueryOptions {
limit: args.limit,
from_oldest: args.first,
since: args.since,
until: args.until,
},
orphans: args.orphans,
};
match export_audit_entries(options) {
Ok(()) => Ok(()),
Err(e) => {
let error_msg = format!("{:?}", e);
if error_msg.contains("Broken pipe") || error_msg.contains("BrokenPipe") {
return Ok(());
}
Err(e)
}
}
}