Compare commits
3 Commits
5c261c0d34
...
3a4e32471a
| Author | SHA1 | Date | |
|---|---|---|---|
| 3a4e32471a | |||
| 3a223aa084 | |||
| 9c0d92d07c |
103
src/main.rs
103
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<String>| {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
50
src/utils.rs
50
src/utils.rs
@@ -1,7 +1,21 @@
|
||||
use crate::structs::{Library, Shelf};
|
||||
use std::{fs, io, path::Path};
|
||||
use std::{
|
||||
fs,
|
||||
io::{self, Write},
|
||||
path::Path,
|
||||
};
|
||||
use xdg::BaseDirectories;
|
||||
|
||||
pub fn prompt(text: &str) -> Result<String, std::io::Error> {
|
||||
print!("{} ", text);
|
||||
std::io::stdout().flush()?;
|
||||
|
||||
let mut response = String::new();
|
||||
std::io::stdin().read_line(&mut response);
|
||||
|
||||
Ok(response.trim_end().to_owned())
|
||||
}
|
||||
|
||||
pub fn get_shelf_index(shelves: &Vec<Shelf>, id: &u8) -> Option<usize> {
|
||||
shelves.iter().position(|shelf| shelf.id == *id)
|
||||
}
|
||||
@@ -76,26 +90,26 @@ pub fn match_file_name(file_name: &str, shelf: &mut Shelf) -> Result<usize, std:
|
||||
// Turn given file into string
|
||||
let match_path = Path::new(file_name);
|
||||
|
||||
// If the file path doesnt exist
|
||||
if !match_path.exists() {
|
||||
let file_ending = match_path.file_name().expect("Error getting file name");
|
||||
// For every entry in current shelf
|
||||
for (i, path) in shelf.content.iter().enumerate() {
|
||||
// Turn the string back into a path
|
||||
let shelf_path = Path::new(path).canonicalize()?;
|
||||
|
||||
if shelf_path.ends_with(file_ending) {
|
||||
return Ok(i);
|
||||
};
|
||||
}
|
||||
} // else if it does
|
||||
|
||||
let cannonical_path = match_path.canonicalize()?;
|
||||
|
||||
// Check if there is a match in the shelf before taking the path verbatim
|
||||
let file_ending = match_path.file_name().expect("Error getting file name");
|
||||
// For every entry in current shelf
|
||||
for (i, path) in shelf.content.iter().enumerate() {
|
||||
// Turn the string back into a path
|
||||
let shelf_path = Path::new(path).canonicalize()?;
|
||||
if cannonical_path == shelf_path {
|
||||
|
||||
if shelf_path.ends_with(file_ending) {
|
||||
return Ok(i);
|
||||
};
|
||||
}
|
||||
|
||||
if match_path.exists() {
|
||||
let cannonical_path = match_path.canonicalize()?;
|
||||
|
||||
for (i, path) in shelf.content.iter().enumerate() {
|
||||
let shelf_path = Path::new(path).canonicalize()?;
|
||||
if cannonical_path == shelf_path {
|
||||
return Ok(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user