aboutsummaryrefslogtreecommitdiffstats
path: root/mumctl
diff options
context:
space:
mode:
authorEskil Queseth <eskilq@kth.se>2020-10-16 01:43:13 +0200
committerEskil Queseth <eskilq@kth.se>2020-10-16 01:43:13 +0200
commit7e8e67edb7d6ac66fd6073c55053a4e8234710e8 (patch)
tree4c8e04f4b1a031adaf46d39197675d4e500338bd /mumctl
parentd38696eaef440f551fde00e91d73e192d1e8e89b (diff)
parent18a3c0b3cf8254b70857e31ddd2b6213b10db156 (diff)
downloadmum-7e8e67edb7d6ac66fd6073c55053a4e8234710e8.tar.gz
Merge remote-tracking branch 'origin/main' into error-handling
Diffstat (limited to 'mumctl')
-rw-r--r--mumctl/Cargo.toml2
-rw-r--r--mumctl/src/main.rs110
2 files changed, 93 insertions, 19 deletions
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 4aed39c..d6c046d 100644
--- a/mumctl/src/main.rs
+++ b/mumctl/src/main.rs
@@ -1,30 +1,104 @@
-use ipc_channel::ipc::{self, IpcSender};
+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!");
- debug!("Creating channel");
+ let mut app = 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))
+ .arg(Arg::with_name("username")
+ .required(true)
+ .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")
+ .long("short")))
+ .subcommand(SubCommand::with_name("connect")
+ .arg(Arg::with_name("channel")
+ .required(true))))
+ .subcommand(SubCommand::with_name("status"))
+ .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();
+
+ debug!("Matching clap");
+ 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 //TODO
+ };*/
+ } else if let Some(matches) = matches.subcommand_matches("connect") {
+ send_command(Command::ChannelJoin {
+ channel_id: matches.value_of("channel").unwrap().parse::<u32>().unwrap()
+ }).unwrap();
+ }
+ } 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;
+ };
+}
+
+fn send_command(command: Command) -> mumlib::error::Result<Option<CommandResponse>> {
let (tx_client, rx_client) = ipc::channel::<mumlib::error::Result<Option<CommandResponse>>>().unwrap();
let server_name = fs::read_to_string("/var/tmp/mumd-oneshot").unwrap(); //TODO don't panic
- debug!("Connecting to mumd at {}", server_name);
+
let tx0 = IpcSender::connect(server_name).unwrap();
- let connect_command = Command::ServerConnect {
- host: "icahasse.se".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();
-
- debug!("Reading response");
- let response = rx_client.recv().unwrap();
- debug!("{:?}", response);
+
+ tx0.send((command, tx_client)).unwrap();
+
+ rx_client.recv().unwrap()
}