Skip to content

Commit

Permalink
commands: filter get users by project and permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
joe-prosser committed Oct 23, 2023
1 parent 2f96ce7 commit 465908b
Showing 1 changed file with 41 additions and 11 deletions.
52 changes: 41 additions & 11 deletions cli/src/commands/get/users.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use anyhow::{Context, Result};
use reinfer_client::{Client, UserIdentifier};
use anyhow::{bail, Context, Result};
use reinfer_client::{Client, ProjectName, ProjectPermission, UserIdentifier};
use structopt::StructOpt;

use crate::printer::Printer;
Expand All @@ -9,24 +9,54 @@ pub struct GetUsersArgs {
#[structopt(short = "u", long = "user")]
/// Use to retrieve a single user with the provided id
user: Option<UserIdentifier>,

#[structopt(short = "o", long = "project")]
/// Filter users by a given project
project_name_filter: Option<ProjectName>,

#[structopt(short = "p", long = "permission")]
/// Filter users by a given project permission
project_permission_filter: Option<ProjectPermission>,
}

pub fn get(client: &Client, args: &GetUsersArgs, printer: &Printer) -> Result<()> {
let GetUsersArgs { user } = args;
match user {
let GetUsersArgs {
user,
project_name_filter,
project_permission_filter,
} = args;

if project_name_filter.is_none() && project_permission_filter.is_some() {
bail!("You cannot filter on `permission` without a `project`")
}

let mut users = match user {
Some(user_id) => {
let user = client
.get_user(user_id.clone())
.context("Operation to get user has failed.")?;
printer.print_resources(&[user])
}
None => {
let users = client
.get_users()
.context("Operation to list users has failed.")?;
printer.print_resources(&users)
vec![user]
}
None => client
.get_users()
.context("Operation to list users has failed.")?,
};

if let Some(project_name) = project_name_filter {
users.retain(|user| {
user.project_permissions
.get(project_name)
.is_some_and(|user_permissions| {
if let Some(project_permission) = project_permission_filter {
user_permissions.contains(project_permission)
} else {
true
}
})
})
}

printer.print_resources(&users)
}

pub fn get_current_user(client: &Client, printer: &Printer) -> Result<()> {
Expand Down

0 comments on commit 465908b

Please sign in to comment.