about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorVincent Ambo <tazjin@gmail.com>2017-10-20T14·36+0200
committerKarl Erik Asbjørnsen <karl@asbjornsen.org>2017-10-20T15·30+0200
commitd742c6526b72d34dc6a540f8e0d1b5c91171ae2a (patch)
treed7266d03f3799932cf501d7dcfcb3895f8641d50 /src
parenta023e05474536e9397bd4e3bf1ddf32e20366ff9 (diff)
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.
Diffstat (limited to 'src')
-rw-r--r--src/main.rs34
1 files changed, 32 insertions, 2 deletions
diff --git a/src/main.rs b/src/main.rs
index 08595d96555a..634d98f900ad 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!(),
     }
 }