use clap::ArgMatches;
use ros_add::{
XMLHelper, add_dependency_to_cargo_toml, add_dependency_to_package_xml, env_to_matches,
path_to_document,
};
use std::{env, io::ErrorKind, path::Path, process::Command};
fn main() -> Result<(), ErrorKind> {
let matches: ArgMatches = env_to_matches(env::args().collect());
let dependency = matches.get_one::<String>("dependency").unwrap_or_else(|| {
panic!(
"Argument 'dependency' is required but missing. Check `env_to_matches` implementation."
)
});
let package_name = dependency.split('@').next().unwrap_or(dependency);
println!("Attempting to add '{package_name}' to Cargo.toml...");
if !matches.get_flag("no_cargo_toml")
&& !Command::new("cargo")
.arg("add")
.arg(dependency)
.args(
matches
.get_one::<String>("color")
.into_iter()
.flat_map(|e| ["--color", e]),
)
.status()
.map_err(|e| e.kind())?
.success()
&& Path::new("Cargo.toml").exists()
{
eprintln!("Warning: 'cargo add' command failed");
path_to_document("Cargo.toml".to_string()).map(|ok| {
add_dependency_to_cargo_toml(ok.0, "dependencies".to_string(), ok.1, package_name)
})??;
}
if !matches.get_flag("no_package_xml") {
XMLHelper::new("package.xml".to_string()).map(|ok| {
add_dependency_to_package_xml(ok.path, ok.reader, ok.writer, ok.buf, package_name)
})??;
}
Ok(())
}