shitbox/corebox/commands/groups/mod.rs

43 lines
1.3 KiB
Rust
Raw Permalink Normal View History

2023-01-12 18:33:41 -05:00
use super::Cmd;
use clap::{Arg, Command};
#[derive(Debug)]
2023-01-12 18:33:41 -05:00
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: &clap::ArgMatches) -> Result<(), Box<dyn std::error::Error>> {
2023-01-12 18:33:41 -05:00
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<shitbox::Path> {
Some(shitbox::Path::UsrBin)
2023-01-12 18:33:41 -05:00
}
}