aboutsummaryrefslogtreecommitdiffstats
path: root/mumctl
diff options
context:
space:
mode:
Diffstat (limited to 'mumctl')
-rw-r--r--mumctl/Cargo.toml2
-rw-r--r--mumctl/src/main.rs227
2 files changed, 145 insertions, 84 deletions
diff --git a/mumctl/Cargo.toml b/mumctl/Cargo.toml
index c955afa..8483042 100644
--- a/mumctl/Cargo.toml
+++ b/mumctl/Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "mumctl"
-version = "0.1.0"
+version = "0.2.0"
authors = ["Gustav Sörnäs <gustav@sornas.net>",
"Eskil Queseth <eskilq@kth.se>"]
license = "MIT"
diff --git a/mumctl/src/main.rs b/mumctl/src/main.rs
index 7f74077..77340c0 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)
@@ -33,7 +31,7 @@ fn main() {
.subcommand(
SubCommand::with_name("connect")
.arg(Arg::with_name("host").required(true))
- .arg(Arg::with_name("username").required(true))
+ .arg(Arg::with_name("username"))
.arg(Arg::with_name("port")
.long("port")
.short("p")
@@ -42,7 +40,7 @@ fn main() {
SubCommand::with_name("disconnect"))
.subcommand(
SubCommand::with_name("config")
- .arg(Arg::with_name("server_name").required(true))
+ .arg(Arg::with_name("server_name"))
.arg(Arg::with_name("var_name"))
.arg(Arg::with_name("var_value")))
.subcommand(
@@ -97,25 +95,17 @@ fn main() {
if let Some(matches) = matches.subcommand_matches("server") {
if let Some(matches) = matches.subcommand_matches("connect") {
- match_server_connect(matches);
+ match_server_connect(matches, &config);
} 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,26 +162,67 @@ 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();
+ }
}
}
-fn match_server_connect(matches : &clap::ArgMatches<'_>) {
+fn match_server_connect(matches : &clap::ArgMatches<'_>, config: &Option<mumlib::config::Config>) {
let host = matches.value_of("host").unwrap();
- let username = matches.value_of("username").unwrap();
+ let username = matches.value_of("username");
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 {
- match send_command(Command::ServerConnect {
- host: host.to_string(),
- port,
- username: username.to_string(),
- accept_invalid_cert: true, //TODO
- }) {
- Ok(e) => {
+ let response = match config
+ .as_ref()
+ .and_then(|e| e.servers
+ .as_ref()
+ .and_then(|e| e.iter()
+ .find(|e| e.name == host))) {
+ Some(config) => {
+ let host = config.host.as_str();
+ let port = config.port.unwrap_or(port);
+ let username = config.username.as_ref().map(|e| e.as_str()).or(username);
+ if username.is_none() {
+ println!("{} no username specified", "error:".red());
+ return;
+ }
+ send_command(Command::ServerConnect {
+ host: host.to_string(),
+ port,
+ username: username.unwrap().to_string(),
+ accept_invalid_cert: true, //TODO
+ }).map(|e| (e, host))
+ }
+ None => {
+ if username.is_none() {
+ println!("{} no username specified", "error:".red());
+ return
+ }
+ send_command(Command::ServerConnect {
+ host: host.to_string(),
+ port,
+ username: username.unwrap().to_string(),
+ accept_invalid_cert: true, //TODO
+ }).map(|e| (e, host))
+ }
+ };
+ match response {
+ Ok((e, host)) => {
if let Some(CommandResponse::ServerConnect { welcome_message }) = e {
println!("Connected to {}", host);
if let Some(message) = welcome_message {
@@ -206,65 +237,83 @@ fn match_server_connect(matches : &clap::ArgMatches<'_>) {
}
}
-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()) },
- });
+fn match_server_config(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(server_name) = matches.value_of("server_name") {
+ 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 { // 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 { // server is None
- println!("{} server {} not found", "error:".red(), server_name);
+ } else { // servers is None
+ println!("{} no servers found in configuration", "error:".red());
+ }
+ } else {
+ for server in config.servers.iter().flat_map(|e| e.iter()) {
+ println!("{}", 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) {
+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 +327,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 +349,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