use std::path::{Path, PathBuf};
use crate::error::ZigError;
pub fn global_workflows_dir_from(home: &Path) -> PathBuf {
home.join(".zig").join("workflows")
}
pub fn global_workflows_dir() -> Option<PathBuf> {
std::env::var("HOME")
.ok()
.map(|home| global_workflows_dir_from(Path::new(&home)))
}
pub fn ensure_global_workflows_dir() -> Result<PathBuf, ZigError> {
let dir = global_workflows_dir()
.ok_or_else(|| ZigError::Io("HOME environment variable not set".into()))?;
if !dir.exists() {
std::fs::create_dir_all(&dir)
.map_err(|e| ZigError::Io(format!("failed to create {}: {e}", dir.display())))?;
}
Ok(dir)
}
pub fn global_base_dir() -> Option<PathBuf> {
std::env::var("HOME")
.ok()
.map(|h| Path::new(&h).join(".zig"))
}
pub fn sanitize_project_path(path: &str) -> String {
path.trim_start_matches('/').replace('/', "-")
}
fn find_git_root(start: &Path) -> Option<PathBuf> {
let mut current = start;
loop {
if current.join(".git").exists() {
return Some(current.to_path_buf());
}
current = current.parent()?;
}
}
pub fn project_dir(root: Option<&str>) -> Option<PathBuf> {
let base = global_base_dir()?;
if let Some(r) = root {
return Some(base.join("projects").join(sanitize_project_path(r)));
}
let cwd = std::env::current_dir().ok()?;
if let Some(git_root) = find_git_root(&cwd) {
let sanitized = sanitize_project_path(&git_root.to_string_lossy());
return Some(base.join("projects").join(sanitized));
}
Some(base)
}
pub fn project_logs_dir(root: Option<&str>) -> Option<PathBuf> {
project_dir(root).map(|p| p.join("logs"))
}
pub fn project_sessions_dir(root: Option<&str>) -> Option<PathBuf> {
project_logs_dir(root).map(|p| p.join("sessions"))
}
pub fn project_index_path(root: Option<&str>) -> Option<PathBuf> {
project_logs_dir(root).map(|p| p.join("index.json"))
}
pub fn global_sessions_index_path() -> Option<PathBuf> {
global_base_dir().map(|p| p.join("sessions_index.json"))
}
pub fn ensure_project_sessions_dir(root: Option<&str>) -> Result<PathBuf, ZigError> {
let dir = project_sessions_dir(root)
.ok_or_else(|| ZigError::Io("HOME environment variable not set".into()))?;
if !dir.exists() {
std::fs::create_dir_all(&dir)
.map_err(|e| ZigError::Io(format!("failed to create {}: {e}", dir.display())))?;
}
Ok(dir)
}
#[cfg(test)]
#[path = "paths_tests.rs"]
mod tests;