shitbox/src/cmd/groups/mod.rs

56 lines
1.6 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 name(&self) -> &str {
"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") {
Some(u) => {
pw::get_group_names_for_name(&u)?
},
None => {
pw::get_group_names()?
},
};
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)
}
}