diff options
author | Jesse Luehrs <doy@tozt.net> | 2019-11-15 13:11:07 -0500 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2019-11-15 13:11:07 -0500 |
commit | bbf15cfef8134da720a27bd71a93efcb8467025b (patch) | |
tree | aa58a5d7c1862fcdd6c8629651f664aa12c70f66 /teleterm/src/dirs.rs | |
parent | fe4fa53dbbb6030beae2094e33d1db008532ae3c (diff) | |
download | teleterm-bbf15cfef8134da720a27bd71a93efcb8467025b.tar.gz teleterm-bbf15cfef8134da720a27bd71a93efcb8467025b.zip |
use workspaces
Diffstat (limited to 'teleterm/src/dirs.rs')
-rw-r--r-- | teleterm/src/dirs.rs | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/teleterm/src/dirs.rs b/teleterm/src/dirs.rs new file mode 100644 index 0000000..bd4741d --- /dev/null +++ b/teleterm/src/dirs.rs @@ -0,0 +1,98 @@ +use crate::prelude::*; + +pub struct Dirs { + project_dirs: Option<directories::ProjectDirs>, +} + +impl Dirs { + pub fn new() -> Self { + Self { + project_dirs: directories::ProjectDirs::from("", "", "teleterm"), + } + } + + pub fn create_all(&self) -> Result<()> { + if let Some(filename) = self.data_dir() { + std::fs::create_dir_all(filename).with_context(|| { + crate::error::CreateDir { + filename: filename.to_string_lossy(), + } + })?; + } + Ok(()) + } + + fn has_home(&self) -> bool { + directories::BaseDirs::new().map_or(false, |dirs| { + dirs.home_dir() != std::path::Path::new("/") + }) + } + + fn global_config_dir(&self) -> &std::path::Path { + std::path::Path::new("/etc/teleterm") + } + + fn config_dir(&self) -> Option<&std::path::Path> { + if self.has_home() { + self.project_dirs + .as_ref() + .map(directories::ProjectDirs::config_dir) + } else { + None + } + } + + pub fn config_file( + &self, + name: &str, + must_exist: bool, + ) -> Option<std::path::PathBuf> { + if let Some(config_dir) = self.config_dir() { + let file = config_dir.join(name); + if !must_exist || file.exists() { + return Some(file); + } + } + + let file = self.global_config_dir().join(name); + if !must_exist || file.exists() { + return Some(file); + } + + None + } + + fn global_data_dir(&self) -> &std::path::Path { + std::path::Path::new("/var/lib/teleterm") + } + + fn data_dir(&self) -> Option<&std::path::Path> { + if self.has_home() { + self.project_dirs + .as_ref() + .map(directories::ProjectDirs::data_dir) + } else { + None + } + } + + pub fn data_file( + &self, + name: &str, + must_exist: bool, + ) -> Option<std::path::PathBuf> { + if let Some(data_dir) = self.data_dir() { + let file = data_dir.join(name); + if !must_exist || file.exists() { + return Some(file); + } + } + + let file = self.global_data_dir().join(name); + if !must_exist || file.exists() { + return Some(file); + } + + None + } +} |