use std::path::PathBuf;
use crate::commands::{apply, generate, init, unapply}; use crate::config::Config;
use crate::error::LichenError;
use crate::models::Commands;
use log::debug;
pub struct LichenApp {}
impl LichenApp {
pub fn new() -> Self {
debug!("LichenApp instance created.");
LichenApp {}
}
pub async fn run(&self, command: Commands, config_path: PathBuf) -> Result<(), LichenError> {
debug!("Dispatching command: {:?}", command);
let cfg = Config::load_or_default(config_path)?;
match command {
Commands::Gen(args) => {
if cfg.licenses.is_none() {
let settings = generate::GenSettings::new(&args, &cfg, None)?;
generate::handle_gen(&settings)?;
} else {
for (idx, _license) in cfg.licenses.iter().enumerate() {
let settings = generate::GenSettings::new(&args, &cfg, Some(idx))?;
generate::handle_gen(&settings)?;
}
}
Ok(()) }
Commands::Apply(args) => {
if cfg.licenses.is_none() {
let settings = apply::ApplySettings::new(&args, &cfg, None)?;
apply::handle_apply(&settings).await?;
} else {
for (idx, _license) in cfg.licenses.iter().enumerate() {
let settings = apply::ApplySettings::new(&args, &cfg, Some(idx))?;
apply::handle_apply(&settings).await?;
}
}
Ok(()) }
Commands::Init(args) => init::handle_init(args), Commands::Unapply(args) => unapply::handle_unapply(args).await, }
}
}
impl Default for LichenApp {
fn default() -> Self {
Self::new()
}
}