Add new subcommand list

Also add it to the readme and manpage.
This commit is contained in:
finga 2021-07-07 18:27:41 +02:00
parent 12b117db7e
commit 4020f062ef
5 changed files with 90 additions and 6 deletions

View file

@ -46,10 +46,10 @@ from `target/{debug,release}/mqrs` depending on which
one you built. one you built.
## Using `mqrs` ## Using `mqrs`
`mqrs` supports five commands: `create`, `info`, `unlink`, `send` and `mqrs` supports five commands: `create`, `info`, `list`, `unlink`,
`recv`. All commands do not have to be specified fully. If the `send` and `recv`. All commands do not have to be specified fully. If
command is clearly distinguishable from all the others, it does not the command is clearly distinguishable from all the others, it does
have to be completed further. not have to be completed further.
### Create a message queue ### Create a message queue
Use the `create` command to create a new POSIX message queue. Following Use the `create` command to create a new POSIX message queue. Following
@ -62,6 +62,11 @@ optional arguments are supported:
Use the `info` command to print further information about a message Use the `info` command to print further information about a message
queue. queue.
### List all message queues
Use the `list` command to print a list of all message
queues. Following option argument is supported:
- `-a`, `--all`: Print all available information
### Delete a message queue ### Delete a message queue
Use the `unlink` command to delete a message queue. Use the `unlink` command to delete a message queue.

21
mqrs.1
View file

@ -11,6 +11,8 @@ is a small cli program to handle POSIX message queues. Supported commands are
, ,
.B info\ .B info\
, ,
.B list\
,
.B unlink\ .B unlink\
, ,
.B send .B send
@ -18,8 +20,8 @@ and
.B recv .B recv
. .
All commands do not have to be specified fully. If the command is All commands do not have to be specified fully. If the command is
clearly distinguishable from all the others, it does not have to be clearly distinguishable from all the others, it is not needed to
completed further. complete it further.
.SH OPTIONS .SH OPTIONS
.TP 8 .TP 8
.B \-h, \-\-help .B \-h, \-\-help
@ -90,6 +92,21 @@ Prints help information
.B \-v, \-\-verbose .B \-v, \-\-verbose
Produce verbose output Produce verbose output
.RE .RE
.SS list [FLAGS]
Print a list of all existing POSIX message queues.
.TP 8
.SS FLAGS
.RS
.TP 8
.B \-h, \-\-help
Prints help information
.TP 8
.B \-v, \-\-verbose
Produce verbose output
.TP 8
.B \-a, \-\-all
Print all available information
.RE
.SS recv [FLAGS] [OPTIONS] \fI<QUEUE>\fP .SS recv [FLAGS] [OPTIONS] \fI<QUEUE>\fP
Receive and print one or more messages message from a message queue. Receive and print one or more messages message from a message queue.
.TP 8 .TP 8

View file

@ -7,6 +7,7 @@ mod posix;
enum Command { enum Command {
Create(posix::Create), Create(posix::Create),
Info(posix::Info), Info(posix::Info),
List(posix::List),
Unlink(posix::Unlink), Unlink(posix::Unlink),
Send(posix::Send), Send(posix::Send),
Recv(posix::Recv), Recv(posix::Recv),
@ -47,6 +48,7 @@ fn main() -> Result<()> {
Command::Unlink(u) => u.run()?, Command::Unlink(u) => u.run()?,
Command::Send(s) => s.run()?, Command::Send(s) => s.run()?,
Command::Recv(r) => r.run()?, Command::Recv(r) => r.run()?,
Command::List(l) => l.run()?,
} }
Ok(()) Ok(())

View file

@ -1,11 +1,13 @@
mod create; mod create;
mod info; mod info;
mod list;
mod recv; mod recv;
mod send; mod send;
mod unlink; mod unlink;
pub use create::Create; pub use create::Create;
pub use info::Info; pub use info::Info;
pub use list::List;
pub use recv::Recv; pub use recv::Recv;
pub use send::Send; pub use send::Send;
pub use unlink::Unlink; pub use unlink::Unlink;

58
src/posix/list.rs Normal file
View file

@ -0,0 +1,58 @@
use anyhow::{anyhow, Result};
use chrono::{DateTime, Local};
use clap::Clap;
use log::warn;
use std::{fs, os::unix::fs::PermissionsExt};
/// Print information about an existing message queue
#[derive(Clap, Debug)]
pub struct List {
/// Show all parameters
#[clap(short, long)]
all: bool,
}
impl List {
pub fn run(&self) -> Result<()> {
match self.all {
false => println!("Name"),
true => println!(
"{0: <10} {1: <10} {2: <12} {3: <26} {4: <26}",
"Name", "Size", "Permissions", "Modified", "Accessed",
),
}
for mq in fs::read_dir("/dev/mqueue")? {
match mq {
Ok(mq) => {
print!(
"/{0: <10}",
mq.file_name().into_string().map_err(|e| anyhow!(
"Could not convert queue name to string: {:?}",
e
))?
);
if self.all {
let metadata = mq.metadata()?;
let modified: DateTime<Local> = metadata.modified()?.into();
let accessed: DateTime<Local> = metadata.accessed()?.into();
print!(
"{0: <10} {1: <12o} {2: <26} {3: <26}",
metadata.len(),
metadata.permissions().mode(),
modified,
accessed,
);
}
println!();
}
Err(e) => warn!("Could not read file: {:?}", e),
}
}
Ok(())
}
}