cleanpy 0.1.3

Python 3.12+ 最佳实践知识库 - MCP 服务器
Documentation
// Installation functionality for Claude Code integration

use anyhow::{Context, Result};
use std::fs;
use std::path::PathBuf;

use crate::cli::Scope;

/// Install slash command to Claude Code
pub fn install_command(scope: Scope) -> Result<()> {
    /// Helper function to install a single file
    fn install_file(
        base_path: &std::path::Path,
        relative_path: &str,
        content: &str,
        description: &str,
    ) -> Result<()> {
        let target_path = base_path.join(relative_path);

        // Create parent directory if it doesn't exist
        if let Some(parent) = target_path.parent() {
            fs::create_dir_all(parent).context(format!("无法创建目录: {}", parent.display()))?;
        }

        // Write the file
        fs::write(&target_path, content)
            .context(format!("无法写入文件: {}", target_path.display()))?;

        println!("✓ 已安装 {} 到: {}", description, target_path.display());

        Ok(())
    }

    // Determine base path based on scope
    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"),
    };

    // Define files to install: (relative_path, content, description)
    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",
        ),
    ];

    // Install all files
    for (relative_path, content, description) in &files {
        install_file(&base_path, relative_path, content, description)?;
    }

    println!("\n所有文件安装完成!");
    println!("现在可以在 Claude Code 中使用 /py 命令");

    Ok(())
}