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
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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
//! mv command - Move objects
//!
//! Moves objects between locations (copy + delete).

use clap::Args;
use rc_core::{AliasManager, ListOptions, ObjectStore as _, ParsedPath, RemotePath, parse_path};
use rc_s3::S3Client;
use serde::Serialize;
use std::path::{Path, PathBuf};

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

/// Move objects
#[derive(Args, Debug)]
pub struct MvArgs {
    /// Source path (local path or alias/bucket/key)
    pub source: String,

    /// Destination path (local path or alias/bucket/key)
    pub target: String,

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

    /// Continue on errors
    #[arg(long)]
    pub continue_on_error: bool,

    /// Only show what would be moved (dry run)
    #[arg(long)]
    pub dry_run: bool,
}

#[derive(Debug, Serialize)]
struct MvOutput {
    status: &'static str,
    source: String,
    target: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    size_bytes: Option<i64>,
}

/// Execute the mv command
pub async fn execute(args: MvArgs, output_config: OutputConfig) -> ExitCode {
    let formatter = Formatter::new(output_config);
    let alias_manager = AliasManager::new().ok();

    // Parse source and target paths
    let source = match parse_mv_path(&args.source, alias_manager.as_ref()) {
        Ok(p) => p,
        Err(e) => {
            formatter.error(&format!("Invalid source path: {e}"));
            return ExitCode::UsageError;
        }
    };

    let target = match parse_mv_path(&args.target, alias_manager.as_ref()) {
        Ok(p) => p,
        Err(e) => {
            formatter.error(&format!("Invalid target path: {e}"));
            return ExitCode::UsageError;
        }
    };

    // Determine move direction
    match (&source, &target) {
        (ParsedPath::Local(src), ParsedPath::Remote(dst)) => {
            // Local to S3: upload then delete local
            move_local_to_s3(src, dst, &args, &formatter).await
        }
        (ParsedPath::Remote(src), ParsedPath::Local(dst)) => {
            // S3 to Local: download then delete from S3
            move_s3_to_local(src, dst, &args, &formatter).await
        }
        (ParsedPath::Remote(src), ParsedPath::Remote(dst)) => {
            // S3 to S3: copy then delete source
            move_s3_to_s3(src, dst, &args, &formatter).await
        }
        (ParsedPath::Local(_), ParsedPath::Local(_)) => {
            formatter.error("Cannot move between two local paths. Use system mv command.");
            ExitCode::UsageError
        }
    }
}

fn parse_mv_path(path: &str, alias_manager: Option<&AliasManager>) -> rc_core::Result<ParsedPath> {
    let parsed = parse_path(path)?;

    let ParsedPath::Remote(remote) = &parsed else {
        return Ok(parsed);
    };

    if let Some(manager) = alias_manager
        && matches!(manager.exists(&remote.alias), Ok(true))
    {
        return Ok(parsed);
    }

    if Path::new(path).exists() {
        return Ok(ParsedPath::Local(PathBuf::from(path)));
    }

    Ok(parsed)
}

async fn move_local_to_s3(
    src: &std::path::Path,
    dst: &RemotePath,
    args: &MvArgs,
    formatter: &Formatter,
) -> ExitCode {
    use crate::commands::cp;

    // First, copy local to S3
    let cp_args = cp::CpArgs {
        source: src.to_string_lossy().to_string(),
        target: format!("{}/{}/{}", dst.alias, dst.bucket, dst.key),
        recursive: args.recursive,
        preserve: false,
        continue_on_error: args.continue_on_error,
        overwrite: true,
        dry_run: args.dry_run,
        storage_class: None,
        content_type: None,
    };

    let cp_result = cp::execute(
        cp_args,
        OutputConfig {
            json: formatter.is_json(),
            quiet: formatter.is_quiet(),
            ..Default::default()
        },
    )
    .await;

    if cp_result != ExitCode::Success {
        return cp_result;
    }

    // If not dry run, delete local file(s)
    if !args.dry_run {
        if src.is_file()
            && let Err(e) = std::fs::remove_file(src)
        {
            formatter.error(&format!("Failed to delete local file: {e}"));
            return ExitCode::GeneralError;
        } else if src.is_dir()
            && args.recursive
            && let Err(e) = std::fs::remove_dir_all(src)
        {
            formatter.error(&format!("Failed to delete local directory: {e}"));
            return ExitCode::GeneralError;
        }
    }

    ExitCode::Success
}

async fn move_s3_to_local(
    src: &RemotePath,
    dst: &std::path::Path,
    args: &MvArgs,
    formatter: &Formatter,
) -> ExitCode {
    use crate::commands::cp;

    // First, copy S3 to local
    let cp_args = cp::CpArgs {
        source: format!("{}/{}/{}", src.alias, src.bucket, src.key),
        target: dst.to_string_lossy().to_string(),
        recursive: args.recursive,
        preserve: false,
        continue_on_error: args.continue_on_error,
        overwrite: true,
        dry_run: args.dry_run,
        storage_class: None,
        content_type: None,
    };

    let cp_result = cp::execute(
        cp_args,
        OutputConfig {
            json: formatter.is_json(),
            quiet: formatter.is_quiet(),
            ..Default::default()
        },
    )
    .await;

    if cp_result != ExitCode::Success {
        return cp_result;
    }

    // If not dry run, delete S3 object(s)
    if !args.dry_run {
        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(&src.alias) {
            Ok(a) => a,
            Err(_) => {
                formatter.error(&format!("Alias '{}' not found", src.alias));
                return ExitCode::NotFound;
            }
        };

        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 Err(e) = client.delete_object(src).await {
            formatter.error(&format!("Failed to delete source: {e}"));
            return ExitCode::NetworkError;
        }
    }

    ExitCode::Success
}

async fn move_s3_to_s3(
    src: &RemotePath,
    dst: &RemotePath,
    args: &MvArgs,
    formatter: &Formatter,
) -> ExitCode {
    // For S3-to-S3, we need same alias for server-side copy
    if src.alias != dst.alias {
        formatter.error("Cross-alias S3-to-S3 move not yet supported.");
        return ExitCode::UnsupportedFeature;
    }

    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(&src.alias) {
        Ok(a) => a,
        Err(_) => {
            formatter.error(&format!("Alias '{}' not found", src.alias));
            return ExitCode::NotFound;
        }
    };

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

    let src_display = format!("{}/{}/{}", src.alias, src.bucket, src.key);
    let dst_display = format!("{}/{}/{}", dst.alias, dst.bucket, dst.key);

    if args.dry_run {
        formatter.println(&format!("Would move: {src_display} -> {dst_display}"));
        return ExitCode::Success;
    }

    // Recursive move for prefix/directory semantics.
    if args.recursive {
        let mut continuation_token: Option<String> = None;
        let mut moved_count = 0usize;
        let mut error_count = 0usize;
        let src_prefix = src.key.clone();

        loop {
            let list_opts = ListOptions {
                recursive: true,
                continuation_token: continuation_token.clone(),
                ..Default::default()
            };

            let list_result = match client.list_objects(src, list_opts).await {
                Ok(result) => result,
                Err(e) => {
                    formatter.error(&format!("Failed to list source objects: {e}"));
                    return ExitCode::NetworkError;
                }
            };

            for item in &list_result.items {
                if item.is_dir {
                    continue;
                }

                let relative = if src_prefix.is_empty() {
                    item.key.clone()
                } else if let Some(rest) = item.key.strip_prefix(&src_prefix) {
                    rest.trim_start_matches('/').to_string()
                } else {
                    item.key.clone()
                };

                let target_key = if dst.key.is_empty() {
                    relative.clone()
                } else if dst.key.ends_with('/') {
                    format!("{}{}", dst.key, relative)
                } else {
                    format!("{}/{}", dst.key, relative)
                };

                let src_obj = RemotePath::new(&src.alias, &src.bucket, &item.key);
                let dst_obj = RemotePath::new(&dst.alias, &dst.bucket, &target_key);
                let src_obj_display = src_obj.to_string();
                let dst_obj_display = dst_obj.to_string();

                match client.copy_object(&src_obj, &dst_obj).await {
                    Ok(_) => match client.delete_object(&src_obj).await {
                        Ok(()) => {
                            moved_count += 1;
                            if !formatter.is_json() {
                                formatter
                                    .println(&format!("{src_obj_display} -> {dst_obj_display}"));
                            }
                        }
                        Err(e) => {
                            error_count += 1;
                            formatter.error(&format!(
                                "Copied but failed to delete source '{src_obj_display}': {e}"
                            ));
                            if !args.continue_on_error {
                                return ExitCode::GeneralError;
                            }
                        }
                    },
                    Err(e) => {
                        error_count += 1;
                        formatter.error(&format!(
                            "Failed to move '{src_obj_display}' -> '{dst_obj_display}': {e}"
                        ));
                        if !args.continue_on_error {
                            return ExitCode::NetworkError;
                        }
                    }
                }
            }

            if !list_result.truncated {
                break;
            }
            continuation_token = match list_result.continuation_token.clone() {
                Some(token) => Some(token),
                None => {
                    formatter.error(
                        "Backend indicated truncated results but did not provide a continuation token; stopping to avoid an infinite loop.",
                    );
                    return ExitCode::GeneralError;
                }
            };
        }

        if formatter.is_json() {
            #[derive(Serialize)]
            struct MvRecursiveOutput {
                status: &'static str,
                source: String,
                target: String,
                moved: usize,
                errors: usize,
            }

            formatter.json(&MvRecursiveOutput {
                status: if error_count == 0 {
                    "success"
                } else {
                    "partial"
                },
                source: src_display,
                target: dst_display,
                moved: moved_count,
                errors: error_count,
            });
        } else if error_count == 0 {
            formatter.println(&format!("Moved {moved_count} object(s)."));
        } else {
            formatter.println(&format!(
                "Moved {moved_count} object(s), {error_count} failed."
            ));
        }

        if error_count == 0 {
            ExitCode::Success
        } else {
            ExitCode::GeneralError
        }
    } else {
        // Copy
        match client.copy_object(src, dst).await {
            Ok(info) => {
                // Delete source
                if let Err(e) = client.delete_object(src).await {
                    formatter.error(&format!("Copied but failed to delete source: {e}"));
                    return ExitCode::GeneralError;
                }

                if formatter.is_json() {
                    let output = MvOutput {
                        status: "success",
                        source: src_display,
                        target: dst_display,
                        size_bytes: info.size_bytes,
                    };
                    formatter.json(&output);
                } else {
                    formatter.println(&format!(
                        "{src_display} -> {dst_display} ({})",
                        info.size_human.unwrap_or_default()
                    ));
                }
                ExitCode::Success
            }
            Err(e) => {
                let err_str = e.to_string();
                if err_str.contains("NotFound") || err_str.contains("NoSuchKey") {
                    formatter.error(&format!("Source not found: {src_display}"));
                    ExitCode::NotFound
                } else {
                    formatter.error(&format!("Failed to move: {e}"));
                    ExitCode::NetworkError
                }
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use rc_core::{Alias, ConfigManager};
    use tempfile::TempDir;

    fn temp_alias_manager() -> (AliasManager, TempDir) {
        let temp_dir = TempDir::new().expect("create temp dir");
        let config_path = temp_dir.path().join("config.toml");
        let config_manager = ConfigManager::with_path(config_path);
        let alias_manager = AliasManager::with_config_manager(config_manager);
        (alias_manager, temp_dir)
    }

    #[test]
    fn test_parse_paths() {
        // Local path
        let local = parse_path("./file.txt").unwrap();
        assert!(matches!(local, ParsedPath::Local(_)));

        // Remote path
        let remote = parse_path("myalias/bucket/file.txt").unwrap();
        assert!(matches!(remote, ParsedPath::Remote(_)));
    }

    #[test]
    fn test_parse_local_absolute_path() {
        // Use platform-appropriate absolute path
        #[cfg(unix)]
        let path = "/tmp/file.txt";
        #[cfg(windows)]
        let path = "C:\\temp\\file.txt";

        let result = parse_path(path).unwrap();
        assert!(matches!(result, ParsedPath::Local(_)));
        if let ParsedPath::Local(p) = result {
            assert!(p.is_absolute());
        }
    }

    #[test]
    fn test_parse_remote_path_components() {
        let result = parse_path("s3/mybucket/path/to/file.txt").unwrap();
        if let ParsedPath::Remote(r) = result {
            assert_eq!(r.alias, "s3");
            assert_eq!(r.bucket, "mybucket");
            assert_eq!(r.key, "path/to/file.txt");
        } else {
            panic!("Expected Remote path");
        }
    }

    #[test]
    fn test_parse_mv_path_prefers_existing_local_path_when_alias_missing() {
        let (alias_manager, temp_dir) = temp_alias_manager();
        let full = temp_dir.path().join("issue-2094-mv-local").join("file.txt");
        let full_str = full.to_string_lossy().to_string();

        if let Some(parent) = full.parent() {
            std::fs::create_dir_all(parent).expect("create parent dirs");
        }
        std::fs::write(&full, b"test").expect("write local file");

        let parsed = parse_mv_path(&full_str, Some(&alias_manager)).expect("parse path");
        assert!(matches!(parsed, ParsedPath::Local(_)));
    }

    #[test]
    fn test_parse_mv_path_keeps_remote_when_alias_exists() {
        let (alias_manager, _temp_dir) = temp_alias_manager();
        alias_manager
            .set(Alias::new("target", "http://localhost:9000", "a", "b"))
            .expect("set alias");

        let parsed = parse_mv_path("target/bucket/file.txt", Some(&alias_manager))
            .expect("parse remote path");
        assert!(matches!(parsed, ParsedPath::Remote(_)));
    }

    #[test]
    fn test_parse_mv_path_keeps_remote_when_local_missing() {
        let (alias_manager, _temp_dir) = temp_alias_manager();
        let parsed = parse_mv_path("missing/bucket/file.txt", Some(&alias_manager))
            .expect("parse remote path");
        assert!(matches!(parsed, ParsedPath::Remote(_)));
    }

    #[test]
    fn test_mv_args_defaults() {
        let args = MvArgs {
            source: "src".to_string(),
            target: "dst".to_string(),
            recursive: false,
            continue_on_error: false,
            dry_run: false,
        };
        assert!(!args.recursive);
        assert!(!args.dry_run);
        assert!(!args.continue_on_error);
    }

    #[test]
    fn test_mv_output_serialization() {
        let output = MvOutput {
            status: "success",
            source: "src/file.txt".to_string(),
            target: "dst/file.txt".to_string(),
            size_bytes: Some(2048),
        };
        let json = serde_json::to_string(&output).unwrap();
        assert!(json.contains("\"status\":\"success\""));
        assert!(json.contains("\"size_bytes\":2048"));
    }

    #[test]
    fn test_mv_output_skips_none_size() {
        let output = MvOutput {
            status: "success",
            source: "src".to_string(),
            target: "dst".to_string(),
            size_bytes: None,
        };
        let json = serde_json::to_string(&output).unwrap();
        assert!(!json.contains("size_bytes"));
    }
}