use std::path::PathBuf;
use crate::error::{Error, Result};
pub const ENV_BIN: &str = "STACKQL_MCP_BIN";
pub const ENV_BUNDLE: &str = "STACKQL_MCP_BUNDLE";
pub fn home_dir() -> Result<PathBuf> {
if let Some(home) = std::env::var_os("HOME").filter(|v| !v.is_empty()) {
return Ok(PathBuf::from(home));
}
if cfg!(windows) {
if let Some(profile) = std::env::var_os("USERPROFILE").filter(|v| !v.is_empty()) {
return Ok(PathBuf::from(profile));
}
}
Err(Error::NoHomeDir)
}
pub fn default_approot() -> Result<PathBuf> {
Ok(home_dir()?.join(".stackql"))
}
pub fn bin_cache_root() -> Result<PathBuf> {
Ok(default_approot()?.join("mcp-server-bin"))
}
pub fn bundle_cache_dir(version: &str, platform_key: &str) -> Result<PathBuf> {
Ok(bin_cache_root()?.join(version).join(platform_key))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn cache_dir_matches_the_shared_layout() {
let dir = bundle_cache_dir("0.10.500", "linux-x64").unwrap();
let suffix: PathBuf = [".stackql", "mcp-server-bin", "0.10.500", "linux-x64"]
.iter()
.collect();
assert!(
dir.ends_with(&suffix),
"{} should end with {}",
dir.display(),
suffix.display()
);
}
}