Send a message to a queue

This commit is contained in:
finga 2021-06-14 23:00:06 +02:00
parent 33f0bf8312
commit 36d9e8c02c
3 changed files with 42 additions and 1 deletions

38
src/send.rs Normal file
View file

@ -0,0 +1,38 @@
use anyhow::Result;
use clap::Clap;
/// Send a message to a message queue
#[derive(Clap, Debug)]
pub struct Send {
/// Use priority PRIO, PRIO >= 0
#[clap(short, long, default_value = "0")]
pub priority: u32,
/// Do not block
#[clap(short, long)]
pub non_blocking: bool,
/// Name of the queue
#[clap(value_name = "QNAME")]
pub queue: String,
/// Message to be sent to the queue
#[clap(value_name = "MESSAGE")]
pub msg: String,
}
impl Send {
pub fn run(&self, verbose: bool) -> Result<()> {
let mq = &mut posixmq::OpenOptions::writeonly();
if self.non_blocking {
mq.nonblocking();
}
mq.open(&self.queue)?
.send(self.priority, &self.msg.as_bytes())?;
if verbose {
println!("Sent message: \"{}\" to queue: {}", &self.msg, &self.queue);
}
Ok(())
}
}