use clap::{Args, Parser, Subcommand};
use hs_intake::{
compare, compare_buildrequires, compare_provides, compare_requires, fedrq, safe_to_backport,
};
#[derive(Parser)]
#[command(
about,
long_about = None,
max_term_width = 80,
version = sandogasa_cli::version!(),
before_help = sandogasa_cli::banner!()
)]
struct Cli {
#[command(subcommand)]
command: Commands,
}
#[derive(Args)]
struct CompareArgs {
srpm: String,
source_branch: String,
target_branch: String,
#[arg(long)]
json: bool,
#[arg(long)]
show_unchanged: bool,
}
#[derive(Subcommand)]
enum Commands {
CompareBuildRequires(CompareArgs),
CompareProvides(CompareArgs),
CompareRequires(CompareArgs),
SafeToBackport {
srpm: String,
target_branch: String,
source_branch: String,
#[arg(long)]
json: bool,
#[arg(long, value_delimiter = ',')]
also_check: Vec<String>,
},
}
fn run_compare(
args: &CompareArgs,
label: &str,
compare_fn: fn(&str, &str, &str) -> Result<compare::CompareResult, fedrq::Error>,
) {
match compare_fn(&args.srpm, &args.source_branch, &args.target_branch) {
Ok(cmp) => {
if args.json {
println!("{}", serde_json::to_string_pretty(&cmp).unwrap());
} else {
compare::print_result(
&cmp,
label,
&args.source_branch,
&args.target_branch,
args.show_unchanged,
);
}
}
Err(e) => {
eprintln!("Error: {e}");
std::process::exit(1);
}
}
}
fn main() {
sandogasa_cli::init();
let cli = sandogasa_cli::parse_with_defaults::<Cli>(env!("CARGO_PKG_NAME"));
match cli.command {
Commands::CompareBuildRequires(args) => run_compare(
&args,
"BuildRequire",
compare_buildrequires::compare_buildrequires,
),
Commands::CompareProvides(args) => {
run_compare(&args, "Provide", compare_provides::compare_provides)
}
Commands::CompareRequires(args) => {
run_compare(&args, "Require", compare_requires::compare_requires)
}
Commands::SafeToBackport {
srpm,
target_branch,
source_branch,
json,
also_check,
} => {
match safe_to_backport::safe_to_backport(
&srpm,
&target_branch,
&source_branch,
&also_check,
) {
Ok(result) => {
if json {
println!("{}", serde_json::to_string_pretty(&result).unwrap());
} else {
safe_to_backport::print_result(
&result,
&srpm,
&target_branch,
&source_branch,
);
}
if !result.safe {
std::process::exit(1);
}
}
Err(e) => {
eprintln!("Error: {e}");
std::process::exit(1);
}
}
}
}
}
#[cfg(test)]
mod tests {
#[test]
fn man_page_matches_cli() {
sandogasa_cli::man::check::<super::Cli>(
concat!(env!("CARGO_MANIFEST_DIR"), "/man/hs-intake.1"),
env!("CARGO_PKG_VERSION"),
);
}
}