raps-cli 4.15.0

RAPS (rapeseed) - Rust Autodesk Platform Services CLI
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
// SPDX-License-Identifier: Apache-2.0
// Copyright 2024-2025 Dmytro Yemelianov

//! Folder management commands
//!
//! Commands for listing, creating, and managing folders (requires 3-legged auth).

use anyhow::{Context, Result};
use clap::Subcommand;
use colored::Colorize;
use dialoguer::Input;
#[allow(unused_imports)]
use raps_kernel::prompts;
use serde::Serialize;

use crate::commands::interactive;
use crate::commands::tracked::tracked_op;

use crate::output::OutputFormat;
use raps_acc::permissions::FolderPermissionsClient;
use raps_dm::DataManagementClient;
// use raps_kernel::output::OutputFormat;

#[derive(Debug, Subcommand)]
pub enum FolderCommands {
    /// List folder contents
    List {
        /// Project ID
        project_id: Option<String>,
        /// Folder ID
        folder_id: Option<String>,
        /// Hub ID (for interactive mode)
        #[arg(long, hide = true)]
        hub_id: Option<String>,
    },

    /// Create a new folder
    Create {
        /// Project ID
        project_id: Option<String>,
        /// Parent folder ID
        parent_folder_id: Option<String>,
        /// Folder name (interactive if not provided)
        #[arg(short, long)]
        name: Option<String>,
        /// Hub ID (for interactive mode)
        #[arg(long, hide = true)]
        hub_id: Option<String>,
    },

    /// Rename a folder
    Rename {
        /// Project ID
        project_id: Option<String>,
        /// Folder ID
        folder_id: Option<String>,
        /// New folder name (interactive if not provided)
        #[arg(short, long)]
        name: Option<String>,
        /// Hub ID (for interactive mode)
        #[arg(long, hide = true)]
        hub_id: Option<String>,
    },

    /// Delete a folder
    Delete {
        /// Project ID
        project_id: Option<String>,
        /// Folder ID
        folder_id: Option<String>,
        /// Hub ID (for interactive mode)
        #[arg(long, hide = true)]
        hub_id: Option<String>,
    },

    /// Show permissions (rights) for a folder
    Rights {
        /// Project ID
        project_id: Option<String>,
        /// Folder ID
        folder_id: Option<String>,
        /// Hub ID (for interactive mode)
        #[arg(long, hide = true)]
        hub_id: Option<String>,
    },
}

impl FolderCommands {
    pub async fn execute(
        self,
        client: &DataManagementClient,
        permissions_client: &FolderPermissionsClient,
        output_format: OutputFormat,
    ) -> Result<()> {
        match self {
            FolderCommands::List {
                project_id,
                folder_id,
                hub_id,
            } => {
                let (p_id, f_id) =
                    resolve_folder_args(client, hub_id, project_id, folder_id).await?;
                list_folder_contents(client, &p_id, &f_id, output_format).await
            }
            FolderCommands::Create {
                project_id,
                parent_folder_id,
                name,
                hub_id,
            } => {
                let (p_id, f_id) =
                    resolve_folder_args(client, hub_id, project_id, parent_folder_id).await?;
                create_folder(client, &p_id, &f_id, name, output_format).await
            }
            FolderCommands::Rename {
                project_id,
                folder_id,
                name,
                hub_id,
            } => {
                let (p_id, f_id) =
                    resolve_folder_args(client, hub_id, project_id, folder_id).await?;
                rename_folder(client, &p_id, &f_id, name, output_format).await
            }
            FolderCommands::Delete {
                project_id,
                folder_id,
                hub_id,
            } => {
                let (p_id, f_id) =
                    resolve_folder_args(client, hub_id, project_id, folder_id).await?;
                if !raps_kernel::interactive::should_proceed_destructive("delete this folder") {
                    println!("Operation cancelled.");
                    return Ok(());
                }
                delete_folder(client, &p_id, &f_id, output_format).await
            }
            FolderCommands::Rights {
                project_id,
                folder_id,
                hub_id,
            } => {
                let (p_id, f_id) =
                    resolve_folder_args(client, hub_id, project_id, folder_id).await?;
                folder_rights(permissions_client, &p_id, &f_id, output_format).await
            }
        }
    }
}

async fn resolve_folder_args(
    client: &DataManagementClient,
    opt_hub_id: Option<String>,
    opt_project_id: Option<String>,
    opt_folder_id: Option<String>,
) -> Result<(String, String)> {
    let hub_id = match (&opt_hub_id, &opt_project_id, &opt_folder_id) {
        (Some(h), _, _) => h.clone(),
        (None, Some(_), Some(_)) => String::new(), // Not needed if both P and F are provided
        (None, _, _) => interactive::prompt_for_hub(client).await?,
    };

    let project_id = match opt_project_id {
        Some(p) => p,
        None => interactive::prompt_for_project(client, &hub_id).await?,
    };

    let folder_id = match opt_folder_id {
        Some(f) => f,
        None => interactive::prompt_for_folder(client, &hub_id, &project_id).await?,
    };

    Ok((project_id, folder_id))
}

#[derive(Serialize)]
struct FolderItemOutput {
    id: String,
    name: String,
    item_type: String,
}

async fn list_folder_contents(
    client: &DataManagementClient,
    project_id: &str,
    folder_id: &str,
    output_format: OutputFormat,
) -> Result<()> {
    let contents = tracked_op("Fetching folder contents", output_format, || async {
        client
            .list_folder_contents(project_id, folder_id)
            .await
            .context(format!(
                "Failed to list folder '{}' contents. Verify folder ID and permissions",
                folder_id
            ))
    })
    .await?;

    let items: Vec<FolderItemOutput> = contents
        .iter()
        .map(|item| {
            let item_type = item
                .get("type")
                .and_then(|t| t.as_str())
                .unwrap_or("unknown");
            let id = item.get("id").and_then(|i| i.as_str()).unwrap_or("unknown");
            let name = item
                .get("attributes")
                .and_then(|a| a.get("displayName").or(a.get("name")))
                .and_then(|n| n.as_str())
                .unwrap_or("Unnamed");

            FolderItemOutput {
                id: id.to_string(),
                name: name.to_string(),
                item_type: item_type.to_string(),
            }
        })
        .collect();

    if items.is_empty() {
        match output_format {
            OutputFormat::Table => println!("{}", "Folder is empty.".yellow()),
            _ => {
                output_format.write(&Vec::<FolderItemOutput>::new())?;
            }
        }
        return Ok(());
    }

    match output_format {
        OutputFormat::Table => {
            println!("\n{}", "Folder Contents:".bold());
            println!("{}", "-".repeat(80));

            for item in &items {
                let icon = if item.item_type == "folders" {
                    "[folder]"
                } else {
                    "[file]"
                };
                let type_label = if item.item_type == "folders" {
                    "folder"
                } else {
                    "item"
                };

                println!("  {} {} [{}]", icon, item.name.cyan(), type_label.dimmed());
                println!("    {} {}", "ID:".dimmed(), item.id);
            }

            println!("{}", "-".repeat(80));
        }
        _ => {
            output_format.write(&items)?;
        }
    }
    Ok(())
}

#[derive(Serialize)]
struct CreateFolderOutput {
    success: bool,
    id: String,
    name: String,
}

async fn create_folder(
    client: &DataManagementClient,
    project_id: &str,
    parent_folder_id: &str,
    name: Option<String>,
    output_format: OutputFormat,
) -> Result<()> {
    let folder_name = match name {
        Some(n) => n,
        None => {
            // In non-interactive mode, require the name
            if interactive::is_non_interactive() {
                anyhow::bail!("Folder name is required in non-interactive mode. Use --name flag.");
            }
            Input::new()
                .with_prompt("Enter folder name")
                .interact_text()?
        }
    };

    if output_format.supports_colors() {
        println!("{}", "Creating folder...".dimmed());
    }

    let folder = client
        .create_folder(project_id, parent_folder_id, &folder_name)
        .await
        .context(format!(
            "Failed to create folder '{}' in project '{}'. Check parent folder permissions",
            folder_name, project_id
        ))?;

    let output = CreateFolderOutput {
        success: true,
        id: folder.id.clone(),
        name: folder.attributes.name.clone(),
    };

    match output_format {
        OutputFormat::Table => {
            println!("{} Folder created successfully!", "".green().bold());
            println!("  {} {}", "Name:".bold(), output.name.cyan());
            println!("  {} {}", "ID:".bold(), output.id);
        }
        _ => {
            output_format.write(&output)?;
        }
    }

    Ok(())
}

#[derive(Serialize)]
struct RenameFolderOutput {
    success: bool,
    id: String,
    name: String,
}

async fn rename_folder(
    client: &DataManagementClient,
    project_id: &str,
    folder_id: &str,
    new_name: Option<String>,
    output_format: OutputFormat,
) -> Result<()> {
    let name_str = match new_name {
        Some(n) => n,
        None => {
            if interactive::is_non_interactive() {
                anyhow::bail!("Folder name is required in non-interactive mode. Use --name flag.");
            }
            Input::new()
                .with_prompt("Enter new folder name")
                .interact_text()?
        }
    };

    if output_format.supports_colors() {
        println!("{}", "Renaming folder...".dimmed());
    }

    let folder = client
        .rename_folder(project_id, folder_id, &name_str)
        .await
        .context(format!(
            "Failed to rename folder '{}'. Check permissions and that folder exists",
            folder_id
        ))?;

    let output = RenameFolderOutput {
        success: true,
        id: folder.id.clone(),
        name: folder.attributes.name.clone(),
    };

    match output_format {
        OutputFormat::Table => {
            println!("{} Folder renamed successfully!", "".green().bold());
            println!("  {} {}", "Name:".bold(), output.name.cyan());
            println!("  {} {}", "ID:".bold(), output.id);
        }
        _ => {
            output_format.write(&output)?;
        }
    }

    Ok(())
}

#[derive(Serialize)]
struct DeleteFolderOutput {
    success: bool,
    folder_id: String,
    message: String,
}

async fn delete_folder(
    client: &DataManagementClient,
    project_id: &str,
    folder_id: &str,
    output_format: OutputFormat,
) -> Result<()> {
    if output_format.supports_colors() {
        println!("{}", "Deleting folder...".dimmed());
    }

    client
        .delete_folder(project_id, folder_id)
        .await
        .context(format!(
            "Failed to delete folder '{}'. Folder may not be empty or you lack permissions",
            folder_id
        ))?;

    let output = DeleteFolderOutput {
        success: true,
        folder_id: folder_id.to_string(),
        message: "Folder deleted successfully!".to_string(),
    };

    match output_format {
        OutputFormat::Table => {
            println!("{} {}", "".green().bold(), output.message);
        }
        _ => {
            output_format.write(&output)?;
        }
    }
    Ok(())
}

#[derive(Serialize)]
struct FolderRightOutput {
    subject_id: String,
    subject_type: String,
    actions: Vec<String>,
    inherited_from: Option<String>,
}

async fn folder_rights(
    client: &FolderPermissionsClient,
    project_id: &str,
    folder_id: &str,
    output_format: OutputFormat,
) -> Result<()> {
    let permissions = tracked_op("Fetching folder permissions", output_format, || async {
        client
            .get_permissions(project_id, folder_id)
            .await
            .context(format!(
                "Failed to get permissions for folder '{}'",
                folder_id
            ))
    })
    .await?;

    let items: Vec<FolderRightOutput> = permissions
        .iter()
        .map(|p| FolderRightOutput {
            subject_id: p.subject_id.clone(),
            subject_type: p.subject_type.clone(),
            actions: p.actions.clone(),
            inherited_from: p.inherited_from.clone(),
        })
        .collect();

    if items.is_empty() {
        match output_format {
            OutputFormat::Table => println!("{}", "No permissions found for this folder.".yellow()),
            _ => {
                output_format.write(&Vec::<FolderRightOutput>::new())?;
            }
        }
        return Ok(());
    }

    match output_format {
        OutputFormat::Table => {
            println!("\n{}", "Folder Permissions:".bold());
            println!("{}", "-".repeat(80));

            for item in &items {
                let inherited = item.inherited_from.as_deref().unwrap_or("direct");
                println!(
                    "  {} {} [{}]",
                    item.subject_type.cyan(),
                    item.subject_id,
                    inherited.dimmed()
                );
                println!("    {} {}", "Actions:".dimmed(), item.actions.join(", "));
            }

            println!("{}", "-".repeat(80));
        }
        _ => {
            output_format.write(&items)?;
        }
    }
    Ok(())
}