diff options
| author | Gustav Sörnäs <gustav@sornas.net> | 2021-05-25 21:00:18 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-05-25 21:00:18 +0200 |
| commit | ea8b1906e14c3b319d3ad184b6d7cfc507c23b4f (patch) | |
| tree | 6fb5dd74ab3ca333591778bcf91d45fe988c2cd4 | |
| parent | 4473bdd1536699c53f27085dd7d4d5d14dd93508 (diff) | |
| parent | a5a7a524981005c63931b62567ffca355965b2ba (diff) | |
| download | mum-ea8b1906e14c3b319d3ad184b6d7cfc507c23b4f.tar.gz | |
Merge pull request #85 from mum-rs/config-paths
remove cfg_exists, pass paths explicitly
| -rw-r--r-- | mumctl/src/main.rs | 11 | ||||
| -rw-r--r-- | mumd/src/state.rs | 4 | ||||
| -rw-r--r-- | mumlib/src/config.rs | 29 |
3 files changed, 12 insertions, 32 deletions
diff --git a/mumctl/src/main.rs b/mumctl/src/main.rs index 5abed50..191a973 100644 --- a/mumctl/src/main.rs +++ b/mumctl/src/main.rs @@ -211,7 +211,7 @@ fn main() { } } fn match_opt() -> Result<(), Error> { - let mut config = config::read_default_cfg()?; + let mut config = config::read_cfg(&config::default_cfg_path())?; let opt = Mum::from_args(); match opt.command { @@ -351,18 +351,19 @@ fn match_opt() -> Result<(), Error> { } } - if !config::cfg_exists() { + let config_path = config::default_cfg_path(); + if !config_path.exists() { println!( "Config file not found. Create one in {}? [Y/n]", - config::default_cfg_path().display(), + config_path.display(), ); let stdin = std::io::stdin(); let response = stdin.lock().lines().next(); if let Some(Ok(true)) = response.map(|e| e.map(|e| &e == "Y")) { - config.write_default_cfg(true)?; + config.write(&config_path, true)?; } } else { - config.write_default_cfg(false)?; + config.write(&config_path, false)?; } Ok(()) } diff --git a/mumd/src/state.rs b/mumd/src/state.rs index 45e7301..46df421 100644 --- a/mumd/src/state.rs +++ b/mumd/src/state.rs @@ -65,7 +65,7 @@ pub struct State { impl State { pub fn new() -> Result<Self, StateError> { - let config = mumlib::config::read_default_cfg()?; + let config = mumlib::config::read_cfg(&mumlib::config::default_cfg_path())?; let phase_watcher = watch::channel(StatePhase::Disconnected); let audio_input = AudioInput::new( config.audio.input_volume.unwrap_or(1.0), @@ -574,7 +574,7 @@ impl State { } pub fn reload_config(&mut self) { - match mumlib::config::read_default_cfg() { + match mumlib::config::read_cfg(&mumlib::config::default_cfg_path()) { Ok(config) => { self.config = config; } diff --git a/mumlib/src/config.rs b/mumlib/src/config.rs index 9394b85..587d8d0 100644 --- a/mumlib/src/config.rs +++ b/mumlib/src/config.rs @@ -23,9 +23,7 @@ pub struct Config { } impl Config { - pub fn write_default_cfg(&self, create: bool) -> Result<(), ConfigError> { - let path = default_cfg_path(); - + pub fn write(&self, path: &Path, create: bool) -> Result<(), ConfigError> { // Possible race here. It's fine since it shows when: // 1) the file doesn't exist when checked and is then created // 2) the file exists when checked but is then removed @@ -40,7 +38,7 @@ impl Config { } Ok(fs::write( - &path, + path, toml::to_string(&TOMLConfig::from(self.clone()))?, )?) } @@ -91,24 +89,6 @@ pub fn default_cfg_path() -> PathBuf { } } -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 { type Error = toml::de::Error; @@ -149,9 +129,8 @@ impl From<Config> for TOMLConfig { } } -pub fn read_default_cfg() -> Result<Config, ConfigError> { - let path = default_cfg_path(); - match fs::read_to_string(&path) { +pub fn read_cfg(path: &Path) -> Result<Config, ConfigError> { + match fs::read_to_string(path) { Ok(s) => { let toml_config: TOMLConfig = toml::from_str(&s)?; Ok(Config::try_from(toml_config)?) |
