mqrs/src/send.rs
2021-06-20 16:35:20 +02:00

39 lines
900 B
Rust

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 = "QUEUE")]
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(())
}
}