cat-fetch 0.1.1

MusKitty multi-repo source management CLI — one command to clone, build, test, push, and publish 8 independent crate repositories
//! Token detection — checks environment on startup.

/// Token status for a single credential.
pub struct TokenStatus {
    pub name: &'static str,
    pub label: &'static str,
    pub present: bool,
}

/// Run at startup before any command. Checks if required tokens are loaded
/// and prints a summary. Returns false if critical tokens are missing for
/// operations that require auth.
pub fn check_on_startup(action_needs_auth: bool) {
    let tokens = [
        TokenStatus {
            name: "CRATES_GIT_TOKEN",
            label: "crate repos (muskitty-dev)",
            present: std::env::var("CRATES_GIT_TOKEN").is_ok(),
        },
        TokenStatus {
            name: "MAIN_GIT_TOKEN",
            label: "main repo (Ink-dark)",
            present: std::env::var("MAIN_GIT_TOKEN").is_ok(),
        },
        TokenStatus {
            name: "CARGO_REGISTRY_TOKEN",
            label: "crates.io publish",
            present: std::env::var("CARGO_REGISTRY_TOKEN").is_ok(),
        },
    ];

    let all_ok = tokens.iter().all(|t| t.present);
    let any_missing = tokens.iter().any(|t| !t.present);

    if all_ok {
        // All good, just a quiet note
        eprintln!("\x1b[36m[cat] tokens: ✓ all 3 loaded\x1b[0m");
    } else if any_missing {
        eprintln!("\x1b[33m[cat] token status:\x1b[0m");
        for t in &tokens {
            if t.present {
                eprintln!("  \x1b[32m✓ {}\x1b[0m — {}", t.name, t.label);
            } else {
                eprintln!("  \x1b[31m✗ {}\x1b[0m — {} (missing)", t.name, t.label);
            }
        }

        if action_needs_auth {
            eprintln!();
            eprintln!(
                "\x1b[33m  Run '. .\\env.ps1' to load tokens, or set env vars manually.\x1b[0m"
            );
        }
    }
}

/// Get the token value for a given token_key from repos.toml.
/// "main" → MAIN_GIT_TOKEN, "crates" → CRATES_GIT_TOKEN
#[allow(dead_code)]
pub fn get_token(token_key: &str) -> Option<String> {
    let env_var = match token_key {
        "main" => "MAIN_GIT_TOKEN",
        _ => "CRATES_GIT_TOKEN",
    };
    std::env::var(env_var).ok()
}