use std::path::PathBuf;
use bestool_psql::{ExportOptions, QueryOptions, default_audit_dir, export_audit_entries};
use clap::Parser;
use miette::Result;
use crate::actions::Context;
use crate::args::Args;
#[derive(Debug, Clone, Parser)]
pub struct AuditPsqlArgs {
#[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()
)
}
pub async fn run(ctx: Context<Args, AuditPsqlArgs>) -> Result<()> {
let args = ctx.args_sub;
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)
}
}
}