From 00f95b860b30923016ebf5d9e78c520b95afb9d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Mon, 19 Oct 2020 03:28:02 +0200 Subject: server config subcommand --- mumctl/src/main.rs | 47 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) (limited to 'mumctl/src/main.rs') diff --git a/mumctl/src/main.rs b/mumctl/src/main.rs index f4b8139..7b60f04 100644 --- a/mumctl/src/main.rs +++ b/mumctl/src/main.rs @@ -1,7 +1,9 @@ use clap::{App, AppSettings, Arg, Shell, SubCommand}; use colored::Colorize; use ipc_channel::ipc::{self, IpcSender}; +use log::*; use mumlib::command::{Command, CommandResponse}; +use mumlib::config; use mumlib::setup_logger; use mumlib::state::Channel; use std::{fs, io, iter}; @@ -18,6 +20,7 @@ macro_rules! err_print { fn main() { setup_logger(io::stderr(), true); + let mut config = config::read_default_cfg(); let mut app = App::new("mumctl") .setting(AppSettings::ArgRequiredElseHelp) @@ -31,7 +34,13 @@ fn main() { .arg(Arg::with_name("username").required(true).index(2)) .arg(Arg::with_name("port").short("p").long("port").takes_value(true)), ) - .subcommand(SubCommand::with_name("disconnect")), + .subcommand(SubCommand::with_name("disconnect")) + .subcommand( + SubCommand::with_name("config") + .setting(AppSettings::ArgRequiredElseHelp) + .arg(Arg::with_name("server_name").required(true).index(1)) + .arg(Arg::with_name("var_name").required(true).index(2)) + .arg(Arg::with_name("var_value").required(true).index(3))), ) .subcommand( SubCommand::with_name("channel") @@ -79,6 +88,40 @@ 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") { + let server_name = matches.value_of("server_name").unwrap(); + let var_name = matches.value_of("var_name").unwrap(); + let var_value = matches.value_of("var_value").unwrap(); + if let Some(ref mut servers) = config.servers { + let server = servers + .iter_mut() + .find( + |s| s.name == server_name); + if server.is_none() { + println!("{} server {} not found", "error:".red(), server_name); + } else { + let server = server.unwrap(); + match var_name { + "host" => { + server.host = var_value.to_string(); + }, + "port" => { + server.port = var_value.parse().unwrap(); + }, + "username" => { + server.username = Some(var_value.to_string()); + }, + "password" => { + server.password = Some(var_value.to_string()); //TODO ask stdin if empty + }, + &_ => { + println!("{} variable {} not found", "error:".red(), var_name); + }, + }; + } + } else { + println!("{} no servers found in configuration", "error:".red()); + } } } else if let Some(matches) = matches.subcommand_matches("channel") { if let Some(_matches) = matches.subcommand_matches("list") { @@ -153,6 +196,8 @@ fn main() { ); return; }; + + config.write_default_cfg(); } fn send_command(command: Command) -> mumlib::error::Result> { -- cgit v1.2.1 From 9d01775d4ce468718c903d9f9e80e696359fdbe8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Mon, 19 Oct 2020 03:35:07 +0200 Subject: server rename command --- mumctl/src/main.rs | 36 +++++++++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 9 deletions(-) (limited to 'mumctl/src/main.rs') diff --git a/mumctl/src/main.rs b/mumctl/src/main.rs index 7b60f04..eaad1d1 100644 --- a/mumctl/src/main.rs +++ b/mumctl/src/main.rs @@ -32,27 +32,27 @@ fn main() { .setting(AppSettings::ArgRequiredElseHelp) .arg(Arg::with_name("host").required(true).index(1)) .arg(Arg::with_name("username").required(true).index(2)) - .arg(Arg::with_name("port").short("p").long("port").takes_value(true)), - ) + .arg(Arg::with_name("port").short("p").long("port").takes_value(true))) .subcommand(SubCommand::with_name("disconnect")) .subcommand( SubCommand::with_name("config") .setting(AppSettings::ArgRequiredElseHelp) .arg(Arg::with_name("server_name").required(true).index(1)) .arg(Arg::with_name("var_name").required(true).index(2)) - .arg(Arg::with_name("var_value").required(true).index(3))), - ) + .arg(Arg::with_name("var_value").required(true).index(3))) + .subcommand( + SubCommand::with_name("rename") + .setting(AppSettings::ArgRequiredElseHelp) + .arg(Arg::with_name("prev_name").required(true).index(1)) + .arg(Arg::with_name("next_name").required(true).index(2)))) .subcommand( SubCommand::with_name("channel") .setting(AppSettings::ArgRequiredElseHelp) .subcommand( SubCommand::with_name("list") - .arg(Arg::with_name("short").short("s").long("short")), - ) + .arg(Arg::with_name("short").short("s").long("short"))) .subcommand( - SubCommand::with_name("connect").arg(Arg::with_name("channel").required(true)), - ), - ) + SubCommand::with_name("connect").arg(Arg::with_name("channel").required(true)))) .subcommand(SubCommand::with_name("status")) .subcommand(SubCommand::with_name("config") .arg(Arg::with_name("name") @@ -102,6 +102,9 @@ fn main() { } else { let server = server.unwrap(); match var_name { + "name" => { + println!("use mumctl server rename instead!"); + }, "host" => { server.host = var_value.to_string(); }, @@ -122,6 +125,21 @@ fn main() { } else { println!("{} no servers found in configuration", "error:".red()); } + } else if let Some(matches) = matches.subcommand_matches("rename") { + if let Some(ref mut servers) = config.servers { + let prev_name = matches.value_of("prev_name").unwrap(); + let next_name = matches.value_of("next_name").unwrap(); + let server = servers + .iter_mut() + .find( + |s| s.name == prev_name + ); + if server.is_none() { + println!("{} server {} not found", "error:".red(), prev_name); + } else { + server.unwrap().name = next_name.to_string(); + } + } } } else if let Some(matches) = matches.subcommand_matches("channel") { if let Some(_matches) = matches.subcommand_matches("list") { -- cgit v1.2.1 From e8e24a6a630bec475232ab862c864ddb59d1526f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Mon, 19 Oct 2020 03:58:05 +0200 Subject: server new --- mumctl/src/main.rs | 46 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) (limited to 'mumctl/src/main.rs') diff --git a/mumctl/src/main.rs b/mumctl/src/main.rs index eaad1d1..2cffe44 100644 --- a/mumctl/src/main.rs +++ b/mumctl/src/main.rs @@ -4,6 +4,7 @@ use ipc_channel::ipc::{self, IpcSender}; use log::*; use mumlib::command::{Command, CommandResponse}; use mumlib::config; +use mumlib::config::ServerConfig; use mumlib::setup_logger; use mumlib::state::Channel; use std::{fs, io, iter}; @@ -44,7 +45,15 @@ fn main() { SubCommand::with_name("rename") .setting(AppSettings::ArgRequiredElseHelp) .arg(Arg::with_name("prev_name").required(true).index(1)) - .arg(Arg::with_name("next_name").required(true).index(2)))) + .arg(Arg::with_name("next_name").required(true).index(2))) + .subcommand( + SubCommand::with_name("add") + .setting(AppSettings::ArgRequiredElseHelp) + .arg(Arg::with_name("name").required(true)) + .arg(Arg::with_name("host").required(true)) + .arg(Arg::with_name("port").long("port").takes_value(true).default_value("64738")) + .arg(Arg::with_name("username").long("username").takes_value(true)) + .arg(Arg::with_name("password").long("password").takes_value(true)))) .subcommand( SubCommand::with_name("channel") .setting(AppSettings::ArgRequiredElseHelp) @@ -140,6 +149,41 @@ fn main() { server.unwrap().name = next_name.to_string(); } } + } else if let Some(matches) = matches.subcommand_matches("add") { + let name = matches.value_of("name").unwrap().to_string(); + let host = matches.value_of("host").unwrap().to_string(); + let port = matches.value_of("port").unwrap().parse().unwrap(); + let username = if let Some(username) = matches.value_of("username") { + Some(username.to_string()) + } else { + None + }; + let password = if let Some(password) = matches.value_of("password") { + Some(password.to_string()) + } else { + None + }; + if let Some(ref mut servers) = config.servers { + if servers.into_iter().any(|s| s.name == name) { + println!("{} a server named {} already exists", "error:".red(), name); + } else { + servers.push(ServerConfig { + name, + host, + port, + username, + password, + }); + } + } else { + config.servers = Some(vec![ServerConfig { + name, + host, + port, + username, + password, + }]); + } } } else if let Some(matches) = matches.subcommand_matches("channel") { if let Some(_matches) = matches.subcommand_matches("list") { -- cgit v1.2.1 From 6d4206da1cb917e33a2ae874b11fc35a001de639 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Mon, 19 Oct 2020 03:59:08 +0200 Subject: skip arg .index() --- mumctl/src/main.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'mumctl/src/main.rs') diff --git a/mumctl/src/main.rs b/mumctl/src/main.rs index 2cffe44..f35c826 100644 --- a/mumctl/src/main.rs +++ b/mumctl/src/main.rs @@ -31,21 +31,21 @@ fn main() { .subcommand( SubCommand::with_name("connect") .setting(AppSettings::ArgRequiredElseHelp) - .arg(Arg::with_name("host").required(true).index(1)) - .arg(Arg::with_name("username").required(true).index(2)) + .arg(Arg::with_name("host").required(true)) + .arg(Arg::with_name("username").required(true)) .arg(Arg::with_name("port").short("p").long("port").takes_value(true))) .subcommand(SubCommand::with_name("disconnect")) .subcommand( SubCommand::with_name("config") .setting(AppSettings::ArgRequiredElseHelp) - .arg(Arg::with_name("server_name").required(true).index(1)) - .arg(Arg::with_name("var_name").required(true).index(2)) - .arg(Arg::with_name("var_value").required(true).index(3))) + .arg(Arg::with_name("server_name").required(true)) + .arg(Arg::with_name("var_name").required(true)) + .arg(Arg::with_name("var_value").required(true))) .subcommand( SubCommand::with_name("rename") .setting(AppSettings::ArgRequiredElseHelp) - .arg(Arg::with_name("prev_name").required(true).index(1)) - .arg(Arg::with_name("next_name").required(true).index(2))) + .arg(Arg::with_name("prev_name").required(true)) + .arg(Arg::with_name("next_name").required(true))) .subcommand( SubCommand::with_name("add") .setting(AppSettings::ArgRequiredElseHelp) -- cgit v1.2.1 From 034efb9431897c098f859286b059298ea1123471 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Mon, 19 Oct 2020 16:34:02 +0200 Subject: clap cleanup --- mumctl/src/main.rs | 56 ++++++++++++++++++++++++++++++++---------------------- 1 file changed, 33 insertions(+), 23 deletions(-) (limited to 'mumctl/src/main.rs') diff --git a/mumctl/src/main.rs b/mumctl/src/main.rs index f35c826..5e6ce84 100644 --- a/mumctl/src/main.rs +++ b/mumctl/src/main.rs @@ -30,51 +30,61 @@ fn main() { .setting(AppSettings::ArgRequiredElseHelp) .subcommand( SubCommand::with_name("connect") - .setting(AppSettings::ArgRequiredElseHelp) .arg(Arg::with_name("host").required(true)) .arg(Arg::with_name("username").required(true)) - .arg(Arg::with_name("port").short("p").long("port").takes_value(true))) - .subcommand(SubCommand::with_name("disconnect")) + .arg(Arg::with_name("port") + .long("port") + .short("p") + .takes_value(true))) + .subcommand( + SubCommand::with_name("disconnect")) .subcommand( SubCommand::with_name("config") - .setting(AppSettings::ArgRequiredElseHelp) .arg(Arg::with_name("server_name").required(true)) .arg(Arg::with_name("var_name").required(true)) .arg(Arg::with_name("var_value").required(true))) .subcommand( SubCommand::with_name("rename") - .setting(AppSettings::ArgRequiredElseHelp) .arg(Arg::with_name("prev_name").required(true)) .arg(Arg::with_name("next_name").required(true))) .subcommand( SubCommand::with_name("add") - .setting(AppSettings::ArgRequiredElseHelp) .arg(Arg::with_name("name").required(true)) .arg(Arg::with_name("host").required(true)) - .arg(Arg::with_name("port").long("port").takes_value(true).default_value("64738")) - .arg(Arg::with_name("username").long("username").takes_value(true)) - .arg(Arg::with_name("password").long("password").takes_value(true)))) + .arg(Arg::with_name("port") + .long("port") + .takes_value(true) + .default_value("64738")) + .arg(Arg::with_name("username") + .long("username") + .takes_value(true)) + .arg(Arg::with_name("password") + .long("password") + .takes_value(true)))) .subcommand( SubCommand::with_name("channel") .setting(AppSettings::ArgRequiredElseHelp) .subcommand( SubCommand::with_name("list") - .arg(Arg::with_name("short").short("s").long("short"))) + .arg(Arg::with_name("short") + .long("short") + .short("s"))) .subcommand( - SubCommand::with_name("connect").arg(Arg::with_name("channel").required(true)))) - .subcommand(SubCommand::with_name("status")) - .subcommand(SubCommand::with_name("config") - .arg(Arg::with_name("name") - .required(true)) - .arg(Arg::with_name("value") - .required(true))) + SubCommand::with_name("connect") + .arg(Arg::with_name("channel").required(true)))) + .subcommand( + SubCommand::with_name("status")) + .subcommand( + SubCommand::with_name("config") + .arg(Arg::with_name("name").required(true)) + .arg(Arg::with_name("value").required(true))) .subcommand(SubCommand::with_name("completions") - .arg(Arg::with_name("zsh") - .long("zsh")) - .arg(Arg::with_name("bash") - .long("bash")) - .arg(Arg::with_name("fish") - .long("fish"))); + .arg(Arg::with_name("zsh") + .long("zsh")) + .arg(Arg::with_name("bash") + .long("bash")) + .arg(Arg::with_name("fish") + .long("fish"))); let matches = app.clone().get_matches(); -- cgit v1.2.1 From d7a35eaf7900659d54419ae356fec1f658caac48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Mon, 19 Oct 2020 17:44:17 +0200 Subject: minor --- mumctl/src/main.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'mumctl/src/main.rs') diff --git a/mumctl/src/main.rs b/mumctl/src/main.rs index 5e6ce84..44c5d26 100644 --- a/mumctl/src/main.rs +++ b/mumctl/src/main.rs @@ -111,7 +111,7 @@ fn main() { let server_name = matches.value_of("server_name").unwrap(); let var_name = matches.value_of("var_name").unwrap(); let var_value = matches.value_of("var_value").unwrap(); - if let Some(ref mut servers) = config.servers { + if let Some(servers) = &mut config.servers { let server = servers .iter_mut() .find( @@ -136,7 +136,7 @@ fn main() { "password" => { server.password = Some(var_value.to_string()); //TODO ask stdin if empty }, - &_ => { + _ => { println!("{} variable {} not found", "error:".red(), var_name); }, }; @@ -145,7 +145,7 @@ fn main() { println!("{} no servers found in configuration", "error:".red()); } } else if let Some(matches) = matches.subcommand_matches("rename") { - if let Some(ref mut servers) = config.servers { + 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(); let server = servers @@ -173,7 +173,7 @@ fn main() { } else { None }; - if let Some(ref mut servers) = config.servers { + if let Some(servers) = &mut config.servers { if servers.into_iter().any(|s| s.name == name) { println!("{} a server named {} already exists", "error:".red(), name); } else { -- cgit v1.2.1 From d75bc24bdd176786a1340e16f7aa3db6e3e6a093 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Mon, 19 Oct 2020 23:34:39 +0200 Subject: send config-reload-command --- mumctl/src/main.rs | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'mumctl/src/main.rs') diff --git a/mumctl/src/main.rs b/mumctl/src/main.rs index 44c5d26..e55796b 100644 --- a/mumctl/src/main.rs +++ b/mumctl/src/main.rs @@ -78,6 +78,8 @@ fn main() { SubCommand::with_name("config") .arg(Arg::with_name("name").required(true)) .arg(Arg::with_name("value").required(true))) + .subcommand( + SubCommand::with_name("config-reload")) .subcommand(SubCommand::with_name("completions") .arg(Arg::with_name("zsh") .long("zsh")) @@ -256,6 +258,8 @@ fn main() { println!("{} Unknown config value {}", "error:".red(), name); } } + } else if matches.subcommand_matches("config-reload").is_some() { + send_command(Command::ConfigReload).unwrap(); } else if let Some(matches) = matches.subcommand_matches("completions") { app.gen_completions_to( "mumctl", -- cgit v1.2.1 From d290a6926b2c495d04396924985ac87e632de1da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Mon, 19 Oct 2020 23:35:21 +0200 Subject: some changes They got a bit bundled together but they at least do the following: - read_default_cfg returns a result - command to print all server config values - command to print a single server config value - command to remove servers - Option::map instead of if let Some()-... - TryFrom instead of Try --- mumctl/src/main.rs | 128 +++++++++++++++++++++++++++++++---------------------- 1 file changed, 76 insertions(+), 52 deletions(-) (limited to 'mumctl/src/main.rs') diff --git a/mumctl/src/main.rs b/mumctl/src/main.rs index e55796b..7c36f02 100644 --- a/mumctl/src/main.rs +++ b/mumctl/src/main.rs @@ -21,7 +21,8 @@ macro_rules! err_print { fn main() { setup_logger(io::stderr(), true); - let mut config = config::read_default_cfg(); + let mut config = config::read_default_cfg() + .expect("format error in config file"); let mut app = App::new("mumctl") .setting(AppSettings::ArgRequiredElseHelp) @@ -41,8 +42,8 @@ fn main() { .subcommand( SubCommand::with_name("config") .arg(Arg::with_name("server_name").required(true)) - .arg(Arg::with_name("var_name").required(true)) - .arg(Arg::with_name("var_value").required(true))) + .arg(Arg::with_name("var_name")) + .arg(Arg::with_name("var_value"))) .subcommand( SubCommand::with_name("rename") .arg(Arg::with_name("prev_name").required(true)) @@ -60,7 +61,10 @@ fn main() { .takes_value(true)) .arg(Arg::with_name("password") .long("password") - .takes_value(true)))) + .takes_value(true))) + .subcommand( + SubCommand::with_name("remove") + .arg(Arg::with_name("name").required(true)))) .subcommand( SubCommand::with_name("channel") .setting(AppSettings::ArgRequiredElseHelp) @@ -111,72 +115,92 @@ fn main() { err_print!(send_command(Command::ServerDisconnect)); } else if let Some(matches) = matches.subcommand_matches("config") { let server_name = matches.value_of("server_name").unwrap(); - let var_name = matches.value_of("var_name").unwrap(); - let var_value = matches.value_of("var_value").unwrap(); if let Some(servers) = &mut config.servers { let server = servers .iter_mut() - .find( - |s| s.name == server_name); - if server.is_none() { + .find(|s| s.name == server_name); + if let Some(server) = server { + if let Some(var_name) = matches.value_of("var_name") { + if let Some(var_value) = matches.value_of("var_value") { + // save var_value in var_name + match var_name { + "name" => { + println!("{} use mumctl server rename instead!", "error:".red()); + }, + "host" => { + server.host = var_value.to_string(); + }, + "port" => { + server.port = Some(var_value.parse().unwrap()); + }, + "username" => { + server.username = Some(var_value.to_string()); + }, + "password" => { + server.password = Some(var_value.to_string()); //TODO ask stdin if empty + }, + _ => { + println!("{} variable {} not found", "error:".red(), var_name); + }, + }; + } else { // var_value is None + // print value of var_name + println!("{}", match var_name { + "name" => { server.name.to_string() }, + "host" => { server.host.to_string() }, + "port" => { server.port.map(|s| s.to_string()).unwrap_or(format!("{} not set", "error:".red())) }, + "username" => { server.username.as_ref().map(|s| s.to_string()).unwrap_or(format!("{} not set", "error:".red())) }, + "password" => { server.password.as_ref().map(|s| s.to_string()).unwrap_or(format!("{} not set", "error:".red())) }, + _ => { format!("{} unknown variable", "error:".red()) }, + }); + } + } else { // var_name is None + // print server config + print!("{}{}{}{}", + format!("host: {}\n", server.host.to_string()), + server.port.map(|s| format!("port: {}\n", s)).unwrap_or("".to_string()), + server.username.as_ref().map(|s| format!("username: {}\n", s)).unwrap_or("".to_string()), + server.password.as_ref().map(|s| format!("password: {}\n", s)).unwrap_or("".to_string()), + ) + } + } else { // server is None println!("{} server {} not found", "error:".red(), server_name); - } else { - let server = server.unwrap(); - match var_name { - "name" => { - println!("use mumctl server rename instead!"); - }, - "host" => { - server.host = var_value.to_string(); - }, - "port" => { - server.port = var_value.parse().unwrap(); - }, - "username" => { - server.username = Some(var_value.to_string()); - }, - "password" => { - server.password = Some(var_value.to_string()); //TODO ask stdin if empty - }, - _ => { - println!("{} variable {} not found", "error:".red(), var_name); - }, - }; } - } else { + } else { // servers is None println!("{} no servers found in configuration", "error:".red()); } } else if let Some(matches) = matches.subcommand_matches("rename") { 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(); - let server = servers - .iter_mut() - .find( - |s| s.name == prev_name - ); - if server.is_none() { - println!("{} server {} not found", "error:".red(), prev_name); + if let Some(server) = servers + .iter_mut() + .find(|s| s.name == prev_name) { + server.name = next_name.to_string(); } else { - server.unwrap().name = next_name.to_string(); + println!("{} server {} not found", "error:".red(), prev_name); + } + } + } else if let Some(matches) = matches.subcommand_matches("remove") { + let name = matches.value_of("name").unwrap(); + if config.servers.is_none() { + println!("{} no servers found in configuration", "error:".red()); + } else { + let prev_amount = config.servers.as_ref().unwrap().len(); + config.servers = config.servers.map(|servers| servers.into_iter().filter(|server| server.name != name).collect()); + if prev_amount == config.servers.as_ref().unwrap().len() { + println!("{} server {} not found", "error:".red(), name); } } } else if let Some(matches) = matches.subcommand_matches("add") { let name = matches.value_of("name").unwrap().to_string(); let host = matches.value_of("host").unwrap().to_string(); - let port = matches.value_of("port").unwrap().parse().unwrap(); - let username = if let Some(username) = matches.value_of("username") { - Some(username.to_string()) - } else { - None - }; - let password = if let Some(password) = matches.value_of("password") { - Some(password.to_string()) - } else { - None - }; + // optional arguments map None to None + let port = matches.value_of("port").map(|s| s.parse().unwrap()); + let username = matches.value_of("username").map(|s| s.to_string()); + let password = matches.value_of("password").map(|s| s.to_string()); if let Some(servers) = &mut config.servers { - if servers.into_iter().any(|s| s.name == name) { + if servers.iter().any(|s| s.name == name) { println!("{} a server named {} already exists", "error:".red(), name); } else { servers.push(ServerConfig { -- cgit v1.2.1 From 2afb9bed4ef5a51b1d2b49728819d187b194829d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Wed, 21 Oct 2020 00:33:30 +0200 Subject: mumctl: refactor matching of config values --- mumctl/src/main.rs | 313 +++++++++++++++++++++++++++++------------------------ 1 file changed, 170 insertions(+), 143 deletions(-) (limited to 'mumctl/src/main.rs') diff --git a/mumctl/src/main.rs b/mumctl/src/main.rs index 7c36f02..0392b2c 100644 --- a/mumctl/src/main.rs +++ b/mumctl/src/main.rs @@ -96,130 +96,17 @@ fn main() { if let Some(matches) = matches.subcommand_matches("server") { if let Some(matches) = matches.subcommand_matches("connect") { - let host = matches.value_of("host").unwrap(); - let username = matches.value_of("username").unwrap(); - let port = match matches.value_of("port").map(|e| e.parse()) { - None => Some(64738), - Some(Err(_)) => None, - Some(Ok(v)) => Some(v), - }; - if let Some(port) = port { - err_print!(send_command(Command::ServerConnect { - host: host.to_string(), - port, - username: username.to_string(), - accept_invalid_cert: true, //TODO - })); - } + match_server_connect(matches); } else if let Some(_) = matches.subcommand_matches("disconnect") { err_print!(send_command(Command::ServerDisconnect)); } else if let Some(matches) = matches.subcommand_matches("config") { - let server_name = matches.value_of("server_name").unwrap(); - if let Some(servers) = &mut config.servers { - let server = servers - .iter_mut() - .find(|s| s.name == server_name); - if let Some(server) = server { - if let Some(var_name) = matches.value_of("var_name") { - if let Some(var_value) = matches.value_of("var_value") { - // save var_value in var_name - match var_name { - "name" => { - println!("{} use mumctl server rename instead!", "error:".red()); - }, - "host" => { - server.host = var_value.to_string(); - }, - "port" => { - server.port = Some(var_value.parse().unwrap()); - }, - "username" => { - server.username = Some(var_value.to_string()); - }, - "password" => { - server.password = Some(var_value.to_string()); //TODO ask stdin if empty - }, - _ => { - println!("{} variable {} not found", "error:".red(), var_name); - }, - }; - } else { // var_value is None - // print value of var_name - println!("{}", match var_name { - "name" => { server.name.to_string() }, - "host" => { server.host.to_string() }, - "port" => { server.port.map(|s| s.to_string()).unwrap_or(format!("{} not set", "error:".red())) }, - "username" => { server.username.as_ref().map(|s| s.to_string()).unwrap_or(format!("{} not set", "error:".red())) }, - "password" => { server.password.as_ref().map(|s| s.to_string()).unwrap_or(format!("{} not set", "error:".red())) }, - _ => { format!("{} unknown variable", "error:".red()) }, - }); - } - } else { // var_name is None - // print server config - print!("{}{}{}{}", - format!("host: {}\n", server.host.to_string()), - server.port.map(|s| format!("port: {}\n", s)).unwrap_or("".to_string()), - server.username.as_ref().map(|s| format!("username: {}\n", s)).unwrap_or("".to_string()), - server.password.as_ref().map(|s| format!("password: {}\n", s)).unwrap_or("".to_string()), - ) - } - } else { // server is None - println!("{} server {} not found", "error:".red(), server_name); - } - } else { // servers is None - println!("{} no servers found in configuration", "error:".red()); - } + match_server_config(matches, &mut config); } else if let Some(matches) = matches.subcommand_matches("rename") { - 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(); - if let Some(server) = servers - .iter_mut() - .find(|s| s.name == prev_name) { - server.name = next_name.to_string(); - } else { - println!("{} server {} not found", "error:".red(), prev_name); - } - } + match_server_rename(matches, &mut config); } else if let Some(matches) = matches.subcommand_matches("remove") { - let name = matches.value_of("name").unwrap(); - if config.servers.is_none() { - println!("{} no servers found in configuration", "error:".red()); - } else { - let prev_amount = config.servers.as_ref().unwrap().len(); - config.servers = config.servers.map(|servers| servers.into_iter().filter(|server| server.name != name).collect()); - if prev_amount == config.servers.as_ref().unwrap().len() { - println!("{} server {} not found", "error:".red(), name); - } - } + match_server_remove(matches, &mut config); } else if let Some(matches) = matches.subcommand_matches("add") { - let name = matches.value_of("name").unwrap().to_string(); - let host = matches.value_of("host").unwrap().to_string(); - // optional arguments map None to None - let port = matches.value_of("port").map(|s| s.parse().unwrap()); - let username = matches.value_of("username").map(|s| s.to_string()); - let password = matches.value_of("password").map(|s| s.to_string()); - if let Some(servers) = &mut config.servers { - if servers.iter().any(|s| s.name == name) { - println!("{} a server named {} already exists", "error:".red(), name); - } else { - servers.push(ServerConfig { - name, - host, - port, - username, - password, - }); - } - } else { - config.servers = Some(vec![ServerConfig { - name, - host, - port, - username, - password, - }]); - } + match_server_add(matches, &mut config); } } else if let Some(matches) = matches.subcommand_matches("channel") { if let Some(_matches) = matches.subcommand_matches("list") { @@ -237,34 +124,12 @@ fn main() { channel_identifier: matches.value_of("channel").unwrap().to_string() })); } - } else if let Some(_matches) = matches.subcommand_matches("status") { + } else if let Some(_) = matches.subcommand_matches("status") { match send_command(Command::Status) { Ok(res) => match res { Some(CommandResponse::Status { server_state }) => { - println!( - "Connected to {} as {}", - server_state.host, server_state.username - ); - let own_channel = server_state - .channels - .iter() - .find(|e| e.users.iter().any(|e| e.name == server_state.username)) - .unwrap(); - println!( - "Currently in {} with {} other client{}:", - own_channel.name, - own_channel.users.len() - 1, - if own_channel.users.len() == 2 { - "" - } else { - "s" - } - ); - println!("{}{}", INDENTATION, own_channel.name); - for user in &own_channel.users { - println!("{}{}{}", INDENTATION, INDENTATION, user); - } - } + parse_status(&server_state); + }, _ => unreachable!(), }, Err(e) => println!("{} {}", "error:".red(), e), @@ -300,6 +165,168 @@ fn main() { config.write_default_cfg(); } +fn match_server_connect(matches : &clap::ArgMatches<>) { + let host = matches.value_of("host").unwrap(); + let username = matches.value_of("username").unwrap(); + let port = match matches.value_of("port").map(|e| e.parse()) { + None => Some(64738), + Some(Err(_)) => None, + Some(Ok(v)) => Some(v), + }; + if let Some(port) = port { + err_print!(send_command(Command::ServerConnect { + host: host.to_string(), + port, + username: username.to_string(), + accept_invalid_cert: true, //TODO + })); + } +} + +fn match_server_config(matches: &clap::ArgMatches<>, config: &mut mumlib::config::Config) { + let server_name = matches.value_of("server_name").unwrap(); + if let Some(servers) = &mut config.servers { + let server = servers + .iter_mut() + .find(|s| s.name == server_name); + if let Some(server) = server { + if let Some(var_name) = matches.value_of("var_name") { + if let Some(var_value) = matches.value_of("var_value") { + // save var_value in var_name (if it is valid) + match var_name { + "name" => { + println!("{} use mumctl server rename instead!", "error:".red()); + }, + "host" => { + server.host = var_value.to_string(); + }, + "port" => { + server.port = Some(var_value.parse().unwrap()); + }, + "username" => { + server.username = Some(var_value.to_string()); + }, + "password" => { + server.password = Some(var_value.to_string()); //TODO ask stdin if empty + }, + _ => { + println!("{} variable {} not found", "error:".red(), var_name); + }, + }; + } else { // var_value is None + // print value of var_name + println!("{}", match var_name { + "name" => { server.name.to_string() }, + "host" => { server.host.to_string() }, + "port" => { server.port.map(|s| s.to_string()).unwrap_or(format!("{} not set", "error:".red())) }, + "username" => { server.username.as_ref().map(|s| s.to_string()).unwrap_or(format!("{} not set", "error:".red())) }, + "password" => { server.password.as_ref().map(|s| s.to_string()).unwrap_or(format!("{} not set", "error:".red())) }, + _ => { format!("{} unknown variable", "error:".red()) }, + }); + } + } else { // var_name is None + // print server config + print!("{}{}{}{}", + format!("host: {}\n", server.host.to_string()), + server.port.map(|s| format!("port: {}\n", s)).unwrap_or("".to_string()), + server.username.as_ref().map(|s| format!("username: {}\n", s)).unwrap_or("".to_string()), + server.password.as_ref().map(|s| format!("password: {}\n", s)).unwrap_or("".to_string()), + ) + } + } else { // server is None + println!("{} server {} not found", "error:".red(), server_name); + } + } else { // servers is None + println!("{} no servers found in configuration", "error:".red()); + } +} + +fn match_server_rename(matches: &clap::ArgMatches<>, config: &mut mumlib::config::Config) { + 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(); + if let Some(server) = servers + .iter_mut() + .find(|s| s.name == prev_name) { + server.name = next_name.to_string(); + } else { + println!("{} server {} not found", "error:".red(), prev_name); + } + } +} + +fn match_server_remove(matches: &clap::ArgMatches<>, config: &mut mumlib::config::Config) { + let name = matches.value_of("name").unwrap(); + if let Some(servers) = &mut config.servers { + match servers.iter().position(|server| server.name == name) { + Some(idx) => { + servers.remove(idx); + }, + None => { + println!("{} server {} not found", "error:".red(), name); + } + }; + } else { + println!("{} no servers found in configuration", "error:".red()); + } +} + +fn match_server_add(matches: &clap::ArgMatches<>, config: &mut mumlib::config::Config) { + let name = matches.value_of("name").unwrap().to_string(); + let host = matches.value_of("host").unwrap().to_string(); + // optional arguments map None to None + let port = matches.value_of("port").map(|s| s.parse().unwrap()); + let username = matches.value_of("username").map(|s| s.to_string()); + let password = matches.value_of("password").map(|s| s.to_string()); + if let Some(servers) = &mut config.servers { + if servers.iter().any(|s| s.name == name) { + println!("{} a server named {} already exists", "error:".red(), name); + } else { + servers.push(ServerConfig { + name, + host, + port, + username, + password, + }); + } + } else { + config.servers = Some(vec![ServerConfig { + name, + host, + port, + username, + password, + }]); + } +} + +fn parse_status(server_state: &mumlib::state::Server) { + println!( + "Connected to {} as {}", + server_state.host, server_state.username + ); + let own_channel = server_state + .channels + .iter() + .find(|e| e.users.iter().any(|e| e.name == server_state.username)) + .unwrap(); + println!( + "Currently in {} with {} other client{}:", + own_channel.name, + own_channel.users.len() - 1, + if own_channel.users.len() == 2 { + "" + } else { + "s" + } + ); + println!("{}{}", INDENTATION, own_channel.name); + for user in &own_channel.users { + println!("{}{}{}", INDENTATION, INDENTATION, user); + } +} + fn send_command(command: Command) -> mumlib::error::Result> { let (tx_client, rx_client) = ipc::channel::>>().unwrap(); -- cgit v1.2.1 From 6a136ac842dd601ce7f68566c27b5262d221872c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Wed, 21 Oct 2020 00:57:37 +0200 Subject: default cfg is option None if the file doesn't exist --- mumctl/src/main.rs | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) (limited to 'mumctl/src/main.rs') diff --git a/mumctl/src/main.rs b/mumctl/src/main.rs index 0392b2c..6e97296 100644 --- a/mumctl/src/main.rs +++ b/mumctl/src/main.rs @@ -1,7 +1,6 @@ use clap::{App, AppSettings, Arg, Shell, SubCommand}; use colored::Colorize; use ipc_channel::ipc::{self, IpcSender}; -use log::*; use mumlib::command::{Command, CommandResponse}; use mumlib::config; use mumlib::config::ServerConfig; @@ -21,8 +20,10 @@ macro_rules! err_print { fn main() { setup_logger(io::stderr(), true); - let mut config = config::read_default_cfg() - .expect("format error in config file"); + 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) @@ -100,13 +101,21 @@ 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") { - match_server_config(matches, &mut config); + if let Some(config) = &mut config { + match_server_config(matches, config); + } } else if let Some(matches) = matches.subcommand_matches("rename") { - match_server_rename(matches, &mut config); + if let Some(config) = &mut config { + match_server_rename(matches, config); + } } else if let Some(matches) = matches.subcommand_matches("remove") { - match_server_remove(matches, &mut config); + if let Some(config) = &mut config { + match_server_remove(matches, config); + } } else if let Some(matches) = matches.subcommand_matches("add") { - match_server_add(matches, &mut config); + if let Some(config) = &mut config { + match_server_add(matches, config); + } } } else if let Some(matches) = matches.subcommand_matches("channel") { if let Some(_matches) = matches.subcommand_matches("list") { @@ -162,7 +171,9 @@ fn main() { return; }; - config.write_default_cfg(); + if let Some(config) = config { + config.write_default_cfg(); + } } fn match_server_connect(matches : &clap::ArgMatches<>) { -- cgit v1.2.1