oc-session 0.1.0

Global OpenCode session browser and resume tool
use crate::cli;
use anyhow::Context;
use std::{fs, path::PathBuf};

const SKILL_MD: &str = include_str!("../../skills/oc-session/SKILL.md");

pub fn run(args: cli::SkillArgs) -> anyhow::Result<()> {
    match args.command {
        cli::SkillCommand::Install(args) => install(args.force),
        cli::SkillCommand::Print => {
            print!("{SKILL_MD}");
            Ok(())
        }
        cli::SkillCommand::Path => {
            println!("{}", skill_path()?.display());
            Ok(())
        }
    }
}

fn install(force: bool) -> anyhow::Result<()> {
    let path = skill_path()?;
    if path.exists() && !force {
        anyhow::bail!(
            "skill already exists at {} (use --force to overwrite)",
            path.display()
        );
    }
    let parent = path.parent().context("skill path has no parent")?;
    fs::create_dir_all(parent).with_context(|| format!("create {}", parent.display()))?;
    fs::write(&path, SKILL_MD).with_context(|| format!("write {}", path.display()))?;
    println!("installed skill to {}", path.display());
    println!("restart OpenCode to load the skill");
    Ok(())
}

fn skill_path() -> anyhow::Result<PathBuf> {
    let home = dirs::home_dir().context("could not determine home directory")?;
    Ok(home
        .join(".config")
        .join("opencode")
        .join("skills")
        .join("oc-session")
        .join("SKILL.md"))
}