pets_tools 0.2.0

MusKitty multi-repo source management CLI — one command to clone, build, test, push, and publish 9 independent crate repositories
//! `pets status <project>` — git status overview for all repos.

use crate::config::Config;
use crate::git;
use crate::ui;
use crate::StatusArgs;

pub fn run(config: Config, args: &StatusArgs) {
    if !args.json {
        ui::heading(&format!(
            "Status Overview — {} ({} repos)",
            config.project_name,
            config.repos.len() + 1
        ));
        println!();
    }

    let mut rows: Vec<(String, String, String, String, String)> = Vec::new();
    let mut json_parts: Vec<String> = Vec::new();

    // Check main repo
    append_status(
        &mut rows,
        &mut json_parts,
        &config.main_repo.name,
        &config.main_repo.dir,
    );

    // Check each crate repo
    for repo in &config.repos {
        append_status(&mut rows, &mut json_parts, &repo.name, &repo.abs_dir);
    }

    if args.json {
        println!("[{}]", json_parts.join(","));
    } else {
        ui::status_table(&rows);
    }
}

fn append_status(
    rows: &mut Vec<(String, String, String, String, String)>,
    json_parts: &mut Vec<String>,
    name: &str,
    dir: &std::path::Path,
) {
    match git::status(dir) {
        Ok(s) => {
            let status_str = if s.clean { "clean" } else { "dirty" };
            let ab = format!("{} / {}", s.ahead, s.behind);
            let branch = s.branch.clone();
            let last_commit = s.last_commit.clone();
            rows.push((
                name.to_string(),
                branch,
                status_str.to_string(),
                ab,
                last_commit,
            ));
            json_parts.push(format!(
                r#"{{"repo":"{}","branch":"{}","clean":{},"ahead":{},"behind":{},"last_commit":"{}"}}"#,
                name, s.branch, s.clean, s.ahead, s.behind, s.last_commit,
            ));
        }
        Err(e) => {
            rows.push((
                name.to_string(),
                "—".into(),
                "ERROR".into(),
                "—".into(),
                e.clone(),
            ));
            json_parts.push(format!(
                r#"{{"repo":"{}","error":"{}"}}"#,
                name,
                e.replace('"', r#"\""#),
            ));
        }
    }
}