diff --git a/src/cli.rs b/src/cli.rs index 5ef375c..8568937 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -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), } diff --git a/src/main.rs b/src/main.rs index ec3e3c6..e361ffe 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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(()) diff --git a/src/send.rs b/src/send.rs new file mode 100644 index 0000000..7b8a6e8 --- /dev/null +++ b/src/send.rs @@ -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(()) + } +}