implement -n, -v and -e commandline options

This commit is contained in:
Ward Wouts 2024-11-27 11:40:37 +01:00
parent 11ce644fca
commit 83ed7ee559
4 changed files with 179 additions and 20 deletions

View file

@ -5,13 +5,13 @@ use std::io::{ErrorKind, Write, BufRead, BufReader};
use std::collections::HashMap;
use rand::distributions::{Alphanumeric, DistString};
const POSTFIX_TMP_FILE: &str = ".mvwrap";
const LOCK_FILE: &str = ".mvwrap";
pub struct DirList {
safe_source: bool,
pub entries: Vec<String>,
noop: bool,
verbose: bool,
entries: Vec<String>,
}
impl DirList {
@ -27,7 +27,7 @@ impl DirList {
let mut path_list : Vec<String> = vec![];
for path in paths {
let path_string = path.display().to_string();
if path_string != format!("./{}", POSTFIX_TMP_FILE) {
if path_string != format!("./{}", LOCK_FILE) {
path_list.push(path_string[2..].to_string());
}
}
@ -35,6 +35,8 @@ impl DirList {
Self {
safe_source: true,
noop: false,
verbose: false,
entries: path_list,
}
}
@ -48,6 +50,8 @@ impl DirList {
Self {
safe_source: true,
noop: false,
verbose: false,
entries: lines,
}
}
@ -55,10 +59,20 @@ impl DirList {
pub fn from_list(list: &Vec<String>) -> Self {
Self {
safe_source: false,
noop: false,
verbose: false,
entries: list.to_vec(),
}
}
pub fn set_noop(&mut self) {
self.noop = true;
}
pub fn set_verbose(&mut self) {
self.verbose = true;
}
pub fn to_file(&self, target_file: &String) {
let mut file = File::create(target_file).expect("no such file");
@ -83,17 +97,17 @@ impl DirList {
if self.entries.iter().any(|j| j==&target_list.entries[i]) {
let unique = Self::get_unique_entry(&target_list);
intermediate_files.insert(unique.clone(), target_list.entries[i].clone());
fs::rename(&self.entries[i], &unique).expect("failed to rename file");
println!("Moving {} -> {}", self.entries[i], unique);
if ! self.noop { fs::rename(&self.entries[i], &unique).expect("failed to rename file"); }
if self.verbose { println!("Moving {} -> {}", self.entries[i], unique); }
} else {
fs::rename(&self.entries[i], &target_list.entries[i]).expect("failed to rename file");
println!("Moving {} -> {}", self.entries[i], target_list.entries[i]);
if ! self.noop { fs::rename(&self.entries[i], &target_list.entries[i]).expect("failed to rename file"); }
if self.verbose { println!("Moving {} -> {}", self.entries[i], target_list.entries[i]); }
}
}
}
for (src, dst) in intermediate_files.iter() {
fs::rename(src, dst).expect("failed to rename file");
println!("Moving {} -> {}", src, dst);
if ! self.noop { fs::rename(src, dst).expect("failed to rename file"); }
if self.verbose { println!("Moving {} -> {}", src, dst); }
}
Ok(())
}