diff options
| -rw-r--r-- | mumctl/src/main.rs | 67 | ||||
| -rw-r--r-- | mumlib/src/config.rs | 67 |
2 files changed, 103 insertions, 31 deletions
diff --git a/mumctl/src/main.rs b/mumctl/src/main.rs index 7f74077..49e5662 100644 --- a/mumctl/src/main.rs +++ b/mumctl/src/main.rs @@ -7,6 +7,7 @@ use mumlib::config::ServerConfig; use mumlib::setup_logger; use mumlib::state::Channel; use std::{fs, io, iter}; +use std::io::BufRead; const INDENTATION: &str = " "; @@ -21,9 +22,6 @@ macro_rules! err_print { fn main() { setup_logger(io::stderr(), true); let mut config = config::read_default_cfg(); - if config.is_none() { - println!("{} unable to find config file", "error:".red()); - } let mut app = App::new("mumctl") .setting(AppSettings::ArgRequiredElseHelp) @@ -101,21 +99,13 @@ fn main() { } else if let Some(_) = matches.subcommand_matches("disconnect") { err_print!(send_command(Command::ServerDisconnect)); } else if let Some(matches) = matches.subcommand_matches("config") { - if let Some(config) = &mut config { - match_server_config(matches, config); - } + match_server_config(matches, &mut config); } else if let Some(matches) = matches.subcommand_matches("rename") { - if let Some(config) = &mut config { - match_server_rename(matches, config); - } + match_server_rename(matches, &mut config); } else if let Some(matches) = matches.subcommand_matches("remove") { - if let Some(config) = &mut config { - match_server_remove(matches, config); - } + match_server_remove(matches, &mut config); } else if let Some(matches) = matches.subcommand_matches("add") { - if let Some(config) = &mut config { - match_server_add(matches, config); - } + match_server_add(matches, &mut config); } } else if let Some(matches) = matches.subcommand_matches("channel") { if let Some(_matches) = matches.subcommand_matches("list") { @@ -172,7 +162,19 @@ fn main() { }; if let Some(config) = config { - config.write_default_cfg().unwrap(); + if !config::cfg_exists() { + println!("Config file not found. Create one in {}? [Y/n]", config::get_creatable_cfg_path()); + let stdin = std::io::stdin(); + let response = stdin.lock().lines().next(); + match response.map(|e| e.map(|e| &e == "Y")) { + Some(Ok(true)) => { + config.write_default_cfg(true).unwrap(); + } + _ => {}, + } + } else { + config.write_default_cfg(false).unwrap(); + } } } @@ -206,8 +208,14 @@ fn match_server_connect(matches : &clap::ArgMatches<'_>) { } } -fn match_server_config(matches: &clap::ArgMatches<'_>, config: &mut mumlib::config::Config) { +fn match_server_config(matches: &clap::ArgMatches<'_>, config: &mut Option<mumlib::config::Config>) { let server_name = matches.value_of("server_name").unwrap(); + if config.is_none() { + *config = Some(mumlib::config::Config::default()); + } + + let config = config.as_mut().unwrap(); + if let Some(servers) = &mut config.servers { let server = servers .iter_mut() @@ -264,7 +272,14 @@ fn match_server_config(matches: &clap::ArgMatches<'_>, config: &mut mumlib::conf } } -fn match_server_rename(matches: &clap::ArgMatches<'_>, config: &mut mumlib::config::Config) { +fn match_server_rename(matches: &clap::ArgMatches<'_>, config: &mut Option<mumlib::config::Config>) { + if config.is_none() { + *config = Some(mumlib::config::Config::default()); + } + + let config = config.as_mut().unwrap(); + + if let Some(servers) = &mut config.servers { let prev_name = matches.value_of("prev_name").unwrap(); let next_name = matches.value_of("next_name").unwrap(); @@ -278,7 +293,13 @@ fn match_server_rename(matches: &clap::ArgMatches<'_>, config: &mut mumlib::conf } } -fn match_server_remove(matches: &clap::ArgMatches<'_>, config: &mut mumlib::config::Config) { +fn match_server_remove(matches: &clap::ArgMatches<'_>, config: &mut Option<mumlib::config::Config>) { + if config.is_none() { + *config = Some(mumlib::config::Config::default()); + } + + let config = config.as_mut().unwrap(); + let name = matches.value_of("name").unwrap(); if let Some(servers) = &mut config.servers { match servers.iter().position(|server| server.name == name) { @@ -294,7 +315,13 @@ fn match_server_remove(matches: &clap::ArgMatches<'_>, config: &mut mumlib::conf } } -fn match_server_add(matches: &clap::ArgMatches<'_>, config: &mut mumlib::config::Config) { +fn match_server_add(matches: &clap::ArgMatches<'_>, config: &mut Option<mumlib::config::Config>) { + if config.is_none() { + *config = Some(mumlib::config::Config::default()); + } + + let mut config = config.as_mut().unwrap(); + let name = matches.value_of("name").unwrap().to_string(); let host = matches.value_of("host").unwrap().to_string(); // optional arguments map None to None 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 { |
