use std::path::Path;
use serde_json::Value;
use crate::data;
use crate::output::{emit, CliError, Ctx};
pub fn list(dir: &Path, ctx: Ctx) -> Result<(), CliError> {
let data = data::load(dir, "products")?;
let arr = data
.get("products")
.cloned()
.unwrap_or(Value::Array(vec![]));
emit(ctx, &arr);
Ok(())
}
pub fn get(dir: &Path, ctx: Ctx, slug: &str) -> Result<(), CliError> {
let data = data::load(dir, "products")?;
let arr = data
.get("products")
.and_then(|v| v.as_array())
.cloned()
.unwrap_or_default();
for p in arr {
if p.get("slug").and_then(|v| v.as_str()) == Some(slug) {
emit(ctx, &p);
return Ok(());
}
}
Err(CliError::not_found(
format!("product not found: {slug}"),
Some("try `liber products list` to see all slugs".into()),
))
}