1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 11:37:44 +00:00

Merge pull request #1522 from sylvestre/groups

groups: clap migration
This commit is contained in:
Sylvestre Ledru 2020-06-08 22:19:08 +02:00 committed by GitHub
commit d0890b72cd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 48 additions and 26 deletions

1
Cargo.lock generated
View file

@ -1334,6 +1334,7 @@ dependencies = [
name = "uu_groups"
version = "0.0.1"
dependencies = [
"clap 2.33.1 (registry+https://github.com/rust-lang/crates.io-index)",
"uucore 0.0.4 (git+https://github.com/uutils/uucore.git?branch=canary)",
"uucore_procs 0.0.4 (git+https://github.com/uutils/uucore.git?branch=canary)",
]

View file

@ -17,6 +17,7 @@ path = "src/groups.rs"
[dependencies]
uucore = { version="0.0.4", package="uucore", git="https://github.com/uutils/uucore.git", branch="canary", features=["entries"] }
uucore_procs = { version="0.0.4", package="uucore_procs", git="https://github.com/uutils/uucore.git", branch="canary" }
clap = "2.32"
[[bin]]
name = "groups"

View file

@ -12,34 +12,54 @@
extern crate uucore;
use uucore::entries::{get_groups, gid2grp, Locate, Passwd};
static SYNTAX: &str = "[user]";
static SUMMARY: &str = "display current group names";
extern crate clap;
use clap::{App, Arg};
static VERSION: &str = env!("CARGO_PKG_VERSION");
static ABOUT: &str = "display current group names";
static OPT_USER: &str = "user";
fn get_usage() -> String {
format!("{0} [USERNAME]", executable!())
}
pub fn uumain(args: Vec<String>) -> i32 {
let matches = app!(SYNTAX, SUMMARY, "").parse(args);
let usage = get_usage();
if matches.free.is_empty() {
println!(
"{}",
get_groups()
.unwrap()
.iter()
.map(|&g| gid2grp(g).unwrap())
.collect::<Vec<_>>()
.join(" ")
);
} else if let Ok(p) = Passwd::locate(matches.free[0].as_str()) {
println!(
"{}",
p.belongs_to()
.iter()
.map(|&g| gid2grp(g).unwrap())
.collect::<Vec<_>>()
.join(" ")
);
} else {
crash!(1, "unknown user {}", matches.free[0]);
let matches = App::new(executable!())
.version(VERSION)
.about(ABOUT)
.usage(&usage[..])
.arg(Arg::with_name(OPT_USER))
.get_matches_from(&args);
match matches.value_of(OPT_USER) {
None => {
println!(
"{}",
get_groups()
.unwrap()
.iter()
.map(|&g| gid2grp(g).unwrap())
.collect::<Vec<_>>()
.join(" ")
);
0
}
Some(user) => {
if let Ok(p) = Passwd::locate(user) {
println!(
"{}",
p.belongs_to()
.iter()
.map(|&g| gid2grp(g).unwrap())
.collect::<Vec<_>>()
.join(" ")
);
0
} else {
crash!(1, "unknown user {}", user);
}
}
}
0
}