rustfs-cli 0.1.12

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
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
490
491
492
493
494
495
496
//! ls command - List buckets and objects
//!
//! Lists buckets when given an alias only, or lists objects when given a bucket path.

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

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

/// List buckets or objects
#[derive(Args, Debug)]
pub struct LsArgs {
    /// Remote path (alias/ or alias/bucket[/prefix])
    pub path: String,

    /// List recursively
    #[arg(short, long)]
    pub recursive: bool,

    /// Show versions (requires versioning support)
    #[arg(long)]
    pub versions: bool,

    /// Include incomplete uploads
    #[arg(long)]
    pub incomplete: bool,

    /// Summarize output (show totals only)
    #[arg(long)]
    pub summarize: bool,
}

/// Output structure for ls command (JSON format)
#[derive(Debug, Serialize)]
struct LsOutput {
    items: Vec<ObjectInfo>,
    truncated: bool,
    #[serde(skip_serializing_if = "Option::is_none")]
    continuation_token: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    summary: Option<Summary>,
}

#[derive(Debug, Serialize)]
struct Summary {
    total_objects: usize,
    total_size_bytes: i64,
    total_size_human: String,
}

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

    // Parse the path
    let (alias_name, bucket, prefix) = match parse_ls_path(&args.path) {
        Ok(parsed) => parsed,
        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;
        }
    };

    if let Some(list_mode) = alias_listing_mode(bucket.as_ref(), args.recursive) {
        return match list_mode {
            AliasListingMode::Buckets => list_buckets(&client, &formatter, args.summarize).await,
            // Keep mc-compatible behavior for `ls alias/ -r`.
            AliasListingMode::AllObjects => {
                list_all_objects(&client, alias_name, &formatter, args.summarize).await
            }
        };
    }

    let bucket = bucket.unwrap();
    let path = RemotePath::new(&alias_name, &bucket, prefix.unwrap_or_default());

    // List objects
    list_objects(&client, &path, &args, &formatter).await
}

async fn list_buckets(client: &S3Client, formatter: &Formatter, summarize: bool) -> ExitCode {
    match client.list_buckets().await {
        Ok(buckets) => {
            if formatter.is_json() {
                let output = LsOutput {
                    items: buckets.clone(),
                    truncated: false,
                    continuation_token: None,
                    summary: if summarize {
                        Some(Summary {
                            total_objects: buckets.len(),
                            total_size_bytes: 0,
                            total_size_human: "0 B".to_string(),
                        })
                    } else {
                        None
                    },
                };
                formatter.json(&output);
            } else {
                for bucket in &buckets {
                    let date = bucket
                        .last_modified
                        .map(|d| d.strftime("%Y-%m-%d %H:%M:%S").to_string())
                        .unwrap_or_else(|| "                   ".to_string());
                    let styled_date = formatter.style_date(&format!("[{date}]"));
                    let styled_size = formatter.style_size(&format!("{:>10}", "0B"));
                    let styled_name = formatter.style_dir(&format!("{}/", bucket.key));
                    formatter.println(&format!("{styled_date} {styled_size} {styled_name}"));
                }
                if summarize {
                    formatter.println(&format!(
                        "\nTotal: {} buckets",
                        formatter.style_size(&buckets.len().to_string())
                    ));
                }
            }
            ExitCode::Success
        }
        Err(e) => {
            formatter.error(&format!("Failed to list buckets: {e}"));
            ExitCode::NetworkError
        }
    }
}

async fn list_objects(
    client: &S3Client,
    path: &RemotePath,
    args: &LsArgs,
    formatter: &Formatter,
) -> ExitCode {
    let options = ListOptions {
        recursive: args.recursive,
        max_keys: Some(1000),
        ..Default::default()
    };

    let (all_items, is_truncated, continuation_token) =
        match list_objects_with_paging(client, path, &options).await {
            Ok(r) => r,
            Err((message, exit_code)) => {
                formatter.error(&message);
                return exit_code;
            }
        };

    // Calculate summary
    let total_objects = all_items.iter().filter(|i| !i.is_dir).count();
    let total_size: i64 = all_items.iter().filter_map(|i| i.size_bytes).sum();

    if formatter.is_json() {
        let output = LsOutput {
            items: all_items,
            truncated: is_truncated,
            continuation_token,
            summary: if args.summarize {
                Some(Summary {
                    total_objects,
                    total_size_bytes: total_size,
                    total_size_human: humansize::format_size(total_size as u64, humansize::BINARY),
                })
            } else {
                None
            },
        };
        formatter.json(&output);
    } else {
        for item in &all_items {
            let date = item
                .last_modified
                .map(|d| d.strftime("%Y-%m-%d %H:%M:%S").to_string())
                .unwrap_or_else(|| "                   ".to_string());
            let styled_date = formatter.style_date(&format!("[{date}]"));

            if item.is_dir {
                let styled_size = formatter.style_size(&format!("{:>10}", "0B"));
                let styled_name = formatter.style_dir(&item.key);
                formatter.println(&format!("{styled_date} {styled_size} {styled_name}"));
            } else {
                let size = item.size_human.clone().unwrap_or_else(|| "0 B".to_string());
                let styled_size = formatter.style_size(&format!("{:>10}", size));
                let styled_name = formatter.style_file(&item.key);
                formatter.println(&format!("{styled_date} {styled_size} {styled_name}"));
            }
        }

        if args.summarize {
            let total_size_human = humansize::format_size(total_size as u64, humansize::BINARY);
            formatter.println(&format!(
                "\nTotal: {} objects, {}",
                formatter.style_size(&total_objects.to_string()),
                formatter.style_size(&total_size_human)
            ));
        }
    }

    ExitCode::Success
}

async fn list_all_objects(
    client: &S3Client,
    alias: String,
    formatter: &Formatter,
    summarize: bool,
) -> ExitCode {
    let buckets = match client.list_buckets().await {
        Ok(buckets) => buckets,
        Err(e) => {
            formatter.error(&format!("Failed to list buckets: {e}"));
            return ExitCode::NetworkError;
        }
    };

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

    let mut all_items: HashMap<&str, Vec<ObjectInfo>> = HashMap::new();
    let mut is_truncated = false;
    let mut continuation_token: Option<String> = None;

    // List all objects in each bucket
    for bucket in &buckets {
        let path = &RemotePath::new(&alias, &bucket.key, "");
        let new_items: Vec<ObjectInfo>;

        (new_items, is_truncated, continuation_token) =
            match list_objects_with_paging(client, path, &options).await {
                Ok(r) => r,
                Err((message, exit_code)) => {
                    formatter.error(&message);
                    return exit_code;
                }
            };

        all_items.entry(&bucket.key).or_default().extend(new_items);
    }

    // Calculate summary
    let total_objects = all_items.values().flatten().filter(|i| !i.is_dir).count();
    let total_size = all_items
        .values()
        .flatten()
        .filter_map(|i| i.size_bytes)
        .sum();

    if formatter.is_json() {
        let output = LsOutput {
            items: all_items
                .into_iter()
                .flat_map(|(bucket, objects)| {
                    objects.into_iter().map(move |mut obj| {
                        obj.key = format!("{}/{}", bucket, obj.key);
                        obj
                    })
                })
                .collect(),
            truncated: is_truncated,
            continuation_token,
            summary: if summarize {
                Some(Summary {
                    total_objects,
                    total_size_bytes: total_size,
                    total_size_human: humansize::format_size(total_size as u64, humansize::BINARY),
                })
            } else {
                None
            },
        };
        formatter.json(&output);
    } else {
        // Ensure deterministic bucket order in text output
        let mut bucket_names: Vec<&str> = all_items.keys().copied().collect();
        bucket_names.sort_unstable();

        for bucket_name in bucket_names {
            if let Some(objects) = all_items.get(bucket_name) {
                for item in objects {
                    let date = item
                        .last_modified
                        .map(|d| d.strftime("%Y-%m-%d %H:%M:%S").to_string())
                        .unwrap_or_else(|| "                   ".to_string());
                    let styled_date = formatter.style_date(&format!("[{date}]"));

                    if item.is_dir {
                        let styled_size = formatter.style_size(&format!("{:>10}", "0B"));
                        let styled_name =
                            formatter.style_dir(&format!("{}/{}", bucket_name, &item.key));
                        formatter.println(&format!("{styled_date} {styled_size} {styled_name}"));
                    } else {
                        let size = item.size_human.clone().unwrap_or_else(|| "0 B".to_string());
                        let styled_size = formatter.style_size(&format!("{:>10}", size));
                        let styled_name =
                            formatter.style_file(&format!("{}/{}", bucket_name, &item.key));
                        formatter.println(&format!("{styled_date} {styled_size} {styled_name}"));
                    }
                }
            }
        }

        if summarize {
            let total_size_human = humansize::format_size(total_size as u64, humansize::BINARY);
            formatter.println(&format!(
                "\nTotal: {} objects, {}",
                formatter.style_size(&total_objects.to_string()),
                formatter.style_size(&total_size_human)
            ));
        }
    }

    ExitCode::Success
}

// List objects using pagination, returns (Vec<ObjectInfo>, is_truncated, Option<continuation_token>)
async fn list_objects_with_paging(
    client: &S3Client,
    path: &RemotePath,
    options: &ListOptions,
) -> Result<(Vec<ObjectInfo>, bool, Option<String>), (String, ExitCode)> {
    let mut all_items = Vec::new();
    let mut is_truncated;
    let mut continuation_token = None;

    // Paginate through all results
    loop {
        let opts = ListOptions {
            continuation_token: continuation_token.clone(),
            ..options.clone()
        };

        match client.list_objects(path, opts).await {
            Ok(result) => {
                all_items.extend(result.items);
                is_truncated = result.truncated;
                continuation_token = result.continuation_token.clone();

                if !result.truncated {
                    break;
                }
            }
            Err(e) => {
                let err_str = e.to_string();
                if err_str.contains("NotFound") || err_str.contains("NoSuchBucket") {
                    return Err((
                        format!("Bucket not found: {}", path.bucket),
                        ExitCode::NotFound,
                    ));
                }
                return Err((
                    format!("Failed to list objects: {e}"),
                    ExitCode::NetworkError,
                ));
            }
        }
    }

    Ok((all_items, is_truncated, continuation_token))
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum AliasListingMode {
    Buckets,
    AllObjects,
}

fn alias_listing_mode(bucket: Option<&String>, recursive: bool) -> Option<AliasListingMode> {
    if bucket.is_some() {
        return None;
    }

    if recursive {
        Some(AliasListingMode::AllObjects)
    } else {
        Some(AliasListingMode::Buckets)
    }
}

/// Parse ls path into (alias, bucket, prefix)
fn parse_ls_path(path: &str) -> Result<(String, Option<String>, Option<String>), String> {
    let path = path.trim_end_matches('/');

    if path.is_empty() {
        return Err("Path cannot be empty".to_string());
    }

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

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

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

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

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

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

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

    #[test]
    fn test_parse_ls_path_empty() {
        assert!(parse_ls_path("").is_err());
    }

    #[test]
    fn test_alias_listing_mode_lists_all_objects_for_recursive_alias_path() {
        assert_eq!(
            alias_listing_mode(None, true),
            Some(AliasListingMode::AllObjects)
        );
    }

    #[test]
    fn test_alias_listing_mode_lists_buckets_without_recursive_flag() {
        assert_eq!(
            alias_listing_mode(None, false),
            Some(AliasListingMode::Buckets)
        );
    }

    #[test]
    fn test_alias_listing_mode_ignores_alias_only_logic_when_bucket_is_present() {
        let bucket = "demo".to_string();
        assert_eq!(alias_listing_mode(Some(&bucket), false), None);
        assert_eq!(alias_listing_mode(Some(&bucket), true), None);
    }
}