chaser 0.1.0

An automated file path synchronization tool that updates changed paths in configuration files in real time.
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
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
use crate::i18n::t;
use clap::{Arg, ArgAction, Command};

pub fn build_cli() -> Command {
    Command::new("chaser")
        .about(&t("app_description"))
        .version(env!("CARGO_PKG_VERSION"))
        .subcommand_required(false)
        .arg_required_else_help(false)
        .subcommand(
            Command::new("add").about(&t("cmd_add")).arg(
                Arg::new("path")
                    .help(&t("arg_path"))
                    .required(true)
                    .index(1),
            ),
        )
        .subcommand(
            Command::new("remove").about(&t("cmd_remove")).arg(
                Arg::new("path")
                    .help(&t("arg_path_remove"))
                    .required(true)
                    .index(1),
            ),
        )
        .subcommand(Command::new("list").about(&t("cmd_list")))
        .subcommand(Command::new("config").about(&t("cmd_config")))
        .subcommand(
            Command::new("recursive").about(&t("cmd_recursive")).arg(
                Arg::new("enabled")
                    .help(&t("arg_recursive_enabled"))
                    .required(true)
                    .action(ArgAction::Set)
                    .index(1),
            ),
        )
        .subcommand(
            Command::new("ignore").about(&t("cmd_ignore")).arg(
                Arg::new("pattern")
                    .help(&t("arg_ignore_pattern"))
                    .required(true)
                    .index(1),
            ),
        )
        .subcommand(Command::new("reset").about(&t("cmd_reset")))
        .subcommand(
            Command::new("lang").about(&t("cmd_lang")).arg(
                Arg::new("language")
                    .help(&t("arg_language"))
                    .required(true)
                    .action(ArgAction::Set)
                    .index(1),
            ),
        )
        .subcommand(
            Command::new("add-target").about(&t("cmd_add_target")).arg(
                Arg::new("file")
                    .help(&t("arg_target_file"))
                    .required(true)
                    .index(1),
            ),
        )
        .subcommand(
            Command::new("remove-target")
                .about(&t("cmd_remove_target"))
                .arg(
                    Arg::new("file")
                        .help(&t("arg_target_file_remove"))
                        .required(true)
                        .index(1),
                ),
        )
        .subcommand(Command::new("list-targets").about(&t("cmd_list_targets")))
        .subcommand(Command::new("status").about(&t("cmd_status")))
        .subcommand(
            Command::new("sync").about(&t("cmd_sync")).arg(
                Arg::new("once")
                    .long("once")
                    .help(&t("arg_sync_once"))
                    .action(ArgAction::SetTrue),
            ),
        )
        .subcommand(
            Command::new("update-path")
                .about(&t("cmd_update_path"))
                .arg(
                    Arg::new("old_path")
                        .help(&t("arg_old_path"))
                        .required(true)
                        .index(1),
                )
                .arg(
                    Arg::new("new_path")
                        .help(&t("arg_new_path"))
                        .required(true)
                        .index(2),
                ),
        )
}

// 简化版CLI构建器,用于测试,不依赖国际化
pub fn build_test_cli() -> Command {
    Command::new("chaser")
        .about("An automated file path synchronization tool")
        .version(env!("CARGO_PKG_VERSION"))
        .subcommand_required(false)
        .arg_required_else_help(false)
        .subcommand(
            Command::new("add").about("Add a path to watch").arg(
                Arg::new("path")
                    .help("Path to add to watch list")
                    .required(true)
                    .index(1),
            ),
        )
        .subcommand(
            Command::new("remove")
                .about("Remove a path from watch list")
                .arg(
                    Arg::new("path")
                        .help("Path to remove from watch list")
                        .required(true)
                        .index(1),
                ),
        )
        .subcommand(Command::new("list").about("List all watched paths and settings"))
        .subcommand(Command::new("config").about("Show config file location"))
        .subcommand(
            Command::new("recursive")
                .about("Set recursive watching (true/false)")
                .arg(
                    Arg::new("enabled")
                        .help("Enable or disable recursive watching")
                        .required(true)
                        .action(ArgAction::Set)
                        .index(1),
                ),
        )
        .subcommand(
            Command::new("ignore").about("Add ignore pattern").arg(
                Arg::new("pattern")
                    .help("Pattern to ignore (e.g., \"*.tmp\", \".git/**\")")
                    .required(true)
                    .index(1),
            ),
        )
        .subcommand(Command::new("reset").about("Reset config to default"))
        .subcommand(
            Command::new("lang").about("Set interface language").arg(
                Arg::new("language")
                    .help("Language code (en, zh-cn)")
                    .required(true)
                    .action(ArgAction::Set)
                    .index(1),
            ),
        )
        .subcommand(
            Command::new("add-target")
                .about("Add a target file for path synchronization")
                .arg(
                    Arg::new("file")
                        .help("Target file path (json, yaml, toml, csv)")
                        .required(true)
                        .index(1),
                ),
        )
        .subcommand(
            Command::new("remove-target")
                .about("Remove a target file")
                .arg(
                    Arg::new("file")
                        .help("Target file path to remove")
                        .required(true)
                        .index(1),
                ),
        )
        .subcommand(Command::new("list-targets").about("List all target files"))
        .subcommand(Command::new("status").about("Show path synchronization status"))
        .subcommand(
            Command::new("sync")
                .about("Start path synchronization monitoring")
                .arg(
                    Arg::new("once")
                        .long("once")
                        .help("Perform one-time sync without monitoring")
                        .action(ArgAction::SetTrue),
                ),
        )
        .subcommand(
            Command::new("update-path")
                .about("Manually update a path in target files")
                .arg(
                    Arg::new("old_path")
                        .help("Old path to replace")
                        .required(true)
                        .index(1),
                )
                .arg(
                    Arg::new("new_path")
                        .help("New path to replace with")
                        .required(true)
                        .index(2),
                ),
        )
}

#[derive(Debug)]
pub enum Commands {
    Add { path: String },
    Remove { path: String },
    List,
    Config,
    Recursive { enabled: String },
    Ignore { pattern: String },
    Reset,
    Lang { language: String },
    AddTarget { file: String },
    RemoveTarget { file: String },
    ListTargets,
    Status,
    Sync { once: bool },
    UpdatePath { old_path: String, new_path: String },
}

pub fn parse_command(matches: &clap::ArgMatches) -> Option<Commands> {
    match matches.subcommand() {
        Some(("add", sub_matches)) => {
            let path = sub_matches.get_one::<String>("path").unwrap().clone();
            Some(Commands::Add { path })
        }
        Some(("remove", sub_matches)) => {
            let path = sub_matches.get_one::<String>("path").unwrap().clone();
            Some(Commands::Remove { path })
        }
        Some(("list", _)) => Some(Commands::List),
        Some(("config", _)) => Some(Commands::Config),
        Some(("recursive", sub_matches)) => {
            let enabled = sub_matches.get_one::<String>("enabled").unwrap().clone();
            Some(Commands::Recursive { enabled })
        }
        Some(("ignore", sub_matches)) => {
            let pattern = sub_matches.get_one::<String>("pattern").unwrap().clone();
            Some(Commands::Ignore { pattern })
        }
        Some(("reset", _)) => Some(Commands::Reset),
        Some(("lang", sub_matches)) => {
            let language = sub_matches.get_one::<String>("language").unwrap().clone();
            Some(Commands::Lang { language })
        }
        Some(("add-target", sub_matches)) => {
            let file = sub_matches.get_one::<String>("file").unwrap().clone();
            Some(Commands::AddTarget { file })
        }
        Some(("remove-target", sub_matches)) => {
            let file = sub_matches.get_one::<String>("file").unwrap().clone();
            Some(Commands::RemoveTarget { file })
        }
        Some(("list-targets", _)) => Some(Commands::ListTargets),
        Some(("status", _)) => Some(Commands::Status),
        Some(("sync", sub_matches)) => {
            let once = sub_matches.get_flag("once");
            Some(Commands::Sync { once })
        }
        Some(("update-path", sub_matches)) => {
            let old_path = sub_matches.get_one::<String>("old_path").unwrap().clone();
            let new_path = sub_matches.get_one::<String>("new_path").unwrap().clone();
            Some(Commands::UpdatePath { old_path, new_path })
        }
        _ => None,
    }
}

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

    // 测试辅助函数:使用简化版CLI构建器
    fn setup_test_cli() -> Command {
        build_test_cli()
    }

    #[test]
    fn test_cli_no_command() {
        let cli = setup_test_cli();
        let matches = cli.try_get_matches_from(&["chaser"]).unwrap();
        assert!(parse_command(&matches).is_none());
    }

    #[test]
    fn test_add_command() {
        let cli = setup_test_cli();
        let matches = cli
            .try_get_matches_from(&["chaser", "add", "/path/to/watch"])
            .unwrap();
        match parse_command(&matches) {
            Some(Commands::Add { path }) => {
                assert_eq!(path, "/path/to/watch");
            }
            _ => panic!("Expected Add command"),
        }
    }

    #[test]
    fn test_remove_command() {
        let cli = setup_test_cli();
        let matches = cli
            .try_get_matches_from(&["chaser", "remove", "/path/to/remove"])
            .unwrap();
        match parse_command(&matches) {
            Some(Commands::Remove { path }) => {
                assert_eq!(path, "/path/to/remove");
            }
            _ => panic!("Expected Remove command"),
        }
    }

    #[test]
    fn test_list_command() {
        let cli = setup_test_cli();
        let matches = cli.try_get_matches_from(&["chaser", "list"]).unwrap();
        match parse_command(&matches) {
            Some(Commands::List) => {}
            _ => panic!("Expected List command"),
        }
    }

    #[test]
    fn test_config_command() {
        let cli = setup_test_cli();
        let matches = cli.try_get_matches_from(&["chaser", "config"]).unwrap();
        match parse_command(&matches) {
            Some(Commands::Config) => {}
            _ => panic!("Expected Config command"),
        }
    }

    #[test]
    fn test_recursive_command() {
        let cli = setup_test_cli();
        let matches = cli
            .try_get_matches_from(&["chaser", "recursive", "true"])
            .unwrap();
        match parse_command(&matches) {
            Some(Commands::Recursive { enabled }) => {
                assert_eq!(enabled, "true");
            }
            _ => panic!("Expected Recursive command"),
        }

        let cli = setup_test_cli();
        let matches = cli
            .try_get_matches_from(&["chaser", "recursive", "false"])
            .unwrap();
        match parse_command(&matches) {
            Some(Commands::Recursive { enabled }) => {
                assert_eq!(enabled, "false");
            }
            _ => panic!("Expected Recursive command"),
        }
    }

    #[test]
    fn test_ignore_command() {
        let cli = setup_test_cli();
        let matches = cli
            .try_get_matches_from(&["chaser", "ignore", "*.tmp"])
            .unwrap();
        match parse_command(&matches) {
            Some(Commands::Ignore { pattern }) => {
                assert_eq!(pattern, "*.tmp");
            }
            _ => panic!("Expected Ignore command"),
        }
    }

    #[test]
    fn test_reset_command() {
        let cli = setup_test_cli();
        let matches = cli.try_get_matches_from(&["chaser", "reset"]).unwrap();
        match parse_command(&matches) {
            Some(Commands::Reset) => {}
            _ => panic!("Expected Reset command"),
        }
    }

    #[test]
    fn test_lang_command() {
        let cli = setup_test_cli();
        let matches = cli
            .try_get_matches_from(&["chaser", "lang", "zh-cn"])
            .unwrap();
        match parse_command(&matches) {
            Some(Commands::Lang { language }) => {
                assert_eq!(language, "zh-cn");
            }
            _ => panic!("Expected Lang command"),
        }
    }

    #[test]
    fn test_add_target_command() {
        let cli = setup_test_cli();
        let matches = cli
            .try_get_matches_from(&["chaser", "add-target", "config.json"])
            .unwrap();
        match parse_command(&matches) {
            Some(Commands::AddTarget { file }) => {
                assert_eq!(file, "config.json");
            }
            _ => panic!("Expected AddTarget command"),
        }
    }

    #[test]
    fn test_remove_target_command() {
        let cli = setup_test_cli();
        let matches = cli
            .try_get_matches_from(&["chaser", "remove-target", "config.json"])
            .unwrap();
        match parse_command(&matches) {
            Some(Commands::RemoveTarget { file }) => {
                assert_eq!(file, "config.json");
            }
            _ => panic!("Expected RemoveTarget command"),
        }
    }

    #[test]
    fn test_list_targets_command() {
        let cli = setup_test_cli();
        let matches = cli
            .try_get_matches_from(&["chaser", "list-targets"])
            .unwrap();
        match parse_command(&matches) {
            Some(Commands::ListTargets) => {}
            _ => panic!("Expected ListTargets command"),
        }
    }

    #[test]
    fn test_status_command() {
        let cli = setup_test_cli();
        let matches = cli.try_get_matches_from(&["chaser", "status"]).unwrap();
        match parse_command(&matches) {
            Some(Commands::Status) => {}
            _ => panic!("Expected Status command"),
        }
    }

    #[test]
    fn test_sync_command() {
        let cli = setup_test_cli();
        let matches = cli.try_get_matches_from(&["chaser", "sync"]).unwrap();
        match parse_command(&matches) {
            Some(Commands::Sync { once }) => {
                assert!(!once);
            }
            _ => panic!("Expected Sync command"),
        }

        let cli = setup_test_cli();
        let matches = cli
            .try_get_matches_from(&["chaser", "sync", "--once"])
            .unwrap();
        match parse_command(&matches) {
            Some(Commands::Sync { once }) => {
                assert!(once);
            }
            _ => panic!("Expected Sync command with once flag"),
        }
    }

    #[test]
    fn test_update_path_command() {
        let cli = setup_test_cli();
        let matches = cli
            .try_get_matches_from(&["chaser", "update-path", "/old/path", "/new/path"])
            .unwrap();
        match parse_command(&matches) {
            Some(Commands::UpdatePath { old_path, new_path }) => {
                assert_eq!(old_path, "/old/path");
                assert_eq!(new_path, "/new/path");
            }
            _ => panic!("Expected UpdatePath command"),
        }
    }

    #[test]
    fn test_invalid_command() {
        let cli = setup_test_cli();
        let result = cli.try_get_matches_from(&["chaser", "invalid"]);
        assert!(result.is_err());
    }

    #[test]
    fn test_missing_required_args() {
        let cli = setup_test_cli();

        // Test Add command without path
        let result = cli.try_get_matches_from(&["chaser", "add"]);
        assert!(result.is_err());

        // Test Remove command without path
        let cli = setup_test_cli();
        let result = cli.try_get_matches_from(&["chaser", "remove"]);
        assert!(result.is_err());

        // Test Recursive command without enabled flag
        let cli = setup_test_cli();
        let result = cli.try_get_matches_from(&["chaser", "recursive"]);
        assert!(result.is_err());

        // Test Ignore command without pattern
        let cli = setup_test_cli();
        let result = cli.try_get_matches_from(&["chaser", "ignore"]);
        assert!(result.is_err());

        // Test Lang command without language
        let cli = setup_test_cli();
        let result = cli.try_get_matches_from(&["chaser", "lang"]);
        assert!(result.is_err());

        // Test UpdatePath command without paths
        let cli = setup_test_cli();
        let result = cli.try_get_matches_from(&["chaser", "update-path"]);
        assert!(result.is_err());

        let cli = setup_test_cli();
        let result = cli.try_get_matches_from(&["chaser", "update-path", "/old/path"]);
        assert!(result.is_err());
    }

    #[test]
    fn test_help_flag() {
        let cli = setup_test_cli();
        let result = cli.try_get_matches_from(&["chaser", "--help"]);
        assert!(result.is_err()); // Help flag causes parse to "fail" but shows help
    }

    #[test]
    fn test_version_flag() {
        let cli = setup_test_cli();
        let result = cli.try_get_matches_from(&["chaser", "--version"]);
        assert!(result.is_err()); // Version flag causes parse to "fail" but shows version
    }

    #[test]
    fn test_commands_with_special_characters() {
        // Test paths with spaces and special characters
        let cli = setup_test_cli();
        let matches = cli
            .try_get_matches_from(&["chaser", "add", "/path with spaces/test"])
            .unwrap();
        match parse_command(&matches) {
            Some(Commands::Add { path }) => {
                assert_eq!(path, "/path with spaces/test");
            }
            _ => panic!("Expected Add command"),
        }

        // Test ignore patterns with special characters
        let cli = setup_test_cli();
        let matches = cli
            .try_get_matches_from(&["chaser", "ignore", "*.log*"])
            .unwrap();
        match parse_command(&matches) {
            Some(Commands::Ignore { pattern }) => {
                assert_eq!(pattern, "*.log*");
            }
            _ => panic!("Expected Ignore command"),
        }
    }

    #[test]
    fn test_recursive_various_values() {
        let test_cases = vec![
            "true", "false", "1", "0", "yes", "no", "on", "off", "invalid",
        ];

        for value in test_cases {
            let cli = setup_test_cli();
            let result = cli.try_get_matches_from(&["chaser", "recursive", value]);
            assert!(
                result.is_ok(),
                "Failed to parse recursive with value: {}",
                value
            );

            match parse_command(&result.unwrap()) {
                Some(Commands::Recursive { enabled }) => {
                    assert_eq!(enabled, value);
                }
                _ => panic!("Expected Recursive command for value: {}", value),
            }
        }
    }
}