use std::io;
use std::process;
use clap::Arg;
use clap::ArgAction;
use clap::Command;
fn main() {
let args = Command::new("pg_dump TOC rewriter")
.author("WiltonDB Software")
.version("1.0.6")
.about("Changes Babelfish logical DB name in pg_dump files")
.arg(Arg::new("dbname")
.short('d')
.long("dbname")
.help("DB name to use instead of original DB name")
)
.arg(Arg::new("print")
.short('p')
.long("print")
.action(ArgAction::SetTrue)
.conflicts_with("dbname")
.help("Only print TOC details without rewriting")
)
.arg(Arg::new("toc.dat")
.required(true)
.help("TOC file")
)
.get_matches();
let toc_file = args.get_one::<String>("toc.dat").map(|s| s.to_string()).expect("toc.dat not specified");
let dbname = args.get_one::<String>("dbname").map(|s| s.to_string());
let print = args.get_one::<bool>("print").map_or(false, |b| *b);
if print {
match pgdump_toc_rewrite::print_toc(&toc_file, &mut io::stdout()) {
Ok(_) => process::exit(0),
Err(e) => eprintln!("TOC print error: {}", e)
}
} else if let Some(name) = dbname {
match pgdump_toc_rewrite::rewrite_toc(&toc_file, &name) {
Ok(_) => process::exit(0),
Err(e) => eprintln!("TOC rewrite error: {}", e)
}
} else {
eprintln!("Error: either 'rewrite' or 'print' flag must be specified")
}
process::exit(1);
}