diff --git a/Cargo.lock b/Cargo.lock index 967408c..b3a3a6c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,6 +1,6 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. -version = 3 +version = 4 [[package]] name = "anstream" @@ -206,7 +206,7 @@ checksum = "78b3ae25bc7c8c38cec158d1f2757ee79e9b3740fbc7ccf0e59e4b08d793fa89" [[package]] name = "mvw" -version = "0.1.5" +version = "0.1.6" dependencies = [ "clap", "dialoguer", diff --git a/src/dirlist.rs b/src/dirlist.rs index 01095a6..2d88dd9 100644 --- a/src/dirlist.rs +++ b/src/dirlist.rs @@ -12,11 +12,26 @@ enum DirListEntry { Text(String) } -impl ToString for DirListEntry { +impl DirListEntry { fn to_string(&self) -> String { match self { DirListEntry::Text(line) => line.clone(), - DirListEntry::Path(path) => path.display().to_string()[2..].to_string(), + // TODO only strip first to chars if they're `./` + DirListEntry::Path(path) => { + let checkstring = path.display().to_string(); + if &checkstring[0..2] == "./" { + path.display().to_string()[2..].to_string() + } else { + path.display().to_string() + } + }, + } + } + + fn exists(&self) -> bool { + match self { + DirListEntry::Text(filename) => Path::new(filename).exists(), + DirListEntry::Path(path) => path.exists(), } } } @@ -72,12 +87,12 @@ impl DirList { } } - pub fn from_list(list: &[String]) -> Self { + pub fn from_list(list: &[PathBuf]) -> Self { Self { safe_source: false, noop: false, verbose: false, - entries: list.iter().map(|l| DirListEntry::Text(l.to_string())).collect(), + entries: list.iter().map(|l| DirListEntry::Path(l.clone())).collect(), } } @@ -102,8 +117,20 @@ impl DirList { let mut intermediate_files: HashMap = HashMap::new(); self.run_basic_checks(target_list)?; + if ! self.safe_source { - println!("Implement check if target files already exist"); + // if target == source => next + // if target exists and is not in source list => Err + // if target exists and is in source list => use intermediate files, which is normal functionality below + let tgt_len = target_list.entries.len(); + for i in 0..tgt_len { + if self.entries[i].to_string() != target_list.entries[i].to_string() { + if target_list.entries[i].exists() { + return Err(format!("ERROR: Target '{}' already exists", target_list.entries[i].to_string()).to_string()); + } + } + + } } for i in 0..src_len { @@ -151,8 +178,7 @@ impl DirList { fn run_basic_checks(&self, target_list: &DirList) -> Result<(), String> { // Make sure there are an equal number of sources and destinations if self.entries.len() != target_list.entries.len() { - return Err("ERROR: Source and target list don't have the same number of - lines.".to_string()); + return Err("ERROR: Source and target list don't have the same number of lines.".to_string()); } // Make sure destination names are not empty diff --git a/src/main.rs b/src/main.rs index 9046b5a..aea3350 100644 --- a/src/main.rs +++ b/src/main.rs @@ -39,9 +39,8 @@ struct Cli { #[arg(short, long)] editor: Option, -// /// Apply substitution pattern(s) instead of using an editor -// #[arg(short, long)] -// pattern: Option>, + #[arg(required = false)] + files: Vec, } @@ -85,14 +84,31 @@ fn edit_temp_file(tmpfile: &String, editor_cli: &Option) { } fn main() { - let args = Cli::parse(); + let mut args = Cli::parse(); // Place lockfile to not have multiple mvw processes running in the same dir at the same time lock_dir(); let _cleanup = Cleanup; // Cleanup LOCK_FILE on panic - // read directory contents into vector of Strings - let mut source_list = DirList::from_current_dir(); + // Do some pre-checks if files were entered on the commandline + // Prevent duplicates + args.files.sort(); + args.files.dedup(); + if ! args.files.is_empty() { + for file in &args.files { + // Check if remaining files exist + if ! file.exists() { + println!("File {:?} does not exist, exiting", file); + process::exit(1); + } + } + } + + let mut source_list = if ! args.files.is_empty() { + DirList::from_list(&args.files) + } else { + DirList::from_current_dir() + }; // create named tempfile and fill with paths let temp_file = create_temp_file();