mod actions;
mod cli;
mod error;
mod services;
use cli::{build_cli, parse_shared_arguments, SharedArguments};
use log::{error, warn};
use std::env;
use std::process;
#[tokio::main]
async fn main() {
setup_logger();
let app = build_cli();
let app_m = app.get_matches();
let res = run_matches(&app_m).await;
if let Err(err) = res {
error!("{}", err);
process::exit(1);
}
}
async fn run_matches(args: &clap::ArgMatches<'_>) -> Result<(), error::FoxdieError> {
match args.subcommand() {
("branches", Some(sub_m)) => {
let SharedArguments {
should_delete,
since,
token,
} = parse_shared_arguments(&sub_m);
let path = sub_m.value_of("DIRECTORY");
if !should_delete {
print_dry_run_warning();
}
actions::local::clean_remote_branches(
path,
actions::local::Options {
should_delete,
since_date: &since,
token,
},
)
.await
}
("push-requests", Some(sub_m)) => {
let SharedArguments {
should_delete,
since,
token,
} = parse_shared_arguments(&sub_m);
if !should_delete {
print_dry_run_warning();
}
let url = sub_m
.value_of("URL")
.expect("URL was supposed to be passed as a positional argument.");
actions::push_requests::clean_push_requests(should_delete, &since, &url, &token).await
}
("report", Some(sub_m)) => {
let output_path = sub_m.value_of("output");
let repo_path = sub_m.value_of("DIRECTORY");
actions::report::report(&output_path, repo_path)
}
_ => unreachable!(),
}
}
fn setup_logger() {
let rust_log = match env::var("RUST_LOG") {
Ok(var) => var,
_ => String::from("foxdie=info"),
};
env_logger::builder()
.format_module_path(false)
.format_timestamp(None)
.parse_filters(&rust_log)
.init();
}
fn print_dry_run_warning() {
warn!(
"Foxdie is being run in dry run mode, which is the default. \
If this is undesirable, run again with the `--delete` flag."
);
}