diff options
| author | Eskil Queseth <eskilq@kth.se> | 2020-10-23 01:59:32 +0200 |
|---|---|---|
| committer | Eskil Queseth <eskilq@kth.se> | 2020-10-23 01:59:32 +0200 |
| commit | 9ba108ecb309a68ee514c02cee473fa40c621d11 (patch) | |
| tree | da031ce99dadd68265b0f81be472506e2a328bd6 /mumlib/src | |
| parent | cd3c1ad6c508a698314ecc59ae6de320263f740d (diff) | |
| download | mum-9ba108ecb309a68ee514c02cee473fa40c621d11.tar.gz | |
add confirmation when creating config file and add check for where to create the config file
Diffstat (limited to 'mumlib/src')
| -rw-r--r-- | mumlib/src/config.rs | 67 |
1 files changed, 56 insertions, 11 deletions
diff --git a/mumlib/src/config.rs b/mumlib/src/config.rs index aa8a8ed..5c37a2b 100644 --- a/mumlib/src/config.rs +++ b/mumlib/src/config.rs @@ -1,9 +1,9 @@ -use log::*; use serde::{Deserialize, Serialize}; use std::convert::TryFrom; use std::fs; use toml::Value; use toml::value::Array; +use std::path::Path; #[derive(Debug, Deserialize, Serialize)] struct TOMLConfig { @@ -11,15 +11,15 @@ struct TOMLConfig { servers: Option<Array>, } -#[derive(Clone, Debug)] +#[derive(Clone, Debug, Default)] pub struct Config { pub audio: Option<AudioConfig>, pub servers: Option<Vec<ServerConfig>>, } impl Config { - pub fn write_default_cfg(&self) -> Result<(), std::io::Error> { - let path = get_cfg_path(); + pub fn write_default_cfg(&self, create: bool) -> Result<(), std::io::Error> { + let path = if create { get_creatable_cfg_path() } else { get_cfg_path() }; let path = std::path::Path::new(&path); // Possible race here. It's fine since it shows when: // 1) the file doesn't exist when checked and is then created @@ -29,12 +29,11 @@ impl Config { // should work. Unless the file is removed AND the permissions // change, but then we don't have permissions so we can't // do anything anyways. - if !path.exists() { - warn!("config file {} does not exist, ignoring", path.display()); - Ok(()) - } else { - fs::write(path, toml::to_string(&TOMLConfig::from(self.clone())).unwrap()) + if !create && !path.exists() { + return Ok(()); } + + fs::write(path, toml::to_string(&TOMLConfig::from(self.clone())).unwrap()) } } @@ -52,8 +51,54 @@ pub struct ServerConfig { pub password: Option<String>, } -fn get_cfg_path() -> String { - ".mumdrc".to_string() //TODO XDG_CONFIG and whatever +pub fn get_cfg_path() -> String { + if let Ok(var) = std::env::var("XDG_CONFIG_HOME") { + let path = format!("{}/mumdrc", var); + if Path::new(&path).exists() { + return path; + } + } else if let Ok(var) = std::env::var("HOME") { + let path = format!("{}/.config/mumdrc", var); + if Path::new(&path).exists() { + return path; + } + } + + "/etc/mumdrc".to_string() +} + +pub fn get_creatable_cfg_path() -> String { + if let Ok(var) = std::env::var("XDG_CONFIG_HOME") { + let path = format!("{}/mumdrc", var); + if !Path::new(&path).exists() { + return path; + } + } else if let Ok(var) = std::env::var("HOME") { + let path = format!("{}/.config/mumdrc", var); + if !Path::new(&path).exists() { + return path; + } + } + + "/etc/mumdrc".to_string() +} + +pub fn cfg_exists() -> bool { + if let Ok(var) = std::env::var("XDG_CONFIG_HOME") { + let path = format!("{}/mumdrc", var); + if Path::new(&path).exists() { + return true; + } + } else if let Ok(var) = std::env::var("HOME") { + let path = format!("{}/.config/mumdrc", var); + if Path::new(&path).exists() { + return true; + } + } else if Path::new("/etc/mumdrc").exists() { + return true; + } + + false } impl TryFrom<TOMLConfig> for Config { |
