Skip to main content

raps_cli/commands/admin/
folder.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2024-2025 Dmytro Yemelianov
3
4//! Folder permission management command implementations
5
6use std::sync::Arc;
7
8use anyhow::Result;
9use colored::Colorize;
10
11use raps_acc::admin::AccountAdminClient;
12use raps_admin::BulkConfig;
13use raps_kernel::auth::AuthClient;
14use raps_kernel::config::Config;
15use raps_kernel::http::HttpClientConfig;
16
17use crate::output::OutputFormat;
18
19use super::operations::display_bulk_result;
20use super::{
21    FolderCommands, create_bulk_progress_bar, get_account_id, make_progress_callback,
22    parse_filter_with_ids,
23};
24
25impl FolderCommands {
26    pub async fn execute(
27        self,
28        config: &Config,
29        auth_client: &AuthClient,
30        output_format: OutputFormat,
31    ) -> Result<()> {
32        match self {
33            FolderCommands::Rights {
34                email,
35                account,
36                level,
37                folder,
38                filter,
39                project_ids,
40                concurrency,
41                dry_run,
42                yes: _,
43            } => {
44                let account_id = get_account_id(account)?;
45                let project_filter = parse_filter_with_ids(&filter, &project_ids)?;
46
47                // Parse folder type
48                let folder_type = match folder.to_lowercase().as_str() {
49                    "project-files" | "projectfiles" => raps_admin::FolderType::ProjectFiles,
50                    "plans" => raps_admin::FolderType::Plans,
51                    _ => raps_admin::FolderType::Custom(folder.clone()),
52                };
53
54                // Create bulk config
55                let bulk_config = BulkConfig {
56                    concurrency: concurrency.min(50),
57                    dry_run,
58                    ..Default::default()
59                };
60
61                if output_format.supports_colors() {
62                    println!(
63                        "\n{} Bulk update folder rights for: {} in account {}",
64                        "\u{2192}".cyan(),
65                        email.green(),
66                        account_id.cyan()
67                    );
68                    println!("  Folder: {}", folder);
69                    println!("  Permission level: {:?}", level);
70                    if let Some(f) = &filter {
71                        println!("  Filter: {}", f);
72                    }
73                    println!("  Concurrency: {}", concurrency.min(50));
74                    if dry_run {
75                        println!("  {} Dry-run mode enabled", "\u{26A0}".yellow());
76                    }
77                    println!();
78                }
79
80                // Create API clients
81                let http_config = HttpClientConfig::default();
82                let admin_client = AccountAdminClient::new_with_http_config(
83                    config.clone(),
84                    auth_client.clone(),
85                    http_config.clone(),
86                );
87                let permissions_client = Arc::new(
88                    raps_acc::permissions::FolderPermissionsClient::new_with_http_config(
89                        config.clone(),
90                        auth_client.clone(),
91                        http_config,
92                    ),
93                );
94
95                let progress_bar = create_bulk_progress_bar(output_format);
96                let on_progress = make_progress_callback(progress_bar.clone());
97
98                // Execute bulk operation
99                let result = raps_admin::bulk_update_folder_rights(
100                    &admin_client,
101                    permissions_client,
102                    &account_id,
103                    &email,
104                    level.into(),
105                    folder_type,
106                    &project_filter,
107                    bulk_config,
108                    on_progress,
109                )
110                .await?;
111
112                // Finish progress bar
113                if let Some(pb) = progress_bar {
114                    pb.finish_and_clear();
115                }
116
117                // Display results
118                display_bulk_result(&result, output_format)?;
119
120                // Exit with appropriate code
121                if result.failed > 0 {
122                    anyhow::bail!(
123                        "Bulk operation partially failed: {} items failed",
124                        result.failed
125                    );
126                }
127
128                Ok(())
129            }
130        }
131    }
132}