Skip to content

Commit 204257b

Browse files
committed
worked on the changed_only flag to copy only the changed files otherwise replace all the files
1 parent 0ae8cd5 commit 204257b

5 files changed

Lines changed: 78 additions & 53 deletions

File tree

Cargo.lock

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

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ walkdir = "2.5.0"
1010
[workspace.package]
1111
edition = "2024"
1212
repository = "https://github.com/ibilalkayy/cover"
13-
version = "0.1.3"
13+
version = "0.1.4"
1414
license = "Apache-2.0"
1515
readme = "README.md"
1616
categories = ["command-line-utilities"]

cli/src/flags/sync.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,21 @@ pub struct SyncData {
1414

1515
/// Copy only the changed files
1616
#[clap(long)]
17-
pub incremental: Option<bool>,
17+
pub changed_only: bool,
1818

1919
/// Remove files in destination not in source
2020
#[clap(long)]
21-
pub delete: Option<bool>,
21+
pub delete: bool,
2222

2323
/// Show what would happen after syncing
2424
#[clap(long)]
25-
pub dry_run: Option<bool>,
25+
pub dry_run: bool,
2626

2727
/// Show detailed logs
2828
#[clap(long)]
29-
pub verbose: Option<bool>,
29+
pub verbose: bool,
3030

3131
/// Check file hashes instead of timestamps
3232
#[clap(long)]
33-
pub hash: Option<bool>,
33+
pub hash: bool,
3434
}

cli/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ fn handle_commands() {
1717
let sync_data = SyncData {
1818
source: s.source,
1919
destination: s.destination,
20-
incremental: s.incremental,
20+
changed_only: s.changed_only,
2121
delete: s.delete,
2222
dry_run: s.dry_run,
2323
verbose: s.verbose,

crates/sync/src/lib.rs

Lines changed: 64 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::{
2-
path::PathBuf,
2+
fs::copy,
3+
path::{Path, PathBuf},
34
time::{SystemTime, UNIX_EPOCH},
45
};
56
use time::{OffsetDateTime, macros::format_description};
@@ -8,11 +9,11 @@ use walkdir::{DirEntry, WalkDir};
89
pub struct SyncData {
910
pub source: PathBuf,
1011
pub destination: PathBuf,
11-
pub incremental: Option<bool>,
12-
pub delete: Option<bool>,
13-
pub dry_run: Option<bool>,
14-
pub verbose: Option<bool>,
15-
pub hash: Option<bool>,
12+
pub changed_only: bool,
13+
pub delete: bool,
14+
pub dry_run: bool,
15+
pub verbose: bool,
16+
pub hash: bool,
1617
}
1718

1819
fn file_size(source_entry: DirEntry) -> (f64, &'static str) {
@@ -84,47 +85,71 @@ impl SyncData {
8485
}
8586
}
8687

87-
// Compare source and destination files
88-
for (src_path, src_modified) in &source_files {
89-
let src_file_name = src_path
90-
.file_name()
91-
.expect("Err: failed to get the file name");
92-
let mut dest_path = self.destination.clone();
93-
dest_path.push(src_file_name);
88+
if self.changed_only {
89+
// Compare source and destination files
90+
for (src_path, src_modified) in &source_files {
91+
let src_file_name = src_path
92+
.file_name()
93+
.expect("Err: failed to get the file name(s)");
94+
let mut dest_path = self.destination.clone();
95+
dest_path.push(src_file_name);
9496

95-
let mut found = false;
96-
let mut needs_update = false;
97+
let mut found = false;
98+
let mut needs_update = false;
9799

98-
for (dest_path, dest_modified) in &destination_files {
99-
let dest_file_name = dest_path
100-
.file_name()
101-
.expect("Err: failed to get the file name");
102-
if src_file_name == dest_file_name {
103-
found = true;
104-
if src_modified > dest_modified {
105-
needs_update = true;
100+
for (dest_path, dest_modified) in &destination_files {
101+
let dest_file_name = dest_path
102+
.file_name()
103+
.expect("Err: failed to get the file name(s)");
104+
if src_file_name == dest_file_name {
105+
found = true;
106+
if src_modified > dest_modified {
107+
needs_update = true;
108+
}
109+
break;
106110
}
107-
break;
111+
}
112+
113+
if !found {
114+
std::fs::copy(src_path, &dest_path).expect("Err: failed to copy file(s)");
115+
files_to_be_copied.push(src_file_name.to_owned());
116+
} else if needs_update {
117+
std::fs::copy(src_path, &dest_path).expect("Err: failed to update file(s)");
118+
files_to_be_updated.push(src_file_name.to_owned());
108119
}
109120
}
110121

111-
if !found {
112-
std::fs::copy(src_path, &dest_path).expect("Err: failed to copy file");
113-
files_to_be_copied.push(src_file_name.to_owned());
114-
} else if needs_update {
115-
std::fs::copy(src_path, &dest_path).expect("Err: failed to update file");
116-
files_to_be_updated.push(src_file_name.to_owned());
122+
if !files_to_be_copied.is_empty() {
123+
println!(
124+
"Missing files {:?} are copied to {:?} destination",
125+
files_to_be_copied, self.destination
126+
);
117127
}
118-
}
128+
if !files_to_be_updated.is_empty() {
129+
println!("Files are updated: {:?}", files_to_be_updated);
130+
}
131+
if files_to_be_copied.is_empty() && files_to_be_updated.is_empty() {
132+
eprintln!("No missing or outdated files. Destination is up-to-date.");
133+
}
134+
} else {
135+
if !destination_files.is_empty() {
136+
for (dest_path, _) in &destination_files {
137+
std::fs::remove_file(dest_path).expect("Err: failed to delete the file(s)");
138+
}
119139

120-
if !files_to_be_copied.is_empty() {
121-
println!("Missing files copied: {:?}", files_to_be_copied);
122-
}
123-
if !files_to_be_updated.is_empty() {
124-
println!("Files updated: {:?}", files_to_be_updated);
125-
}
126-
if files_to_be_copied.is_empty() && files_to_be_updated.is_empty() {
127-
eprintln!("No missing or outdated files. Destination is up-to-date.");
140+
let dest_dir = Path::new(&self.destination);
141+
142+
for (src_path, _) in &source_files {
143+
let file_name = src_path
144+
.file_name()
145+
.expect("Err: failed to get the file name(s)");
146+
let new_dest_path = dest_dir.join(file_name);
147+
copy(src_path, new_dest_path).expect("Err: failed to copy the file(s)");
148+
}
149+
println!("All the files are copied to destination successfully");
150+
} else {
151+
eprintln!("Err: destination path is empty");
152+
}
128153
}
129154
}
130155

0 commit comments

Comments
 (0)