From 402174bc195fd59adcee82a9b2d2b3034320406b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Fri, 16 Oct 2020 00:21:05 +0200 Subject: initial clap --- mumctl/Cargo.toml | 2 +- mumctl/src/main.rs | 69 ++++++++++++++++++++++++++++++++++++++++++++---------- 2 files changed, 58 insertions(+), 13 deletions(-) (limited to 'mumctl') diff --git a/mumctl/Cargo.toml b/mumctl/Cargo.toml index 3a432d9..1f2f727 100644 --- a/mumctl/Cargo.toml +++ b/mumctl/Cargo.toml @@ -11,8 +11,8 @@ edition = "2018" [dependencies] mumlib = { path = "../mumlib" } +clap = { version = "2.33", features = ["yaml"] } log = "0.4" ipc-channel = "0.14" -#clap = "2.33" #cursive = "0.15" diff --git a/mumctl/src/main.rs b/mumctl/src/main.rs index 39986f0..e4746d3 100644 --- a/mumctl/src/main.rs +++ b/mumctl/src/main.rs @@ -1,3 +1,4 @@ +use clap::{App, Arg, SubCommand}; use ipc_channel::ipc::{self, IpcReceiver, IpcSender}; use log::*; use mumlib::command::{Command, CommandResponse}; @@ -7,25 +8,69 @@ use std::fs; fn main() { setup_logger(); + let matches = App::new("mumctl") + .subcommand(SubCommand::with_name("server") + .subcommand(SubCommand::with_name("connect") + .arg(Arg::with_name("host") + .required(true) + .index(1)) + .arg(Arg::with_name("username") + .required(true) + .index(2))) + .subcommand(SubCommand::with_name("disconnect"))) + .subcommand(SubCommand::with_name("channel") + .subcommand(SubCommand::with_name("list") + .arg(Arg::with_name("short") + .short("s") + .long("short"))) + .subcommand(SubCommand::with_name("connect") + .arg(Arg::with_name("channel") + .required(true)))) + .subcommand(SubCommand::with_name("status")) + .get_matches(); + + let command = + 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(); + Some(Command::ServerConnect { + host: host.to_string(), + port: 64738u16, //TODO + username: username.to_string(), + accept_invalid_cert: true, //TODO + }) + } else { + None + } + } else if let Some(matches) = matches.subcommand_matches("channel") { + if let Some(matches) = matches.subcommand_matches("list") { + if matches.is_present("short") { + None //TODO + } else { + None //TODO + } + } else if let Some(_matches) = matches.subcommand_matches("connect") { + None //TODO + } else { + None + } + } else if let Some(_matches) = matches.subcommand_matches("status") { + None //TODO + } else { + None + }; + debug!("Creating channel"); let (tx_client, rx_client): (IpcSender, ()>>, IpcReceiver, ()>>) = ipc::channel().unwrap(); let server_name = fs::read_to_string("/var/tmp/mumd-oneshot").unwrap(); //TODO don't panic - debug!("Connecting to mumd at {}", server_name); + info!("Sending {:#?}", command); let tx0 = IpcSender::connect(server_name).unwrap(); - let connect_command = Command::ServerConnect { - host: "10.0.0.10".to_string(), - port: 64738u16, - username: "gustav-mumd".to_string(), - accept_invalid_cert: true, - }; - debug!("Sending {:?} to mumd", connect_command); - tx0.send(( - connect_command, - tx_client)) .unwrap(); + tx0.send((command.unwrap(), tx_client)).unwrap(); debug!("Reading response"); let response = rx_client.recv().unwrap(); - debug!("{:?}", response); + debug!("\n{:#?}", response); } -- cgit v1.2.1 From 109b7eb4ef01b250b2aae6d9218ba6f629f33042 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Fri, 16 Oct 2020 00:30:12 +0200 Subject: clap set arg required else help --- mumctl/src/main.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'mumctl') diff --git a/mumctl/src/main.rs b/mumctl/src/main.rs index e4746d3..f2defc6 100644 --- a/mumctl/src/main.rs +++ b/mumctl/src/main.rs @@ -1,4 +1,4 @@ -use clap::{App, Arg, SubCommand}; +use clap::{App, AppSettings, Arg, SubCommand}; use ipc_channel::ipc::{self, IpcReceiver, IpcSender}; use log::*; use mumlib::command::{Command, CommandResponse}; @@ -9,6 +9,7 @@ fn main() { setup_logger(); let matches = App::new("mumctl") + .setting(AppSettings::ArgRequiredElseHelp) .subcommand(SubCommand::with_name("server") .subcommand(SubCommand::with_name("connect") .arg(Arg::with_name("host") -- cgit v1.2.1 From 91fbdd888ec5b8356f7278150eebd1a8b980a134 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Fri, 16 Oct 2020 00:32:55 +0200 Subject: more set arg required else help --- mumctl/src/main.rs | 3 +++ 1 file changed, 3 insertions(+) (limited to 'mumctl') diff --git a/mumctl/src/main.rs b/mumctl/src/main.rs index f2defc6..c6339f7 100644 --- a/mumctl/src/main.rs +++ b/mumctl/src/main.rs @@ -11,7 +11,9 @@ fn main() { let matches = App::new("mumctl") .setting(AppSettings::ArgRequiredElseHelp) .subcommand(SubCommand::with_name("server") + .setting(AppSettings::ArgRequiredElseHelp) .subcommand(SubCommand::with_name("connect") + .setting(AppSettings::ArgRequiredElseHelp) .arg(Arg::with_name("host") .required(true) .index(1)) @@ -20,6 +22,7 @@ fn main() { .index(2))) .subcommand(SubCommand::with_name("disconnect"))) .subcommand(SubCommand::with_name("channel") + .setting(AppSettings::ArgRequiredElseHelp) .subcommand(SubCommand::with_name("list") .arg(Arg::with_name("short") .short("s") -- cgit v1.2.1 From 857a3614f0aac2510717799708760a4124d412c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Fri, 16 Oct 2020 00:45:45 +0200 Subject: update logging --- mumctl/src/main.rs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) (limited to 'mumctl') diff --git a/mumctl/src/main.rs b/mumctl/src/main.rs index c6339f7..e8ac47f 100644 --- a/mumctl/src/main.rs +++ b/mumctl/src/main.rs @@ -7,6 +7,7 @@ use std::fs; fn main() { setup_logger(); + debug!("Logger up!"); let matches = App::new("mumctl") .setting(AppSettings::ArgRequiredElseHelp) @@ -33,6 +34,7 @@ fn main() { .subcommand(SubCommand::with_name("status")) .get_matches(); + debug!("Matching clap"); let command = if let Some(matches) = matches.subcommand_matches("server") { if let Some(matches) = matches.subcommand_matches("connect") { @@ -64,17 +66,18 @@ fn main() { } else { None }; + debug!("Matched {:#?}", &command); - debug!("Creating channel"); + debug!("Creating CommandResponse-channel"); let (tx_client, rx_client): (IpcSender, ()>>, IpcReceiver, ()>>) = ipc::channel().unwrap(); let server_name = fs::read_to_string("/var/tmp/mumd-oneshot").unwrap(); //TODO don't panic - info!("Sending {:#?}", command); + info!("Sending {:#?}\n to {}", command, server_name); let tx0 = IpcSender::connect(server_name).unwrap(); tx0.send((command.unwrap(), tx_client)).unwrap(); - debug!("Reading response"); + debug!("Waiting for response"); let response = rx_client.recv().unwrap(); - debug!("\n{:#?}", response); + debug!("Received {:#?}", response); } -- cgit v1.2.1 From 1869caa25a295194a0cef26b7df3c20ef69531ef Mon Sep 17 00:00:00 2001 From: Eskil Queseth Date: Fri, 16 Oct 2020 00:54:15 +0200 Subject: implement rest of commands --- mumctl/src/main.rs | 70 ++++++++++++++++++++++++++---------------------------- 1 file changed, 34 insertions(+), 36 deletions(-) (limited to 'mumctl') diff --git a/mumctl/src/main.rs b/mumctl/src/main.rs index e8ac47f..ebd09bf 100644 --- a/mumctl/src/main.rs +++ b/mumctl/src/main.rs @@ -35,49 +35,47 @@ fn main() { .get_matches(); debug!("Matching clap"); - let command = - 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(); - Some(Command::ServerConnect { - host: host.to_string(), - port: 64738u16, //TODO - username: username.to_string(), - accept_invalid_cert: true, //TODO - }) - } else { - None - } - } else if let Some(matches) = matches.subcommand_matches("channel") { - if let Some(matches) = matches.subcommand_matches("list") { - if matches.is_present("short") { - None //TODO - } else { - None //TODO - } - } else if let Some(_matches) = matches.subcommand_matches("connect") { + 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(); + send_command(Command::ServerConnect { + host: host.to_string(), + port: 64738u16, //TODO + username: username.to_string(), + accept_invalid_cert: true, //TODO + }).unwrap(); + } else if let Some(_) = matches.subcommand_matches("disconnect") { + send_command(Command::ServerDisconnect).unwrap(); + } + } else if let Some(matches) = matches.subcommand_matches("channel") { + if let Some(matches) = matches.subcommand_matches("list") { + let res = send_command(Command::ChannelList).unwrap().unwrap(); + println!("{:#?}", res); + /*if matches.is_present("short") { None //TODO } else { - None - } - } else if let Some(_matches) = matches.subcommand_matches("status") { - None //TODO - } else { - None - }; - debug!("Matched {:#?}", &command); + None //TODO + };*/ + } else if let Some(matches) = matches.subcommand_matches("connect") { + send_command(Command::ChannelJoin { + channel_id: matches.value_of("channel").unwrap().parse::().unwrap() + }).unwrap(); + } + } else if let Some(matches) = matches.subcommand_matches("status") { + let res = send_command(Command::Status).unwrap().unwrap(); + println!("{:#?}", res); + }; +} - debug!("Creating CommandResponse-channel"); +fn send_command(command: Command) -> Result, ()> { let (tx_client, rx_client): (IpcSender, ()>>, IpcReceiver, ()>>) = ipc::channel().unwrap(); let server_name = fs::read_to_string("/var/tmp/mumd-oneshot").unwrap(); //TODO don't panic - info!("Sending {:#?}\n to {}", command, server_name); + let tx0 = IpcSender::connect(server_name).unwrap(); - tx0.send((command.unwrap(), tx_client)).unwrap(); + tx0.send((command, tx_client)).unwrap(); - debug!("Waiting for response"); - let response = rx_client.recv().unwrap(); - debug!("Received {:#?}", response); + rx_client.recv().unwrap() } -- cgit v1.2.1 From 5c3dd13cf344c51af145e324daa7103eec0cb8ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Fri, 16 Oct 2020 01:07:35 +0200 Subject: optionally generate shell completions --- mumctl/src/main.rs | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) (limited to 'mumctl') diff --git a/mumctl/src/main.rs b/mumctl/src/main.rs index ebd09bf..41c7306 100644 --- a/mumctl/src/main.rs +++ b/mumctl/src/main.rs @@ -1,15 +1,15 @@ -use clap::{App, AppSettings, Arg, SubCommand}; +use clap::{App, AppSettings, Arg, Shell, SubCommand}; use ipc_channel::ipc::{self, IpcReceiver, IpcSender}; use log::*; use mumlib::command::{Command, CommandResponse}; use mumlib::setup_logger; -use std::fs; +use std::{fs, io}; fn main() { setup_logger(); debug!("Logger up!"); - let matches = App::new("mumctl") + let mut app = App::new("mumctl") .setting(AppSettings::ArgRequiredElseHelp) .subcommand(SubCommand::with_name("server") .setting(AppSettings::ArgRequiredElseHelp) @@ -32,7 +32,9 @@ fn main() { .arg(Arg::with_name("channel") .required(true)))) .subcommand(SubCommand::with_name("status")) - .get_matches(); + .subcommand(SubCommand::with_name("completions")); + + let matches = app.clone().get_matches(); debug!("Matching clap"); if let Some(matches) = matches.subcommand_matches("server") { @@ -49,7 +51,7 @@ fn main() { send_command(Command::ServerDisconnect).unwrap(); } } else if let Some(matches) = matches.subcommand_matches("channel") { - if let Some(matches) = matches.subcommand_matches("list") { + if let Some(_matches) = matches.subcommand_matches("list") { let res = send_command(Command::ChannelList).unwrap().unwrap(); println!("{:#?}", res); /*if matches.is_present("short") { @@ -62,9 +64,24 @@ fn main() { channel_id: matches.value_of("channel").unwrap().parse::().unwrap() }).unwrap(); } - } else if let Some(matches) = matches.subcommand_matches("status") { + } else if let Some(_matches) = matches.subcommand_matches("status") { let res = send_command(Command::Status).unwrap().unwrap(); println!("{:#?}", res); + } else if let Some(matches) = matches.subcommand_matches("completions") { + app.gen_completions_to("mumctl", + match matches.value_of("shell").unwrap_or("zsh") { + "bash" => { + Shell::Bash + }, + "fish" => { + Shell::Fish + }, + _ => { + Shell::Zsh + }, + }, + &mut io::stdout()); + return; }; } -- cgit v1.2.1 From d35c9171271110339504abd96065dc25e1290500 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Fri, 16 Oct 2020 01:12:49 +0200 Subject: add args for other shells --- mumctl/src/main.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'mumctl') diff --git a/mumctl/src/main.rs b/mumctl/src/main.rs index 41c7306..124cc8c 100644 --- a/mumctl/src/main.rs +++ b/mumctl/src/main.rs @@ -32,7 +32,13 @@ fn main() { .arg(Arg::with_name("channel") .required(true)))) .subcommand(SubCommand::with_name("status")) - .subcommand(SubCommand::with_name("completions")); + .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"))); let matches = app.clone().get_matches(); -- cgit v1.2.1