1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
//! Platform-specific path resolution for template directories
use std::path::PathBuf;
#[derive(Debug)]
/// Utility for resolving template paths
pub struct PathResolver;
impl PathResolver {
/// Get system-wide template directory
pub fn system_template_dir() -> PathBuf {
#[cfg(unix)]
{
PathBuf::from("/usr/local/share/cert-x-gen/templates")
}
#[cfg(windows)]
{
let program_data =
std::env::var("ProgramData").unwrap_or_else(|_| "C:\\ProgramData".to_string());
PathBuf::from(program_data)
.join("cert-x-gen")
.join("templates")
}
}
/// Resolve the base home directory for cert-x-gen's per-user files.
///
/// Honors the `CERT_X_GEN_HOME` override (relocation / test isolation) before
/// falling back to the OS home dir. The override works on every platform,
/// unlike `$HOME`, which `dirs::home_dir()` ignores on Windows.
// @comment -- "resolves the per-user base dir; CERT_X_GEN_HOME overrides the OS home dir for relocation and cross-platform test isolation"
pub(crate) fn user_home() -> Option<PathBuf> {
if let Some(dir) = std::env::var_os("CERT_X_GEN_HOME") {
if !dir.is_empty() {
return Some(PathBuf::from(dir));
}
}
dirs::home_dir()
}
/// Get user-specific template directory
pub fn user_template_dir() -> PathBuf {
Self::user_home()
.map(|h| h.join(".cert-x-gen/templates"))
.unwrap_or_else(|| PathBuf::from("~/.cert-x-gen/templates"))
}
/// Get local project template directory
pub fn local_template_dir() -> PathBuf {
PathBuf::from("./templates")
}
/// Get user config directory
pub fn user_config_dir() -> PathBuf {
Self::user_home()
.map(|h| h.join(".cert-x-gen"))
.unwrap_or_else(|| PathBuf::from("~/.cert-x-gen"))
}
/// Get template cache directory
pub fn cache_dir() -> PathBuf {
Self::user_config_dir().join("cache")
}
/// Get all template directories in priority order (highest first)
pub fn all_template_dirs() -> Vec<PathBuf> {
vec![
Self::local_template_dir(), // Priority 3 (highest)
Self::user_template_dir(), // Priority 2
Self::system_template_dir(), // Priority 1 (lowest)
]
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_path_resolution() {
let system = PathResolver::system_template_dir();
let user = PathResolver::user_template_dir();
let local = PathResolver::local_template_dir();
assert!(system.is_absolute());
assert!(user.to_str().unwrap().contains(".cert-x-gen"));
assert_eq!(local, PathBuf::from("./templates"));
}
}