Skip to main content

bitbucket_cli/cli/
workspace.rs

1use anyhow::Result;
2use clap::Subcommand;
3use tabled::{Table, Tabled};
4
5use crate::api::BitbucketClient;
6
7#[derive(Subcommand)]
8pub enum WorkspaceCommands {
9    /// List all workspaces you have access to
10    List,
11}
12
13#[derive(Tabled)]
14struct WorkspaceRow {
15    #[tabled(rename = "SLUG")]
16    slug: String,
17    #[tabled(rename = "NAME")]
18    name: String,
19    #[tabled(rename = "PRIVATE")]
20    private: String,
21    #[tabled(rename = "CREATED")]
22    created: String,
23}
24
25impl WorkspaceCommands {
26    pub async fn run(self) -> Result<()> {
27        match self {
28            WorkspaceCommands::List => {
29                let client = BitbucketClient::from_stored().await?;
30                let workspaces = client.list_workspaces().await?;
31
32                if workspaces.is_empty() {
33                    println!("No workspaces found");
34                    return Ok(());
35                }
36
37                let rows: Vec<WorkspaceRow> = workspaces
38                    .iter()
39                    .map(|w| WorkspaceRow {
40                        slug: w.slug.clone(),
41                        name: w.name.clone(),
42                        private: match w.is_private {
43                            Some(true) => "Yes",
44                            Some(false) => "No",
45                            None => "-",
46                        }
47                        .to_string(),
48                        created: w
49                            .created_on
50                            .map(|d| d.format("%Y-%m-%d").to_string())
51                            .unwrap_or_default(),
52                    })
53                    .collect();
54
55                let table = Table::new(rows).to_string();
56                println!("{}", table);
57
58                Ok(())
59            }
60        }
61    }
62}