Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
wookietreiber committed Aug 13, 2024
1 parent fb19dba commit f9f100c
Showing 1 changed file with 7 additions and 24 deletions.
31 changes: 7 additions & 24 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use std::path::PathBuf;

use anyhow::{anyhow, Context, Result};
use clap::ArgMatches;
use libc::uid_t;
use libc::{gid_t, uid_t};

pub fn get() -> Result<Config> {
let cli = crate::cli::build();
Expand Down Expand Up @@ -103,21 +103,13 @@ pub enum Filter {
}

impl Filter {
fn group_to_gid(group: &str) -> Result<uid_t> {
fn group_to_gid(group: &str) -> Result<gid_t> {
let is_gid = group.chars().all(char::is_numeric);

if is_gid {
let gid = group.parse::<uid_t>().with_context(|| {
format!("failed to parse {group} as `uid_t`")
})?;

let entry = pwd_grp::getgrgid(gid).with_context(|| {
format!("searching group database for {group}")
})?;

entry
.ok_or_else(|| anyhow!("group {group} not found"))
.map(|_| gid)
group
.parse::<gid_t>()
.with_context(|| format!("failed to parse {group} as `gid_t`"))
} else {
let entry = pwd_grp::getgrnam(group).with_context(|| {
format!("searching group database for {group}")
Expand All @@ -133,17 +125,8 @@ impl Filter {
let is_uid = user.chars().all(char::is_numeric);

if is_uid {
let uid = user.parse::<uid_t>().with_context(|| {
format!("failed to parse {user} as `uid_t`")
})?;

let entry = pwd_grp::getpwuid(uid).with_context(|| {
format!("searching passwd database for {user}")
})?;

entry
.ok_or_else(|| anyhow!("user {user} not found"))
.map(|_| uid)
user.parse::<uid_t>()
.with_context(|| format!("failed to parse {user} as `uid_t`"))
} else {
let entry = pwd_grp::getpwnam(user).with_context(|| {
format!("searching passwd database for {user}")
Expand Down

0 comments on commit f9f100c

Please sign in to comment.