mqrs/src/posix/list.rs
finga 4020f062ef Add new subcommand list
Also add it to the readme and manpage.
2021-07-07 18:27:41 +02:00

59 lines
1.7 KiB
Rust

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