Compare commits
5 Commits
5c261c0d34
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 175feb370a | |||
| 70289215a7 | |||
| 3a4e32471a | |||
| 3a223aa084 | |||
| 9c0d92d07c |
137
src/main.rs
137
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::structs::{Library, Shelf};
|
||||||
use crate::utils::save_library;
|
use crate::utils::save_library;
|
||||||
@@ -22,8 +22,13 @@ fn main() {
|
|||||||
if let Some(arg) = args.next() {
|
if let Some(arg) = args.next() {
|
||||||
match arg.as_str() {
|
match arg.as_str() {
|
||||||
"add" => add_files(&arg, &mut args, &mut current_shelf),
|
"add" => add_files(&arg, &mut args, &mut current_shelf),
|
||||||
"rm" => remove_files(&mut args, &mut current_shelf),
|
"rm" => remove_files(&mut args, &mut current_shelf).unwrap(),
|
||||||
"ls" => list_library(&mut library),
|
"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),
|
&_ => add_files(&arg, &mut args, &mut current_shelf),
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@@ -80,33 +85,21 @@ fn add_files(current_arg: &str, args: &mut env::Args, shelf: &mut Shelf) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn remove_files(args: &mut env::Args, shelf: &mut Shelf) {
|
fn remove_files(
|
||||||
|
args: &mut env::Args,
|
||||||
|
mut shelf: &mut Shelf,
|
||||||
|
) -> Result<(), Box<dyn std::error::Error>> {
|
||||||
if shelf.content.len() < 1 {
|
if shelf.content.len() < 1 {
|
||||||
println!(
|
println!(
|
||||||
"Shelf \"{}\" is Empty. Are you using the right shelf?",
|
"Shelf \"{}\" is Empty. Are you using the right shelf?",
|
||||||
shelf.name
|
shelf.name
|
||||||
);
|
);
|
||||||
return;
|
return Ok(());
|
||||||
}
|
}
|
||||||
if args.len() > 0 {
|
if args.len() > 0 {
|
||||||
for file in args {
|
for file in args {
|
||||||
// If the argument is a number instead of a file name
|
// If the argument is a number instead of a file name
|
||||||
let index: usize = match file.parse::<usize>() {
|
let index = utils::match_file_name_or_index(&file, &mut shelf)?;
|
||||||
Ok(index) => {
|
|
||||||
println!("path is: {}", &shelf.content[*&index]);
|
|
||||||
index
|
|
||||||
}
|
|
||||||
Err(_) => match utils::match_file_name(&file, shelf) {
|
|
||||||
Ok(index) => {
|
|
||||||
println!("path is: {}", &shelf.content[*&index as usize]);
|
|
||||||
index
|
|
||||||
}
|
|
||||||
Err(e) => {
|
|
||||||
eprintln!("{e}");
|
|
||||||
process::exit(1)
|
|
||||||
}
|
|
||||||
},
|
|
||||||
};
|
|
||||||
let file_path = Path::new(&shelf.content[index]);
|
let file_path = Path::new(&shelf.content[index]);
|
||||||
println!(
|
println!(
|
||||||
"Removed file {} from shelf {}",
|
"Removed file {} from shelf {}",
|
||||||
@@ -115,8 +108,110 @@ fn remove_files(args: &mut env::Args, shelf: &mut Shelf) {
|
|||||||
);
|
);
|
||||||
shelf.content.remove(index);
|
shelf.content.remove(index);
|
||||||
}
|
}
|
||||||
|
return Ok(());
|
||||||
} else {
|
} else {
|
||||||
// If there's no file specified, clear the whole thing
|
// If there's no file specified, clear the whole thing
|
||||||
shelf.content.clear()
|
shelf.content.clear();
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
.canonicalize()
|
||||||
|
.expect("couldn't cannonicalize")
|
||||||
|
.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_or_index(&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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
71
src/utils.rs
71
src/utils.rs
@@ -1,7 +1,22 @@
|
|||||||
use crate::structs::{Library, Shelf};
|
use crate::structs::{Library, Shelf};
|
||||||
use std::{fs, io, path::Path};
|
use std::{
|
||||||
|
fs,
|
||||||
|
io::{self, Write},
|
||||||
|
path::Path,
|
||||||
|
process,
|
||||||
|
};
|
||||||
use xdg::BaseDirectories;
|
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> {
|
pub fn get_shelf_index(shelves: &Vec<Shelf>, id: &u8) -> Option<usize> {
|
||||||
shelves.iter().position(|shelf| shelf.id == *id)
|
shelves.iter().position(|shelf| shelf.id == *id)
|
||||||
}
|
}
|
||||||
@@ -76,26 +91,26 @@ pub fn match_file_name(file_name: &str, shelf: &mut Shelf) -> Result<usize, std:
|
|||||||
// Turn given file into string
|
// Turn given file into string
|
||||||
let match_path = Path::new(file_name);
|
let match_path = Path::new(file_name);
|
||||||
|
|
||||||
// If the file path doesnt exist
|
// Check if there is a match in the shelf before taking the path verbatim
|
||||||
if !match_path.exists() {
|
let file_ending = match_path.file_name().expect("Error getting file name");
|
||||||
let file_ending = match_path.file_name().expect("Error getting file name");
|
// For every entry in current shelf
|
||||||
// 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()?;
|
|
||||||
|
|
||||||
for (i, path) in shelf.content.iter().enumerate() {
|
for (i, path) in shelf.content.iter().enumerate() {
|
||||||
|
// Turn the string back into a path
|
||||||
let shelf_path = Path::new(path).canonicalize()?;
|
let shelf_path = Path::new(path).canonicalize()?;
|
||||||
if cannonical_path == shelf_path {
|
|
||||||
|
if shelf_path.ends_with(file_ending) {
|
||||||
return Ok(i);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -104,3 +119,23 @@ pub fn match_file_name(file_name: &str, shelf: &mut Shelf) -> Result<usize, std:
|
|||||||
format!("File not Found in Shelf \"{}\": {file_name}", shelf.name),
|
format!("File not Found in Shelf \"{}\": {file_name}", shelf.name),
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn match_file_name_or_index(file: &str, shelf: &mut Shelf) -> Result<usize, std::io::Error> {
|
||||||
|
let index: usize = match file.parse::<usize>() {
|
||||||
|
Ok(index) => {
|
||||||
|
println!("path is: {}", &shelf.content[*&index]);
|
||||||
|
index
|
||||||
|
}
|
||||||
|
Err(_) => match match_file_name(&file, shelf) {
|
||||||
|
Ok(index) => {
|
||||||
|
println!("path is: {}", &shelf.content[*&index as usize]);
|
||||||
|
index
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
eprintln!("{e}");
|
||||||
|
process::exit(1)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
};
|
||||||
|
Ok(index)
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user