diff --git a/src/main.rs b/src/main.rs index 2ec7327..8c97e12 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,4 @@ -use std::{env, path::Path, process}; +use std::{env, fs, path::Path, process}; use crate::structs::{Library, Shelf}; use crate::utils::save_library; @@ -24,6 +24,11 @@ fn main() { "add" => add_files(&arg, &mut args, &mut current_shelf), "rm" => remove_files(&mut args, &mut current_shelf), "ls" => list_library(&mut library), + "cp" => copy_files( + &mut args, + _binary.unwrap_or("shelf".into()), + &mut current_shelf, + ), &_ => add_files(&arg, &mut args, &mut current_shelf), } } else { @@ -120,3 +125,99 @@ fn remove_files(args: &mut env::Args, shelf: &mut Shelf) { shelf.content.clear() } } + +fn copy_files(args: &mut env::Args, binary: String, shelf: &mut Shelf) { + if args.len() > 2 { + eprintln!( + "Invalid number of arguments. {} cp takes either 0 or 2 arguments", + binary + ); + return; + } + + let move_file = |source: &Path, destination: &Path| { + // Check if the destination is free, or if you are allowed to write over a file + let ok_to_write = if !destination.exists() { + let _ = fs::File::create_new(destination); + true + } else { + match utils::prompt("file {destination} exists. Overwrite? (y/N) ") { + Ok(input) => { + if input == "y" || input == "Y" { + true + } else { + false + } + } + Err(e) => { + eprintln!("Input Error: {e}"); + return; + } + } + }; + + // If you can't write to the destination + if ok_to_write == false { + eprintln!("Skipping file {} (disallowed)", source.to_str().unwrap()); + return; + } + if !(source == destination) { + println!( + "Copying file {} to {}", + source.to_str().unwrap(), + destination.to_str().unwrap() + ); + let _ = fs::copy(source, destination); + return; + } + eprintln!( + "Skipping file {} (identical destination)", + source.to_str().unwrap() + ) + }; + + let make_new_file_path = |old_file_path: &Path| { + let old_file_name = &old_file_path.file_name().unwrap(); + + let new_file_path_str = env::var("PWD").unwrap() + "/" + old_file_name.to_str().unwrap(); + let new_file_path = Path::new(&new_file_path_str); + return new_file_path.to_owned(); + }; + + let mut get_old_file = |old_file: Option| { + let old_file_index = match utils::match_file_name(&old_file.unwrap(), shelf) { + Ok(index) => index, + Err(e) => { + eprintln!("{e}"); + process::exit(1) + } + }; + let old_file_path = Path::new(&shelf.content[old_file_index]); + return old_file_path.to_owned(); + }; + + if args.len() == 0 { + for (_, file) in shelf.content.iter().enumerate() { + let old_file_path = Path::new(&file); + + let new_file_path = make_new_file_path(&old_file_path); + + move_file(&old_file_path, &new_file_path) + } + } else if args.len() == 1 { + let old_file = args.next(); + + let old_file_path = get_old_file(old_file); + let new_file_path = make_new_file_path(&old_file_path); + move_file(&old_file_path, &new_file_path) + } else { + // args.len is 2 + let old_file = args.next(); + let new_file = args.next().unwrap(); + + let old_file_path = get_old_file(old_file); + let new_file_path = Path::new(&new_file); + + move_file(&old_file_path, &new_file_path) + } +}