47 lines
1.5 KiB
Rust
47 lines
1.5 KiB
Rust
use std::path::PathBuf;
|
|
|
|
use anyhow::{anyhow, Result};
|
|
|
|
/// Resolve the Codex home that owns both Codex auth.json and cdxs.toml.
|
|
pub fn codex_home(override_path: Option<PathBuf>) -> Result<PathBuf> {
|
|
if let Some(path) = override_path {
|
|
return Ok(expand_home(path));
|
|
}
|
|
if let Ok(raw) = std::env::var("CODEX_HOME") {
|
|
let trimmed = raw.trim().trim_matches('"').trim_matches('\'').trim();
|
|
if !trimmed.is_empty() {
|
|
return Ok(expand_home(PathBuf::from(trimmed)));
|
|
}
|
|
}
|
|
let home = dirs::home_dir().ok_or_else(|| anyhow!("无法获取用户主目录"))?;
|
|
Ok(home.join(".codex"))
|
|
}
|
|
|
|
pub fn config_path(codex_home: &std::path::Path) -> PathBuf {
|
|
// Keep cdxs state next to Codex state so CODEX_HOME fully scopes an install.
|
|
codex_home.join("cdxs.toml")
|
|
}
|
|
|
|
pub fn auth_path(codex_home: &std::path::Path) -> PathBuf {
|
|
// This is the file Codex itself reads for authentication.
|
|
codex_home.join("auth.json")
|
|
}
|
|
|
|
pub fn codex_config_path(codex_home: &std::path::Path) -> PathBuf {
|
|
codex_home.join("config.toml")
|
|
}
|
|
|
|
pub fn expand_home(path: PathBuf) -> PathBuf {
|
|
// PathBuf does not expand ~ on Windows or Unix, so handle the common cases.
|
|
let raw = path.to_string_lossy();
|
|
if raw == "~" {
|
|
return dirs::home_dir().unwrap_or(path);
|
|
}
|
|
if let Some(rest) = raw.strip_prefix("~/").or_else(|| raw.strip_prefix("~\\")) {
|
|
if let Some(home) = dirs::home_dir() {
|
|
return home.join(rest);
|
|
}
|
|
}
|
|
path
|
|
}
|