Skip to content

Commit

Permalink
feat: list users in project
Browse files Browse the repository at this point in the history
  • Loading branch information
alexng353 committed Mar 10, 2024
1 parent 1926438 commit c125e68
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/commands/list_keys.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub use crate::commands::get::keys::{command, Args};
1 change: 1 addition & 0 deletions src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub mod export;
pub mod gen;
pub mod import;
pub mod link;
pub mod list_keys;
pub mod run;
pub mod set;
pub mod shell;
Expand Down
67 changes: 67 additions & 0 deletions src/commands/project/list_users.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
use super::*;
use crate::types::PartialUser;
use crate::utils::config::get_config;
use crate::{sdk::SDK, utils::choice::Choice};

/// Get all environment variables for a project
#[derive(Parser)]
pub struct Args {
/// Partial fingerprint of key to use
#[clap(short, long)]
key: Option<String>,

/// Project ID
#[clap(short, long)]
project_id: Option<String>,

/// Output in JSON format
#[clap(long)]
json: bool,

/// Show all info
#[clap(short, long)]
all: bool,
}

// TODO: Pretty print project info (in a table?)
pub async fn command(args: Args) -> Result<()> {
let config = get_config()?;
let key = config.get_key_or_default(args.key)?;
let project_id =
Choice::try_project(args.project_id, &key.fingerprint).await?;
let project_info =
SDK::get_project_info(&project_id, &key.fingerprint).await?;

if args.json && args.all {
println!("{}", serde_json::to_string(&project_info.users)?);
return Ok(());
}

if args.json {
println!(
"{}",
serde_json::to_string(
&project_info
.users
.iter()
.map(|u| u.clone().into())
.collect::<Vec<PartialUser>>()
)?
);
return Ok(());
}

if args.all {
for user in project_info.users.iter() {
println!("{} - {} - {} - {}", user.username, user.id, user.created_at, user.public_key);
}
println!("{:?}", &project_info.users);
return Ok(());
}

for user in project_info.users.iter() {
println!("{}", user);
}

Ok(())
}
3 changes: 2 additions & 1 deletion src/commands/project/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub(super) use colored::Colorize;

pub mod add_user;
pub mod remove_user;
pub mod list_users;

use crate::commands_enum;
use clap::Subcommand;
Expand All @@ -20,7 +21,7 @@ pub struct Args {
json: bool,
}

commands_enum!(add_user, remove_user);
commands_enum!(add_user, remove_user, list_users);

pub async fn command(args: Args) -> Result<()> {
Commands::exec(args).await?;
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ commands_enum!(
gen,
import,
link,
list_keys,
run,
set,
shell,
Expand Down
21 changes: 21 additions & 0 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ pub struct User {
pub public_key: String,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct PartialUser {
pub id: String,
pub username: String,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct ProjectInfo {
pub project_id: String,
Expand All @@ -20,3 +26,18 @@ impl Display for User {
write!(f, "{} - {}", self.username, self.id)
}
}

impl From<User> for PartialUser {
fn from(user: User) -> Self {
PartialUser {
id: user.id,
username: user.username,
}
}
}

impl Display for PartialUser {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{} - {}", self.username, self.id)
}
}

0 comments on commit c125e68

Please sign in to comment.