The `list` command was refactored a little bit as well and the man page and readme were updated.
53 lines
1.3 KiB
Rust
53 lines
1.3 KiB
Rust
use anyhow::Result;
|
|
use clap::Clap;
|
|
use std::{
|
|
fs::File,
|
|
io::{BufRead, BufReader},
|
|
};
|
|
|
|
/// Print information about an existing message queue
|
|
#[derive(Clap, Debug)]
|
|
pub struct Info {
|
|
/// Id of the queue
|
|
#[clap(short, long, required_unless_present_any = &["key"], conflicts_with = "key")]
|
|
id: Option<i32>,
|
|
/// Key of the queue
|
|
#[clap(short, long, required_unless_present_any = &["id"], conflicts_with = "id")]
|
|
key: Option<i32>,
|
|
}
|
|
|
|
fn print_line(line: &str) {
|
|
for field in line.split_whitespace().collect::<Vec<&str>>() {
|
|
print!("{0: <10}", field);
|
|
}
|
|
|
|
println!();
|
|
}
|
|
|
|
impl Info {
|
|
pub fn run(&self) -> Result<()> {
|
|
let mut lines = BufReader::new(File::open("/proc/sysvipc/msg")?).lines();
|
|
|
|
print_line(&lines.nth(0).unwrap_or(Ok(String::new()))?);
|
|
|
|
for line in lines {
|
|
let line = line?;
|
|
|
|
if let Some(id) = self.id {
|
|
if id == line.split_whitespace().collect::<Vec<&str>>()[1].parse::<i32>()? {
|
|
print_line(&line);
|
|
|
|
break;
|
|
}
|
|
} else if let Some(key) = self.key {
|
|
if key == line.split_whitespace().collect::<Vec<&str>>()[0].parse::<i32>()? {
|
|
print_line(&line);
|
|
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
Ok(())
|
|
}
|
|
}
|