shitbox/src/cmd/groups/mod.rs

48 lines
1.5 KiB
Rust
Raw Normal View History

2023-01-12 18:33:41 -05:00
use super::Cmd;
use crate::pw;
use clap::{Arg, Command};
use std::io;
#[derive(Debug, Default)]
pub struct Groups;
impl Cmd for Groups {
fn cli(&self) -> clap::Command {
Command::new("groups")
.about("display current group names")
.author("Nathan Fisher")
.version(env!("CARGO_PKG_VERSION"))
.after_help(
"The groups command displays the current group names or ID values. \
If the value does not have a corresponding entry in /etc/group, the \
value will be displayed as the numerical group value. The optional \
user parameter will display the groups for the named user.",
)
.arg(Arg::new("user"))
}
fn run(&self, matches: Option<&clap::ArgMatches>) -> Result<(), Box<dyn std::error::Error>> {
let Some(matches) = matches else {
return Err(Box::new(io::Error::new(io::ErrorKind::Other, "no input")))
};
let groups = match matches.get_one::<String>("user") {
2023-01-14 02:34:27 -05:00
Some(u) => pw::get_group_names_for_name(u)?,
2023-01-12 19:00:16 -05:00
None => pw::get_group_names()?,
2023-01-12 18:33:41 -05:00
};
let len = groups.len();
for (idx, group) in groups.into_iter().enumerate() {
if idx < len - 1 {
print!("{group} ");
} else {
println!("{group}");
}
}
Ok(())
}
fn path(&self) -> Option<crate::Path> {
Some(crate::Path::UsrBin)
}
}