envelope_cli/config/
paths.rs1use std::path::PathBuf;
12
13use crate::error::EnvelopeError;
14
15#[derive(Debug, Clone)]
17pub struct EnvelopePaths {
18 base_dir: PathBuf,
20}
21
22impl EnvelopePaths {
23 pub fn new() -> Result<Self, EnvelopeError> {
34 let base_dir = if let Ok(custom) = std::env::var("ENVELOPE_CLI_DATA_DIR") {
35 PathBuf::from(custom)
36 } else {
37 resolve_default_path()?
38 };
39
40 Ok(Self { base_dir })
41 }
42
43 pub fn with_base_dir(base_dir: PathBuf) -> Self {
45 Self { base_dir }
46 }
47
48 pub fn base_dir(&self) -> &PathBuf {
50 &self.base_dir
51 }
52
53 pub fn config_dir(&self) -> PathBuf {
55 self.base_dir.clone()
56 }
57
58 pub fn data_dir(&self) -> PathBuf {
60 self.base_dir.join("data")
61 }
62
63 pub fn backup_dir(&self) -> PathBuf {
65 self.base_dir.join("backups")
66 }
67
68 pub fn settings_file(&self) -> PathBuf {
70 self.base_dir.join("config.json")
71 }
72
73 pub fn audit_log(&self) -> PathBuf {
75 self.base_dir.join("audit.log")
76 }
77
78 pub fn accounts_file(&self) -> PathBuf {
80 self.data_dir().join("accounts.json")
81 }
82
83 pub fn transactions_file(&self) -> PathBuf {
85 self.data_dir().join("transactions.json")
86 }
87
88 pub fn budget_file(&self) -> PathBuf {
90 self.data_dir().join("budget.json")
91 }
92
93 pub fn allocations_file(&self) -> PathBuf {
95 self.data_dir().join("allocations.json")
96 }
97
98 pub fn payees_file(&self) -> PathBuf {
100 self.data_dir().join("payees.json")
101 }
102
103 pub fn targets_file(&self) -> PathBuf {
105 self.data_dir().join("targets.json")
106 }
107
108 pub fn ensure_directories(&self) -> Result<(), EnvelopeError> {
115 std::fs::create_dir_all(&self.base_dir)
116 .map_err(|e| EnvelopeError::Io(format!("Failed to create base directory: {}", e)))?;
117
118 std::fs::create_dir_all(self.data_dir())
119 .map_err(|e| EnvelopeError::Io(format!("Failed to create data directory: {}", e)))?;
120
121 std::fs::create_dir_all(self.backup_dir())
122 .map_err(|e| EnvelopeError::Io(format!("Failed to create backup directory: {}", e)))?;
123
124 Ok(())
125 }
126
127 pub fn is_initialized(&self) -> bool {
129 self.settings_file().exists()
130 }
131}
132
133#[cfg(not(windows))]
135fn resolve_default_path() -> Result<PathBuf, EnvelopeError> {
136 let config_base = std::env::var("XDG_CONFIG_HOME")
138 .map(PathBuf::from)
139 .unwrap_or_else(|_| {
140 let home = std::env::var("HOME").expect("HOME environment variable not set");
141 PathBuf::from(home).join(".config")
142 });
143 Ok(config_base.join("envelope-cli"))
144}
145
146#[cfg(windows)]
148fn resolve_default_path() -> Result<PathBuf, EnvelopeError> {
149 let appdata = std::env::var("APPDATA")
151 .map_err(|_| EnvelopeError::Config("Could not determine APPDATA directory".into()))?;
152 Ok(PathBuf::from(appdata).join("envelope-cli"))
153}
154
155#[cfg(test)]
156mod tests {
157 use super::*;
158 use std::env;
159 use tempfile::TempDir;
160
161 #[test]
162 fn test_custom_base_dir() {
163 let temp_dir = TempDir::new().unwrap();
164 let paths = EnvelopePaths::with_base_dir(temp_dir.path().to_path_buf());
165
166 assert_eq!(paths.base_dir(), temp_dir.path());
167 assert_eq!(paths.data_dir(), temp_dir.path().join("data"));
168 assert_eq!(paths.backup_dir(), temp_dir.path().join("backups"));
169 }
170
171 #[test]
172 fn test_env_var_override() {
173 let temp_dir = TempDir::new().unwrap();
174 let custom_path = temp_dir.path().to_str().unwrap();
175
176 env::set_var("ENVELOPE_CLI_DATA_DIR", custom_path);
178
179 let paths = EnvelopePaths::new().unwrap();
180 assert_eq!(paths.base_dir(), temp_dir.path());
181
182 env::remove_var("ENVELOPE_CLI_DATA_DIR");
184 }
185
186 #[test]
187 fn test_ensure_directories() {
188 let temp_dir = TempDir::new().unwrap();
189 let paths = EnvelopePaths::with_base_dir(temp_dir.path().to_path_buf());
190
191 paths.ensure_directories().unwrap();
192
193 assert!(paths.data_dir().exists());
194 assert!(paths.backup_dir().exists());
195 }
196
197 #[test]
198 fn test_file_paths() {
199 let temp_dir = TempDir::new().unwrap();
200 let paths = EnvelopePaths::with_base_dir(temp_dir.path().to_path_buf());
201
202 assert_eq!(paths.settings_file(), temp_dir.path().join("config.json"));
203 assert_eq!(
204 paths.accounts_file(),
205 temp_dir.path().join("data").join("accounts.json")
206 );
207 }
208}