use anyhow::Result;
use crate::schema::{InstallSkillsData, InstalledSkillSummary, Response};
use crate::skills::{LockEntry, LockFile, Source, install, now_rfc3339, resolve_agents_dir};
pub struct InstallSkillsOpts<'a> {
pub source: &'a str,
pub global: bool,
}
pub fn execute(opts: InstallSkillsOpts<'_>) -> Result<()> {
let source = Source::parse(opts.source)?;
let agents_dir = resolve_agents_dir(opts.global)?;
let installed = install(&source, &agents_dir)?;
let lock_path = agents_dir.join(".skill-lock.json");
let mut lock = LockFile::read(&lock_path)?;
let entry = LockEntry {
name: installed.name.clone(),
source_type: installed.source_str.clone(),
installed_at: now_rfc3339(),
path: installed.path.to_string_lossy().into_owned(),
};
lock.upsert(entry);
lock.write(&lock_path)?;
let data = InstallSkillsData {
skills: vec![InstalledSkillSummary {
name: installed.name,
source_type: installed.source_str,
path: installed.path.to_string_lossy().into_owned(),
}],
global: opts.global,
lock_file_path: lock_path.to_string_lossy().into_owned(),
};
Response::new("install_skills", data).print();
Ok(())
}