opencode_cloud_core/config/
paths.rs1use std::path::PathBuf;
8
9pub fn get_config_dir() -> Option<PathBuf> {
16 #[cfg(any(target_os = "linux", target_os = "macos"))]
17 {
18 directories::BaseDirs::new()
19 .map(|dirs| dirs.home_dir().join(".config").join("opencode-cloud"))
20 }
21 #[cfg(target_os = "windows")]
22 {
23 directories::BaseDirs::new()
24 .and_then(|dirs| dirs.config_dir().map(|d| d.to_path_buf()))
25 .map(|d| d.join("opencode-cloud"))
26 }
27 #[cfg(not(any(target_os = "linux", target_os = "macos", target_os = "windows")))]
28 {
29 None
30 }
31}
32
33pub fn get_data_dir() -> Option<PathBuf> {
40 #[cfg(any(target_os = "linux", target_os = "macos"))]
41 {
42 directories::BaseDirs::new().map(|dirs| {
43 dirs.home_dir()
44 .join(".local")
45 .join("share")
46 .join("opencode-cloud")
47 })
48 }
49 #[cfg(target_os = "windows")]
50 {
51 directories::BaseDirs::new()
52 .and_then(|dirs| dirs.data_local_dir().map(|d| d.to_path_buf()))
53 .map(|d| d.join("opencode-cloud"))
54 }
55 #[cfg(not(any(target_os = "linux", target_os = "macos", target_os = "windows")))]
56 {
57 None
58 }
59}
60
61pub fn get_config_path() -> Option<PathBuf> {
65 get_config_dir().map(|d| d.join("config.json"))
66}
67
68pub fn get_pid_path() -> Option<PathBuf> {
72 get_data_dir().map(|d| d.join("opencode-cloud.pid"))
73}
74
75#[cfg(test)]
76mod tests {
77 use super::*;
78
79 #[test]
80 fn test_config_dir_exists() {
81 let dir = get_config_dir();
82 assert!(dir.is_some());
83 let path = dir.unwrap();
84 assert!(path.ends_with("opencode-cloud"));
85 }
86
87 #[test]
88 fn test_data_dir_exists() {
89 let dir = get_data_dir();
90 assert!(dir.is_some());
91 let path = dir.unwrap();
92 assert!(path.ends_with("opencode-cloud"));
93 }
94
95 #[test]
96 fn test_config_path_ends_with_config_json() {
97 let path = get_config_path();
98 assert!(path.is_some());
99 assert!(path.unwrap().ends_with("config.json"));
100 }
101
102 #[test]
103 fn test_pid_path_ends_with_pid() {
104 let path = get_pid_path();
105 assert!(path.is_some());
106 assert!(path.unwrap().ends_with("opencode-cloud.pid"));
107 }
108}