use clap::Parser;
use pager::Pager;
use std::fmt;
fn main() -> gitig::Result<()> {
let cli = Cli::parse();
let templates = cli.template;
let shell = cli.completion;
let no_pager = cli.no_pager;
let debug = cli.debug;
handler(templates, shell, no_pager, debug)
}
#[derive(Parser)]
#[command(author, version, about, long_about = None)]
struct Cli {
#[arg()]
template: Vec<String>,
#[clap(long, value_enum)]
completion: Option<gitig::Shell>,
#[clap(long)]
no_pager: bool,
#[clap(short, long)]
debug: bool,
}
fn handler(
templates: Vec<String>,
shell: Option<gitig::Shell>,
no_pager: bool,
debug: bool,
) -> gitig::Result<()> {
if let Some(shell) = shell {
return handle_completion(shell, debug);
}
if templates.is_empty() {
return handle_list_templates(no_pager, debug);
}
handle_create(templates, debug)
}
fn handle_completion(shell: gitig::Shell, debug: bool) -> gitig::Result<()> {
match shell.generate_completion_str() {
Ok(completion_str) => println!("{}", completion_str),
Err(e) => match e {
gitig::Error::ApplicationError(_) => eprintln!("{}", e),
_ => log_err(gitig::Error::GENERIC_MSG, e, debug),
},
}
Ok(())
}
fn handle_list_templates(no_pager: bool, debug: bool) -> gitig::Result<()> {
if !no_pager {
Pager::new().setup();
}
match gitig::list_templates() {
Ok(templates) => println!("{}", templates.join("\n")),
Err(e) => log_err(gitig::Error::FETCH_TEMPLATE_FAILED_MSG, e, debug),
}
Ok(())
}
fn handle_create(templates: Vec<String>, debug: bool) -> gitig::Result<()> {
let templates = templates.iter().map(|t| t.as_str());
match gitig::create(templates) {
Ok(text) => println!("{}", text),
Err(e) => match e {
gitig::Error::TemplateNotFound(_) => {
eprintln!("{}", e);
eprintln!("Run `gi` (without arguments) for a list of templates.");
}
gitig::Error::ApplicationError(_) => eprintln!("{}", e),
_ => log_err(gitig::Error::GENERIC_MSG, e, debug),
},
}
Ok(())
}
fn log_err<E: fmt::Debug>(msg: &str, e: E, debug: bool) {
if debug {
eprintln!("{:?}\n\n{}", e, msg);
} else {
eprintln!("{}", msg);
}
}