diff options
author | Cyborus <cyborus@cyborus.xyz> | 2024-06-18 22:02:33 +0200 |
---|---|---|
committer | Cyborus <cyborus@cyborus.xyz> | 2024-07-08 20:54:57 +0200 |
commit | 2aa4b152ff38c272f9b18e0d650539f4192e42b8 (patch) | |
tree | f2380dd526cb664eebf98bae225efae6065d1a0a /src | |
parent | feat(user): list user repos (diff) | |
download | forgejo-cli-2aa4b152ff38c272f9b18e0d650539f4192e42b8.tar.xz forgejo-cli-2aa4b152ff38c272f9b18e0d650539f4192e42b8.zip |
feat(user): list user orgs
Diffstat (limited to 'src')
-rw-r--r-- | src/user.rs | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/src/user.rs b/src/user.rs index 0c63463..51ed7d7 100644 --- a/src/user.rs +++ b/src/user.rs @@ -56,6 +56,10 @@ pub enum UserSubcommand { #[clap(long)] sort: Option<RepoSortOrder>, }, + Orgs { + /// The name of the user to view org membership of + user: Option<String>, + }, } impl UserCommand { @@ -78,6 +82,7 @@ impl UserCommand { starred, sort, } => list_repos(&api, user.as_deref(), starred, sort).await?, + UserSubcommand::Orgs { user } => list_orgs(&api, user.as_deref()).await?, } Ok(()) } @@ -370,3 +375,51 @@ async fn list_repos( Ok(()) } + +async fn list_orgs(api: &Forgejo, user: Option<&str>) -> eyre::Result<()> { + let mut orgs = match user { + Some(user) => { + let query = forgejo_api::structs::OrgListUserOrgsQuery { + limit: Some(u32::MAX), + ..Default::default() + }; + api.org_list_user_orgs(user, query).await? + } + None => { + let query = forgejo_api::structs::OrgListCurrentUserOrgsQuery { + limit: Some(u32::MAX), + ..Default::default() + }; + api.org_list_current_user_orgs(query).await? + } + }; + + if orgs.is_empty() { + match user { + Some(user) => println!("{user} is not a member of any organizations"), + None => println!("You are not a member of any organizations"), + } + } else { + orgs.sort_unstable_by(|a, b| a.name.cmp(&b.name)); + + let SpecialRender { bullet, dash, .. } = *crate::special_render(); + for org in &orgs { + let name = org.name.as_deref().ok_or_eyre("org does not have name")?; + let full_name = org + .full_name + .as_deref() + .ok_or_eyre("org does not have name")?; + if !full_name.is_empty() { + println!("{bullet} {name} {dash} \"{full_name}\""); + } else { + println!("{bullet} {name}"); + } + } + if orgs.len() == 1 { + println!("1 organization"); + } else { + println!("{} organizations", orgs.len()); + } + } + Ok(()) +} |