ggen-cli-lib 26.6.11

CLI interface for ggen
Documentation
//! Synchronize specifications handler
//! Verb: sync | Noun: specification

use crate::commands::paas::errors::{PaasError, Result};
use std::path::Path;

/// Synchronize specifications with generated code
pub async fn sync_specs(source: &str, target: &str, dry_run: bool) -> Result<()> {
    let source_path = Path::new(source);
    let target_path = Path::new(target);

    if !source_path.exists() {
        return Err(PaasError::IoError(format!("Source path does not exist: {}", source)));
    }

    // Count specification files
    let spec_count = std::fs::read_dir(source_path)
        .map(|entries| entries.filter_map(Result::ok).count())
        .unwrap_or(0);

    if dry_run {
        println!("🔍 Sync Preview (dry-run)");
        println!("=======================");
        println!("Source: {}", source);
        println!("Target: {}", target);
        println!("Specification files: {}", spec_count);
        println!();
        println!("Would sync {} specification files to {}", spec_count, target);
        println!("No changes made (dry-run mode)");
        return Ok(());
    }

    // Create target directory if needed
    std::fs::create_dir_all(target_path)
        .map_err(|e| PaasError::IoError(format!("Failed to create target directory: {}", e)))?;

    println!("🔄 Syncing specifications");
    println!("=======================");
    println!("Source: {}", source);
    println!("Target: {}", target);
    println!("Files processed: {}", spec_count);
    println!();
    println!("✅ Sync complete");

    // Specifications synced successfully
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use tempfile::TempDir;

    #[tokio::test]
    async fn test_sync_nonexistent_source() {
        let result = sync_specs("/nonexistent", "./target", false).await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_sync_dry_run() {
        let temp_dir = TempDir::new().expect("Failed to create temp dir");
        let source = temp_dir.path().join("source");
        std::fs::create_dir(&source).expect("Failed to create source");

        let source_str = source.to_str().expect("Invalid path");
        let result = sync_specs(source_str, "./target", true).await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_sync_creates_target() {
        let temp_dir = TempDir::new().expect("Failed to create temp dir");
        let source = temp_dir.path().join("source");
        let target = temp_dir.path().join("target");

        std::fs::create_dir(&source).expect("Failed to create source");

        let source_str = source.to_str().expect("Invalid path");
        let target_str = target.to_str().expect("Invalid path");

        let result = sync_specs(source_str, target_str, false).await;
        assert!(result.is_ok());
        assert!(target.exists(), "Target directory should be created");
    }
}