From d742c6526b72d34dc6a540f8e0d1b5c91171ae2a Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Fri, 20 Oct 2017 16:36:01 +0200 Subject: [PATCH] feat: Add send command Adds a command that can be used to send simple messages. In the future this may also accept arguments from stdin to send non-text messages. --- src/main.rs | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index 08595d965..634d98f90 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,7 +2,7 @@ extern crate clap; extern crate posix_mq; use clap::{App, SubCommand, Arg, ArgMatches, AppSettings}; -use posix_mq::{Name, Queue}; +use posix_mq::{Name, Queue, Message}; use std::fs::{read_dir, File}; use std::io::{self, Read, Write}; use std::process::exit; @@ -78,6 +78,24 @@ fn run_receive(queue_name: &str) { }; } +fn run_send(queue_name: &str, content: &str) { + let name = Name::new(queue_name).expect("Invalid queue name"); + let queue = Queue::open(name).expect("Could not open queue"); + + let message = Message { + data: content.as_bytes().to_vec(), + priority: 0, + }; + + match queue.send(&message) { + Ok(_) => (), + Err(e) => { + writeln!(io::stderr(), "Could not send message: {}", e).ok(); + exit(1); + } + } +} + fn main() { let ls = SubCommand::with_name("ls").about("list message queues"); @@ -105,6 +123,13 @@ fn main() { .about("Receive a message from a queue") .arg(&queue_arg); + let send = SubCommand::with_name("send") + .about("Send a message to a queue") + .arg(&queue_arg) + .arg(Arg::with_name("message") + .help("the message to send") + .required(true)); + let matches = App::new("mq") .setting(AppSettings::SubcommandRequiredElseHelp) @@ -114,13 +139,18 @@ fn main() { .subcommand(inspect) .subcommand(create) .subcommand(receive) + .subcommand(send) .get_matches(); match matches.subcommand() { ("ls", _) => run_ls(), ("inspect", Some(cmd)) => run_inspect(cmd.value_of("queue").unwrap()), - ("create", Some(cmd)) => run_create(cmd), + ("create", Some(cmd)) => run_create(cmd), ("receive", Some(cmd)) => run_receive(cmd.value_of("queue").unwrap()), + ("send", Some(cmd)) => run_send( + cmd.value_of("queue").unwrap(), + cmd.value_of("message").unwrap() + ), _ => unimplemented!(), } }