liber-cli 0.1.0

AI-agent-readable company directory CLI
//! `liber repos …` verbs.

use std::path::Path;

use serde_json::Value;

use crate::data;
use crate::output::{emit, CliError, Ctx};

pub fn list(dir: &Path, ctx: Ctx, visibility: Option<&str>) -> Result<(), CliError> {
    let data = data::load(dir, "repos")?;
    let arr = data
        .get("repos")
        .and_then(|v| v.as_array())
        .cloned()
        .unwrap_or_default();
    let items: Vec<Value> = arr
        .into_iter()
        .filter(|r| match visibility {
            Some(want) => r.get("visibility").and_then(|v| v.as_str()) == Some(want),
            None => true,
        })
        .collect();
    emit(ctx, &Value::Array(items));
    Ok(())
}

pub fn get(dir: &Path, ctx: Ctx, slug: &str) -> Result<(), CliError> {
    let data = data::load(dir, "repos")?;
    let arr = data
        .get("repos")
        .and_then(|v| v.as_array())
        .cloned()
        .unwrap_or_default();
    for r in arr {
        if r.get("slug").and_then(|v| v.as_str()) == Some(slug) {
            emit(ctx, &r);
            return Ok(());
        }
    }
    Err(CliError::not_found(
        format!("repo not found: {slug}"),
        Some("try `liber repos list` to see all slugs".into()),
    ))
}