use anyhow::{Context, Result};
use std::fs;
use std::path::PathBuf;
use crate::cli::Scope;
pub fn install_command(scope: Scope) -> Result<()> {
fn install_file(
base_path: &std::path::Path,
relative_path: &str,
content: &str,
description: &str,
) -> Result<()> {
let target_path = base_path.join(relative_path);
if let Some(parent) = target_path.parent() {
fs::create_dir_all(parent).context(format!("无法创建目录: {}", parent.display()))?;
}
fs::write(&target_path, content)
.context(format!("无法写入文件: {}", target_path.display()))?;
println!("✓ 已安装 {} 到: {}", description, target_path.display());
Ok(())
}
let base_path = match scope {
Scope::Local => {
let home = std::env::var("HOME")
.or_else(|_| std::env::var("USERPROFILE"))
.context("无法获取用户主目录")?;
PathBuf::from(home).join(".claude")
}
Scope::Project => PathBuf::from(".claude"),
};
let files = [
("commands/py.md", crate::PY_MD_CONTENT, "slash command"),
(
"agents/python-checker.md",
crate::PYTHON_CHECKER_MD_CONTENT,
"python-checker agent",
),
(
"agents/python-implementer.md",
crate::PYTHON_IMPLEMENTER_MD_CONTENT,
"python-implementer agent",
),
];
for (relative_path, content, description) in &files {
install_file(&base_path, relative_path, content, description)?;
}
println!("\n所有文件安装完成!");
println!("现在可以在 Claude Code 中使用 /py 命令");
Ok(())
}