use cu::pre::*;
use crate::Config;
#[derive(Debug, Clone, PartialEq, clap::Parser)]
pub struct Cli {
#[clap(short, long)]
pub fix: bool,
#[clap(short = 'H', long, requires("license"))]
pub holder: Option<String>,
#[clap(short = 'L', long, requires("holder"))]
pub license: Option<String>,
#[clap(flatten)]
pub common: cu::cli::Flags,
pub paths: Vec<String>,
}
pub fn config_from_cli(args: &mut crate::Cli) -> cu::Result<Config> {
match (args.holder.take(), args.license.take()) {
(Some(holder), Some(license)) => {
if let Some(config_path) = crate::try_find_default_config_file() {
cu::bail!(
"--holder or --license cannot be specified when {config_path} is present in the current directory"
);
}
Ok(Config::new(
holder,
license,
std::mem::take(&mut args.paths),
))
}
_ => {
let mut iter = std::mem::take(&mut args.paths).into_iter();
let mut config = match iter.next() {
None => {
let Some(config_path) = crate::try_find_default_config_file() else {
cu::bail!(
"cannot find Lisensor.toml, and no config files are specified on the command line."
);
};
Config::build(config_path)?
}
Some(first) => Config::build(&first)?,
};
for path in iter {
config.absorb(Config::build(&path)?)?;
}
Ok(config)
}
}
}