rustfs-cli 0.1.13

A Rust S3 CLI client for S3-compatible object storage
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
//! tree command - Display objects in tree format
//!
//! Shows a tree view of objects in a bucket or prefix.

use clap::Args;
use rc_core::{AliasManager, ListOptions, ObjectInfo, ObjectStore as _, RemotePath};
use rc_s3::S3Client;
use serde::Serialize;
use std::collections::BTreeMap;

use crate::exit_code::ExitCode;
use crate::output::{Formatter, OutputConfig};

/// Display objects in tree format
#[derive(Args, Debug)]
pub struct TreeArgs {
    /// Path to display (alias/bucket[/prefix])
    pub path: String,

    /// Maximum depth to display
    #[arg(short = 'L', long, default_value = "3")]
    pub level: usize,

    /// Show file sizes
    #[arg(short, long)]
    pub size: bool,

    /// Show only directories
    #[arg(short, long)]
    pub dirs_only: bool,

    /// Pattern to include (glob-style)
    #[arg(short = 'P', long)]
    pub pattern: Option<String>,

    /// Show full path prefix
    #[arg(short, long)]
    pub full_path: bool,
}

#[derive(Debug, Serialize)]
struct TreeOutput {
    name: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    children: Option<Vec<TreeOutput>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    size_bytes: Option<i64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    size_human: Option<String>,
    is_dir: bool,
}

struct TreeStats {
    dirs: usize,
    files: usize,
    total_size: i64,
}

/// Execute the tree command
pub async fn execute(args: TreeArgs, output_config: OutputConfig) -> ExitCode {
    let formatter = Formatter::new(output_config);

    // Parse path
    let (alias_name, bucket, prefix) = match parse_tree_path(&args.path) {
        Ok(p) => p,
        Err(e) => {
            formatter.error(&e);
            return ExitCode::UsageError;
        }
    };

    // Load alias
    let alias_manager = match AliasManager::new() {
        Ok(am) => am,
        Err(e) => {
            formatter.error(&format!("Failed to load aliases: {e}"));
            return ExitCode::GeneralError;
        }
    };

    let alias = match alias_manager.get(&alias_name) {
        Ok(a) => a,
        Err(_) => {
            formatter.error(&format!("Alias '{alias_name}' not found"));
            return ExitCode::NotFound;
        }
    };

    // Create S3 client
    let client = match S3Client::new(alias).await {
        Ok(c) => c,
        Err(e) => {
            formatter.error(&format!("Failed to create S3 client: {e}"));
            return ExitCode::NetworkError;
        }
    };

    // Compile pattern if provided
    let pattern = if let Some(ref p) = args.pattern {
        match glob::Pattern::new(p) {
            Ok(pat) => Some(pat),
            Err(e) => {
                formatter.error(&format!("Invalid pattern: {e}"));
                return ExitCode::UsageError;
            }
        }
    } else {
        None
    };

    // List objects
    let remote_path = RemotePath::new(&alias_name, &bucket, prefix.as_deref().unwrap_or(""));
    let objects = match list_all_objects(&client, &remote_path).await {
        Ok(o) => o,
        Err(e) => {
            formatter.error(&format!("Failed to list objects: {e}"));
            return ExitCode::NetworkError;
        }
    };

    // Build tree structure
    let root_name = if args.full_path {
        args.path.clone()
    } else {
        prefix.clone().unwrap_or_else(|| bucket.clone())
    };

    let base_prefix = prefix.as_deref().unwrap_or("");
    let (tree, stats) = build_tree(&objects, base_prefix, &root_name, &args, pattern.as_ref());

    if formatter.is_json() {
        formatter.json(&tree);
    } else {
        // Print tree
        print_tree(&tree, "", true, &formatter, args.size);

        // Print summary
        formatter.println("");
        formatter.println(&format!(
            "{} directories, {} files",
            formatter.style_size(&stats.dirs.to_string()),
            formatter.style_size(&stats.files.to_string())
        ));
        if args.size {
            let total_size_human =
                humansize::format_size(stats.total_size as u64, humansize::BINARY);
            formatter.println(&format!(
                "Total size: {}",
                formatter.style_size(&total_size_human)
            ));
        }
    }

    ExitCode::Success
}

async fn list_all_objects(
    client: &S3Client,
    path: &RemotePath,
) -> Result<Vec<ObjectInfo>, rc_core::Error> {
    let mut all_objects = Vec::new();
    let mut continuation_token: Option<String> = None;

    loop {
        let options = ListOptions {
            recursive: true,
            max_keys: Some(1000),
            continuation_token: continuation_token.clone(),
            ..Default::default()
        };

        let result = client.list_objects(path, options).await?;
        all_objects.extend(result.items);

        if result.truncated {
            continuation_token = result.continuation_token;
        } else {
            break;
        }
    }

    Ok(all_objects)
}

fn build_tree(
    objects: &[ObjectInfo],
    base_prefix: &str,
    root_name: &str,
    args: &TreeArgs,
    pattern: Option<&glob::Pattern>,
) -> (TreeOutput, TreeStats) {
    let mut tree: BTreeMap<String, TreeNode> = BTreeMap::new();
    let mut stats = TreeStats {
        dirs: 0,
        files: 0,
        total_size: 0,
    };

    for obj in objects {
        // Remove base prefix from key
        let relative_key = obj.key.strip_prefix(base_prefix).unwrap_or(&obj.key);
        let relative_key = relative_key.trim_start_matches('/');

        if relative_key.is_empty() {
            continue;
        }

        // Check pattern
        if let Some(pat) = pattern {
            let filename = relative_key.rsplit('/').next().unwrap_or(relative_key);
            if !pat.matches(filename) {
                continue;
            }
        }

        // Skip files if dirs_only
        if args.dirs_only && !obj.is_dir {
            continue;
        }

        // Check depth
        let depth = relative_key.matches('/').count() + 1;
        if depth > args.level {
            continue;
        }

        // Build path components
        let parts: Vec<&str> = relative_key.split('/').collect();
        insert_into_tree(&mut tree, &parts, obj, &mut stats);
    }

    let children = if tree.is_empty() {
        None
    } else {
        Some(tree_to_output(&tree, args.size))
    };

    (
        TreeOutput {
            name: root_name.to_string(),
            children,
            size_bytes: None,
            size_human: None,
            is_dir: true,
        },
        stats,
    )
}

#[derive(Debug)]
struct TreeNode {
    name: String,
    children: BTreeMap<String, TreeNode>,
    size_bytes: Option<i64>,
    size_human: Option<String>,
    is_dir: bool,
}

fn insert_into_tree(
    tree: &mut BTreeMap<String, TreeNode>,
    parts: &[&str],
    obj: &ObjectInfo,
    stats: &mut TreeStats,
) {
    if parts.is_empty() {
        return;
    }

    let name = parts[0].to_string();

    if parts.len() == 1 {
        // Leaf node
        let is_dir = obj.is_dir || name.ends_with('/');
        if is_dir {
            stats.dirs += 1;
        } else {
            stats.files += 1;
            if let Some(size) = obj.size_bytes {
                stats.total_size += size;
            }
        }

        tree.insert(
            name.clone(),
            TreeNode {
                name,
                children: BTreeMap::new(),
                size_bytes: obj.size_bytes,
                size_human: obj.size_human.clone(),
                is_dir,
            },
        );
    } else {
        // Intermediate directory
        let entry = tree.entry(name.clone()).or_insert_with(|| {
            stats.dirs += 1;
            TreeNode {
                name,
                children: BTreeMap::new(),
                size_bytes: None,
                size_human: None,
                is_dir: true,
            }
        });
        insert_into_tree(&mut entry.children, &parts[1..], obj, stats);
    }
}

fn tree_to_output(tree: &BTreeMap<String, TreeNode>, show_size: bool) -> Vec<TreeOutput> {
    tree.values()
        .map(|node| {
            let children = if node.children.is_empty() {
                None
            } else {
                Some(tree_to_output(&node.children, show_size))
            };

            TreeOutput {
                name: node.name.clone(),
                children,
                size_bytes: if show_size { node.size_bytes } else { None },
                size_human: if show_size {
                    node.size_human.clone()
                } else {
                    None
                },
                is_dir: node.is_dir,
            }
        })
        .collect()
}

fn print_tree(
    node: &TreeOutput,
    prefix: &str,
    is_last: bool,
    formatter: &Formatter,
    show_size: bool,
) {
    // Print current node
    let connector = if prefix.is_empty() {
        String::new()
    } else if is_last {
        formatter.style_tree_branch("└── ")
    } else {
        formatter.style_tree_branch("├── ")
    };

    let size_str = if show_size && !node.is_dir {
        node.size_human
            .as_ref()
            .map(|s| format!(" [{}]", formatter.style_size(s)))
            .unwrap_or_default()
    } else {
        String::new()
    };

    let styled_name = if node.is_dir {
        let name = if node.name.ends_with('/') {
            node.name.clone()
        } else {
            format!("{}/", node.name)
        };
        formatter.style_dir(&name)
    } else {
        formatter.style_file(&node.name)
    };

    let styled_prefix = formatter.style_tree_branch(prefix);
    formatter.println(&format!(
        "{styled_prefix}{connector}{styled_name}{size_str}"
    ));

    // Print children
    if let Some(ref children) = node.children {
        let new_prefix = if prefix.is_empty() {
            String::new()
        } else if is_last {
            format!("{prefix}    ")
        } else {
            format!("{prefix}")
        };

        for (i, child) in children.iter().enumerate() {
            let child_is_last = i == children.len() - 1;
            print_tree(child, &new_prefix, child_is_last, formatter, show_size);
        }
    }
}

/// Parse tree path into (alias, bucket, prefix)
fn parse_tree_path(path: &str) -> Result<(String, String, Option<String>), String> {
    if path.is_empty() {
        return Err("Path cannot be empty".to_string());
    }

    let parts: Vec<&str> = path.splitn(3, '/').collect();

    match parts.len() {
        1 => Err("Bucket name is required".to_string()),
        2 => Ok((parts[0].to_string(), parts[1].to_string(), None)),
        3 => Ok((
            parts[0].to_string(),
            parts[1].to_string(),
            Some(parts[2].to_string()),
        )),
        _ => Err(format!("Invalid path format: '{path}'")),
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_parse_tree_path() {
        let (alias, bucket, prefix) = parse_tree_path("myalias/mybucket").unwrap();
        assert_eq!(alias, "myalias");
        assert_eq!(bucket, "mybucket");
        assert!(prefix.is_none());
    }

    #[test]
    fn test_parse_tree_path_with_prefix() {
        let (alias, bucket, prefix) = parse_tree_path("myalias/mybucket/path/to").unwrap();
        assert_eq!(alias, "myalias");
        assert_eq!(bucket, "mybucket");
        assert_eq!(prefix, Some("path/to".to_string()));
    }

    #[test]
    fn test_parse_tree_path_errors() {
        assert!(parse_tree_path("").is_err());
        assert!(parse_tree_path("myalias").is_err());
    }
}