use crate::core::{project_library, skill_store::SkillStore, tool_adapters};
use clap::{Args, Subcommand};
use std::path::PathBuf;
#[derive(Args, Debug)]
pub(super) struct ProjectArgs {
#[arg(long, default_value = ".", global = true)]
path: PathBuf,
#[command(subcommand)]
command: ProjectCommand,
}
#[derive(Subcommand, Debug)]
enum ProjectCommand {
Status,
Add {
reference: String,
#[arg(long = "agent", required = true)]
agents: Vec<String>,
#[arg(long)]
copy: bool,
},
Deploy {
name: String,
#[arg(long = "agent", required = true)]
agents: Vec<String>,
},
Update { name: String, reference: String },
Repair { name: String },
Enable { name: String },
Disable { name: String },
Remove { name: String },
Undeploy {
name: String,
#[arg(long = "agent")]
agent: String,
},
}
pub(super) fn run(
args: &ProjectArgs,
store: Option<&SkillStore>,
json: bool,
) -> anyhow::Result<()> {
let targets = |keys: &[String]| -> anyhow::Result<Vec<_>> {
keys.iter()
.map(|key| {
let target = if let Some(store) = store {
tool_adapters::find_adapter_with_store(store, key)
} else {
tool_adapters::find_adapter(key)
};
target.ok_or_else(|| anyhow::anyhow!("Unknown agent: {key}"))
})
.collect()
};
let root = &args.path;
let report = match &args.command {
ProjectCommand::Status => project_library::status(root)?,
ProjectCommand::Add {
reference,
agents,
copy,
} => {
let store =
store.ok_or_else(|| anyhow::anyhow!("Personal library required for add"))?;
let skill = super::resolve_skill(store, reference)?;
let preference = crate::core::deployment_preferences::Preference::load(
store,
crate::core::deployment_preferences::Scope::Project,
)?;
project_library::install_with_mode(
root,
&skill,
&targets(agents)?,
matches!(
preference.for_install(copy.then_some(true)),
crate::core::sync_engine::SyncMode::Copy
),
)?
}
ProjectCommand::Deploy { name, agents } => {
project_library::deploy(root, name, &targets(agents)?)?
}
ProjectCommand::Update { name, reference } => {
let store =
store.ok_or_else(|| anyhow::anyhow!("Personal library required for update"))?;
let skill = super::resolve_skill(store, reference)?;
project_library::update(root, name, &skill, false)?
}
ProjectCommand::Remove { name } => project_library::remove(root, name)?,
ProjectCommand::Repair { name } => project_library::repair(root, name)?,
ProjectCommand::Enable { name } => project_library::set_enabled(root, name, true)?,
ProjectCommand::Disable { name } => project_library::set_enabled(root, name, false)?,
ProjectCommand::Undeploy { name, agent } => {
project_library::remove_target(root, name, agent)?
}
};
super::print_json(&report, json);
Ok(())
}
impl ProjectArgs {
pub(super) fn needs_personal_library(&self) -> bool {
matches!(
self.command,
ProjectCommand::Add { .. } | ProjectCommand::Update { .. }
)
}
}