use clap::Parser;
use gitu::{
Res,
cli::{self, Args, Commands},
config,
error::Error,
term,
};
use log::LevelFilter;
use std::{backtrace::Backtrace, fmt::Display, panic, process, sync::Arc};
pub fn main() -> Res<()> {
let args = Args::parse();
if args.version {
println!("gitu {}", git_version::git_version!(cargo_suffix = ""));
return Ok(());
}
if let Some(Commands::Completion { shell }) = args.command {
cli::completions(shell, &mut std::io::stdout());
return Ok(());
}
if args.log {
simple_logging::log_to_file(gitu::LOG_FILE_NAME, LevelFilter::Debug)
.map_err(Error::OpenLogFile)?;
}
let config = Arc::new(config::init_config(args.config.clone())?);
let config_ref = config.clone();
panic::set_hook(Box::new(move |panic_info| {
print_err(term::backend().reset_term(&config));
eprintln!("{}", panic_info);
eprintln!("trace: \n{}", Backtrace::force_capture());
}));
log::debug!("Starting app");
let result = gitu::run(config_ref, &args, &mut term::backend());
process::exit(result?);
}
fn print_err<T, E: Display>(result: Result<T, E>) {
match result {
Ok(_) => (),
Err(error) => eprintln!("Error: {}", error),
};
}