use anyhow::Result;
use clap::{Args, Subcommand};
use serde_json::Value;
use crate::cli::{open_ctx, CommonOpts};
#[derive(Debug, Subcommand)]
pub enum Verb {
#[command(name = "list-stale")]
ListStale(ListStaleArgs),
Gather(GatherArgs),
}
#[derive(Debug, Args)]
pub struct ListStaleArgs {
#[arg(long = "threshold-hours")]
pub threshold_hours: Option<i64>,
#[arg(long)]
pub scope: Option<String>,
#[arg(long)]
pub limit: Option<usize>,
#[arg(long)]
pub project: Option<std::path::PathBuf>,
#[arg(long)]
pub json: bool,
#[arg(long = "no-color")]
pub no_color: bool,
}
pub async fn dispatch(verb: Verb) -> Result<()> {
match verb {
Verb::ListStale(args) => run_list_stale(args).await,
Verb::Gather(args) => run_gather(args).await,
}
}
async fn run_list_stale(args: ListStaleArgs) -> Result<()> {
let common = CommonOpts {
project: args.project.clone(),
json: args.json,
no_color: args.no_color,
};
let output = common.output();
let ctx = open_ctx(&common).await?;
let mut tool_args = serde_json::Map::new();
if let Some(t) = args.threshold_hours {
tool_args.insert("threshold_hours".into(), Value::Number(t.into()));
}
if let Some(s) = &args.scope {
tool_args.insert("scope".into(), Value::String(s.clone()));
}
if let Some(l) = args.limit {
tool_args.insert("limit".into(), Value::Number(l.into()));
}
let v = crate::librarian::tools::refresh_stale::call(&ctx, Value::Object(tool_args)).await?;
crate::cli::format::print(&v, &output)?;
Ok(())
}
#[derive(Debug, Args)]
pub struct GatherArgs {
pub id: String,
#[arg(long)]
pub project: Option<std::path::PathBuf>,
#[arg(long)]
pub json: bool,
#[arg(long = "no-color")]
pub no_color: bool,
}
async fn run_gather(args: GatherArgs) -> Result<()> {
let common = CommonOpts {
project: args.project.clone(),
json: args.json,
no_color: args.no_color,
};
let output = common.output();
let ctx = open_ctx(&common).await?;
let tool_args = serde_json::json!({
"id": args.id,
});
let v = crate::librarian::tools::refresh::call(&ctx, tool_args).await?;
crate::cli::format::print(&v, &output)?;
Ok(())
}