Skip to main content

bitbucket_cli/cli/
user.rs

1use anyhow::Result;
2use clap::Subcommand;
3use tabled::{Table, Tabled};
4
5use crate::api::BitbucketClient;
6use crate::models::User;
7
8#[derive(Subcommand)]
9pub enum UserCommands {
10    /// Show the currently authenticated user
11    Whoami,
12    /// View a user's public profile
13    View {
14        /// Account ID or UUID (including braces) of the user
15        account: String,
16    },
17    /// List email addresses on the authenticated account
18    Emails,
19}
20
21#[derive(Tabled)]
22struct EmailRow {
23    #[tabled(rename = "EMAIL")]
24    email: String,
25    #[tabled(rename = "PRIMARY")]
26    primary: String,
27    #[tabled(rename = "CONFIRMED")]
28    confirmed: String,
29}
30
31/// Render a user profile as aligned key/value lines.
32fn print_user(user: &User) {
33    println!("Display name: {}", user.display_name);
34    println!("Username:     {}", user.username.as_deref().unwrap_or("-"));
35    println!(
36        "Account ID:   {}",
37        user.account_id.as_deref().unwrap_or("-")
38    );
39    println!("UUID:         {}", user.uuid);
40}
41
42/// Format an optional boolean as Yes/No, with "-" when absent.
43fn yes_no(value: Option<bool>) -> String {
44    match value {
45        Some(true) => "Yes",
46        Some(false) => "No",
47        None => "-",
48    }
49    .to_string()
50}
51
52impl UserCommands {
53    pub async fn run(self) -> Result<()> {
54        let client = BitbucketClient::from_stored().await?;
55
56        match self {
57            UserCommands::Whoami => {
58                let user = client.get_current_user().await?;
59                if super::output_json() {
60                    return super::print_json(&user);
61                }
62                print_user(&user);
63            }
64            UserCommands::View { account } => {
65                let user = client.get_user(&account).await?;
66                if super::output_json() {
67                    return super::print_json(&user);
68                }
69                print_user(&user);
70            }
71            UserCommands::Emails => {
72                let emails = client.list_user_emails().await?;
73
74                if super::output_json() {
75                    return super::print_json(&emails.values);
76                }
77
78                if emails.values.is_empty() {
79                    println!("No email addresses found");
80                    return Ok(());
81                }
82
83                let rows: Vec<EmailRow> = emails
84                    .values
85                    .iter()
86                    .map(|e| EmailRow {
87                        email: e.email.clone().unwrap_or_else(|| "-".to_string()),
88                        primary: yes_no(e.is_primary),
89                        confirmed: yes_no(e.is_confirmed),
90                    })
91                    .collect();
92
93                let table = Table::new(rows).to_string();
94                println!("{}", table);
95            }
96        }
97
98        Ok(())
99    }
100}