Skip to content

Commit 6dfca37

Browse files
committed
Initial commit
1 parent 0e10b0d commit 6dfca37

File tree

4 files changed

+193
-81
lines changed

4 files changed

+193
-81
lines changed

Cargo.lock

Lines changed: 98 additions & 75 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
[package]
22
name = "rust_file_sync_tool"
33
version = "0.1.0"
4-
edition = "2021"
4+
edition = "2024"
55

66
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
77

88
[dependencies]
9-
clap = "3.0"
10-
11-
[lib]
12-
name = "rust_file_sync_tool"
13-
path = "src/main.rs"
9+
clap = "4.5.4"

src/cli.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
use clap::{App, Arg};
2+
3+
pub fn run() {
4+
let matches = App::new("Rust File Sync Tool")
5+
.version("0.1.0")
6+
.author("Enow Scott")
7+
.about("Synchronize files between directories")
8+
.arg(
9+
Arg::with_name("source")
10+
.help("Source directory")
11+
.required(true)
12+
.index(1),
13+
)
14+
.arg(
15+
Arg::with_name("destination")
16+
.help("Destination directory")
17+
.required(true)
18+
.index(2),
19+
)
20+
.arg(
21+
Arg::with_name("delete")
22+
.short("d")
23+
.long("delete")
24+
.help("Delete files not present in source directory"),
25+
)
26+
.get_matches();
27+
28+
let source_dir = matches.value_of("source").unwrap();
29+
let destination_dir = matches.value_of("destination").unwrap();
30+
let delete_files = matches.is_present("delete");
31+
32+
// Call synchronization logic
33+
sync::sync_directories(source_dir, destination_dir, delete_files);
34+
}

src/sync.rs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
use std::fs;
2+
use std::io;
3+
4+
pub fn sync_directories(source_dir: &str, destination_dir: &str, delete_files: bool) {
5+
// Implement synchronization logic here
6+
println!("Syncing files from '{}' to '{}'", source_dir, destination_dir);
7+
8+
// Example: Copy all files from source to destination
9+
match fs::read_dir(source_dir) {
10+
Ok(entries) => {
11+
for entry in entries {
12+
if let Ok(entry) = entry {
13+
let file_name = entry.file_name();
14+
let source_path = entry.path();
15+
let dest_path = format!("{}/{}", destination_dir, file_name.to_string_lossy());
16+
17+
if let Err(err) = fs::copy(&source_path, &dest_path) {
18+
println!("Error copying file: {}", err);
19+
}
20+
}
21+
}
22+
}
23+
Err(err) => {
24+
eprintln!("Error reading source directory: {}", err);
25+
}
26+
}
27+
28+
// Optionally, delete files not present in source directory
29+
if delete_files {
30+
delete_extra_files(source_dir, destination_dir);
31+
}
32+
}
33+
34+
fn delete_extra_files(source_dir: &str, destination_dir: &str) {
35+
// Implement deletion of extra files logic here
36+
println!("Deleting extra files from '{}'", destination_dir);
37+
38+
// Example: List all files in destination directory and delete those not present in source directory
39+
match fs::read_dir(destination_dir) {
40+
Ok(entries) => {
41+
for entry in entries {
42+
if let Ok(entry) = entry {
43+
let file_name = entry.file_name();
44+
let source_path = format!("{}/{}", source_dir, file_name.to_string_lossy());
45+
let dest_path = entry.path();
46+
47+
if !fs::metadata(&source_path).is_ok() {
48+
if let Err(err) = fs::remove_file(&dest_path) {
49+
eprintln!("Error deleting file: {}", err);
50+
}
51+
}
52+
}
53+
}
54+
}
55+
Err(err) => {
56+
eprintln!("Error reading destination directory: {}", err);
57+
}
58+
}
59+
}

0 commit comments

Comments
 (0)