limavel 1.0.0

A tool for creating virtualized development environments for Laravel in macOS
use anyhow::Result;
use console::style;

use crate::config::lima_config::LimaConfig;
use crate::config::limavel_config::LimavelConfig;
use crate::lima::client::LimaClient;

pub mod destroy;
pub mod edit;
pub mod exec;
pub mod init;
pub mod provision;
pub mod publish;
pub mod restart;
pub mod ssh;
pub mod start;
pub mod status;
pub mod stop;

/// Compare the config against the live instance and rewrite lima.yaml when
/// cpus, memory, disk, or shared folders changed. Stops the VM first if it is
/// running; callers decide when to start it again. Returns whether changes
/// were applied.
pub fn sync_vm_config(instance: &str, config: &LimavelConfig) -> Result<bool> {
    let current_cpus = LimaClient::instance_cpus(instance)?;
    let current_memory = LimaClient::instance_memory_mib(instance)?;
    let current_disk = LimaClient::instance_disk_gib(instance)?;

    let cpus_changed = config.cpus != current_cpus;
    let memory_changed = config.memory != current_memory;
    let disk_changed = config.disk > current_disk;
    let mounts_changed = folders_changed(instance, config)?;

    if !cpus_changed && !memory_changed && !disk_changed && !mounts_changed {
        return Ok(false);
    }

    let mut changes = Vec::new();
    if cpus_changed {
        changes.push(format!("cpus: {} -> {}", current_cpus, config.cpus));
    }
    if memory_changed {
        changes.push(format!("memory: {}MiB -> {}MiB", current_memory, config.memory));
    }
    if disk_changed {
        changes.push(format!("disk: {}GiB -> {}GiB", current_disk, config.disk));
    }
    if mounts_changed {
        changes.push("shared folders".to_string());
    }
    println!("{} Applying VM changes: {}", style("→").cyan(), changes.join(", "));

    if LimaClient::instance_status(instance)? == "Running" {
        println!("{} Stopping VM '{}' to apply changes...", style("→").cyan(), instance);
        LimaClient::stop(instance)?;
    }

    let ssh_pubkey = config.read_ssh_pubkey()?;
    let lima_config = LimaConfig::from_config(config, &ssh_pubkey)?;
    let yaml = lima_config.to_yaml()?;
    LimaClient::edit(instance, &yaml)?;
    println!("{} VM changes applied.", style("✓").green());

    Ok(true)
}

fn folders_changed(instance: &str, config: &LimavelConfig) -> Result<bool> {
    let current_mounts = LimaClient::instance_mounts(instance)?;
    let desired: Vec<(String, Option<String>)> = config
        .folders
        .iter()
        .map(|f| {
            let expanded = shellexpand::tilde(&f.map).to_string();
            (expanded, Some(f.to.clone()))
        })
        .collect();

    // Filter current mounts to only user-defined ones (those with a mount_point)
    let current_user_mounts: Vec<&(String, Option<String>)> = current_mounts
        .iter()
        .filter(|(_, mp)| mp.is_some())
        .collect();

    if desired.len() != current_user_mounts.len() {
        return Ok(true);
    }

    for (loc, mp) in &desired {
        if !current_user_mounts.iter().any(|(cl, cm)| cl == loc && cm == mp) {
            return Ok(true);
        }
    }

    Ok(false)
}