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

View file

@ -1,4 +1,4 @@
use crate::{create::Create, unlink::Unlink, info::Info};
use crate::{create::Create, info::Info, send::Send, unlink::Unlink};
use clap::{crate_authors, crate_version, AppSettings, Clap};
#[derive(Clap, Debug)]
@ -22,4 +22,5 @@ pub enum Command {
Create(Create),
Info(Info),
Unlink(Unlink),
Send(Send),
}

View file

@ -4,6 +4,7 @@ use clap::Clap;
mod cli;
mod create;
mod info;
mod send;
mod unlink;
use cli::{Command, Opts};
@ -15,6 +16,7 @@ fn main() -> Result<()> {
Command::Create(c) => c.run(opts.verbose)?,
Command::Info(i) => i.run()?,
Command::Unlink(u) => u.run(opts.verbose)?,
Command::Send(s) => s.run(opts.verbose)?,
}
Ok(())

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(())
}
}