Compare commits

...

3 Commits

Author SHA1 Message Date
Lux Aliaga 791cdf3193
src: Supress clippy warnings
Most of them were related to unnecessary borrows, although one was
because I pointlessly added a format! inside a println!. Whoops.
2023-02-09 21:35:12 -03:00
Lux Aliaga 4427dadcfc
src: bin: Remove unused crates
After refactoring the compiler started warning about some crates that
were unused, so I removed them.
2023-02-09 21:30:09 -03:00
Lux Aliaga 9e5e0716d1
src: Error refactoring on vento CLI
Since many of the errors were being reiterated in the files for the
binaries I've decided to refactor them so we borrow which errors we want
to show from an enum and then execute a function which essentially bails
and matches each error.
2023-02-09 21:10:39 -03:00
9 changed files with 160 additions and 92 deletions

View File

@ -17,11 +17,14 @@
*
*/
use anyhow::{bail, Result};
use colored::Colorize;
use anyhow::Result;
use std::env;
use std::path::Path;
use vento::{help, item};
use vento::{
common::get_current_dir,
error::{throw_error, ErrorType},
help, item,
};
fn main() -> Result<()> {
// Handles args in Drop
@ -30,34 +33,40 @@ fn main() -> Result<()> {
if args[1].contains("--slot=") {
// Checks if the user has provided the long argument "--slot="
match args.len() {
4 => item::drop(&args[2], &args[1].as_str().replace("--slot=", ""), Path::new(&args[4]).to_path_buf(), true)?,
3 => item::drop(&args[2], &args[1].as_str().replace("--slot=", ""), match env::current_dir() {
Ok(dir) => dir,
Err(_) => bail!("{}", "Vento was unable to detect your current directory. Have you configured your environment correctly?".red())
}, true)?,
2 => bail!("{}", "You need to specify a file".red()),
_ => bail!("{}", "Too many arguments".red()),
4 => item::drop(
&args[2],
&args[1].as_str().replace("--slot=", ""),
Path::new(&args[4]).to_path_buf(),
true,
)?,
3 => item::drop(
&args[2],
&args[1].as_str().replace("--slot=", ""),
get_current_dir()?,
true,
)?,
2 => throw_error(ErrorType::SpecifyFile)?,
_ => throw_error(ErrorType::TooManyArgs)?,
};
} else {
match args[1].as_str() {
"--help" | "-h" => help::drop()?,
"-s" => match args.len() {
5 => item::drop(&args[3], &args[2], Path::new(&args[4]).to_path_buf(), true)?,
4 => item::drop(&args[3], &args[2], match env::current_dir() {
Ok(dir) => dir,
Err(_) => bail!("{}", "Vento was unable to detect your current directory. Have you configured your environment correctly?".red())
}, true)?,
3 => bail!("{}", "You need to specify a file".red()),
2 => bail!("{}", "You need to specify a slot".red()),
_ => bail!("{}", "Too many arguments".red()),
4 => item::drop(&args[3], &args[2], get_current_dir()?, true)?,
3 => throw_error(ErrorType::SpecifyFile)?,
2 => throw_error(ErrorType::SpecifySlot)?,
_ => throw_error(ErrorType::TooManyArgs)?,
},
_ => match args.len() {
3 => item::drop(&args[1], &String::from("active"), Path::new(&args[2]).to_path_buf(), true)?,
2 => item::drop(&args[1], &String::from("active"), match env::current_dir() {
Ok(dir) => dir,
Err(_) => bail!("{}", "Vento was unable to detect your current directory. Have you configured your environment correctly?".red())
}, true)?,
_ => bail!("{}", "Too many arguments".red()),
3 => item::drop(
&args[1],
&String::from("active"),
Path::new(&args[2]).to_path_buf(),
true,
)?,
2 => item::drop(&args[1], &String::from("active"), get_current_dir()?, true)?,
_ => throw_error(ErrorType::TooManyArgs)?,
},
}
}

View File

@ -17,10 +17,12 @@
*
*/
use anyhow::{bail, Result};
use colored::Colorize;
use anyhow::Result;
use std::env;
use vento::{help, item};
use vento::{
error::{throw_error, ErrorType},
help, item,
};
fn main() -> Result<()> {
// Handles args in Vento
@ -30,21 +32,21 @@ fn main() -> Result<()> {
// Checks if the user has provided the long argument "--slot="
match args.len() {
3 => item::take(&args[2], &args[1].replace("--slot=", ""), true)?,
2 => bail!("{}", "You need to specify a file".red()),
_ => bail!("{}", "Too many arguments".red()),
2 => throw_error(ErrorType::SpecifyFile)?,
_ => throw_error(ErrorType::TooManyArgs)?,
};
} else {
match args[1].as_str() {
"--help" | "-h" => help::take()?,
"-s" => match args.len() {
4 => item::take(&args[3], &args[2], true)?,
3 => bail!("{}", "You need to specify a file".red()),
2 => bail!("{}", "You need to specify a slot".red()),
_ => bail!("{}", "Too many arguments".red()),
3 => throw_error(ErrorType::SpecifyFile)?,
2 => throw_error(ErrorType::SpecifySlot)?,
_ => throw_error(ErrorType::TooManyArgs)?,
},
_ => match args.len() {
2 => item::take(&args[1], &String::from("active"), true)?,
_ => bail!("{}", "Too many arguments".red()),
_ => throw_error(ErrorType::TooManyArgs)?,
},
}
}

View File

@ -17,10 +17,12 @@
*
*/
use anyhow::{bail, Result};
use colored::Colorize;
use anyhow::Result;
use std::env;
use vento::{help, history, inv};
use vento::{
error::{throw_error, ErrorType},
help, history, inv,
};
fn main() -> Result<()> {
// Handles args in Vento
@ -32,7 +34,7 @@ fn main() -> Result<()> {
match args.len() {
3 => inv::list(&args[1].replace("--slot=", ""), &args[2])?,
2 => inv::list(&args[1].replace("--slot=", ""), "")?,
_ => bail!("{}", "Too many arguments".red()),
_ => throw_error(ErrorType::TooManyArgs)?,
};
} else {
match args[1].as_str() {
@ -43,8 +45,8 @@ fn main() -> Result<()> {
"-s" => match args.len() {
4 => inv::list(&args[2], &args[3])?,
3 => inv::list(&args[2], "")?,
2 => bail!("{}", "You need to specify a slot".red()),
_ => bail!("{}", "Too many arguments".red()),
2 => throw_error(ErrorType::SpecifySlot)?,
_ => throw_error(ErrorType::TooManyArgs)?,
},
_ => inv::list("active", args[1].as_str())?,
}

View File

@ -17,9 +17,11 @@
*
*/
use crate::error::{throw_error, ErrorType};
use anyhow::{bail, Result};
use colored::Colorize;
use config::Config;
use std::env::current_dir;
use std::fs::File;
use std::io::Write;
use std::path::{Path, PathBuf};
@ -125,3 +127,17 @@ pub fn history(data: HistoryData) -> Result<()> {
Ok(())
}
/// Gets current directory for commands
pub fn get_current_dir() -> Result<PathBuf> {
let currentdir = match current_dir() {
Ok(dir) => dir,
Err(_) => PathBuf::new(),
};
if currentdir == PathBuf::new() {
throw_error(ErrorType::NoCurrentDirectory)?;
}
Ok(currentdir)
}

41
src/error.rs Normal file
View File

@ -0,0 +1,41 @@
/*
* Vento, a CLI inventory for your files.
* Copyright (C) 2023 Lux Aliaga
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
use anyhow::{bail, Result};
use colored::Colorize;
pub enum ErrorType {
TooManyArgs,
SpecifySlot,
SpecifyFile,
NoCurrentDirectory,
}
pub fn throw_error(error: ErrorType) -> Result<()> {
bail!(
"{}",
match error {
ErrorType::TooManyArgs => "Too many arguments",
ErrorType::SpecifySlot => "You need to specify a file",
ErrorType::SpecifyFile => "You need to specify a slot",
ErrorType::NoCurrentDirectory => "Vento was unable to detect your current directory. Have you configured your environment correctly?",
}
.red()
);
}

View File

@ -60,51 +60,48 @@ pub fn undo() -> Result<()> {
}
println!(
"✅ {}",
format!(
"{}{}{}",
match contents[3] {
"take" => "Take",
"drop" => "Drop",
"switch" => "Switch",
_ => "Unknown",
}
.bold(),
" action undone".green(),
match contents[3] {
"take" => format!(
"{}{}{}{}{}{}{}",
" (".green(),
contents[1].bold(),
", from ".green(),
contents[0],
" to ".green(),
match contents[2] {
"active" => contents[2].green(),
"inactive" => contents[2].blue(),
_ => contents[2].red(),
}
.bold(),
" slot)".green(),
),
"drop" => format!(
"{}{}{}{}{}{}{}",
" (".green(),
contents[1].bold(),
", from ".green(),
match contents[2] {
"active" => contents[2].green(),
"inactive" => contents[2].blue(),
_ => contents[2].red(),
}
.bold(),
" slot to ".green(),
contents[0],
")".green(),
),
_ => String::from(""),
}
)
"✅ {}{}{}",
match contents[3] {
"take" => "Take",
"drop" => "Drop",
"switch" => "Switch",
_ => "Unknown",
}
.bold(),
" action undone".green(),
match contents[3] {
"take" => format!(
"{}{}{}{}{}{}{}",
" (".green(),
contents[1].bold(),
", from ".green(),
contents[0],
" to ".green(),
match contents[2] {
"active" => contents[2].green(),
"inactive" => contents[2].blue(),
_ => contents[2].red(),
}
.bold(),
" slot)".green(),
),
"drop" => format!(
"{}{}{}{}{}{}{}",
" (".green(),
contents[1].bold(),
", from ".green(),
match contents[2] {
"active" => contents[2].green(),
"inactive" => contents[2].blue(),
_ => contents[2].red(),
}
.bold(),
" slot to ".green(),
contents[0],
")".green(),
),
_ => String::from(""),
}
);
Ok(())

View File

@ -36,7 +36,7 @@ pub fn init() -> Result<()> {
let _ = io::stdout().flush();
io::stdin().read_line(&mut answer)?;
match answer.as_str().trim() {
"y" | "Y" => fs::remove_dir_all(&ventodir)?,
"y" | "Y" => fs::remove_dir_all(ventodir)?,
_ => process::exit(0),
};
};
@ -175,9 +175,9 @@ pub fn switch(message: bool) -> Result<()> {
let rename_error = "Vento was unable to switch slots. Try running \"vento -i\" and try again";
fs::rename(&active, &temp).context(rename_error)?;
fs::rename(&inactive, &active).context(rename_error)?;
fs::rename(&temp, &inactive).context(rename_error)?;
fs::rename(active, &temp).context(rename_error)?;
fs::rename(inactive, active).context(rename_error)?;
fs::rename(&temp, inactive).context(rename_error)?;
common::history(common::HistoryData {
path: PathBuf::new(),

View File

@ -72,11 +72,11 @@ pub fn take(file: &String, slot: &str, message: bool) -> Result<()> {
if sourcepath.is_file() | sourcepath.is_symlink() {
// Checks the path's file type
fs::copy(&file, &destpath)?;
fs::remove_file(&file)?;
fs::copy(file, &destpath)?;
fs::remove_file(file)?;
} else if sourcepath.is_dir() {
let options = CopyOptions::new();
move_dir(&file, &slotdir, &options)?;
move_dir(file, &slotdir, &options)?;
} else {
bail!("{}", "No such file or directory".red());
}
@ -161,7 +161,7 @@ pub fn drop(file: &String, slot: &str, dest: PathBuf, message: bool) -> Result<(
} else if sourcepath.is_dir() {
let destpath: PathBuf = Path::new(&dest).to_path_buf();
let options = CopyOptions::new();
move_dir(&sourcepath, &destpath, &options)?;
move_dir(&sourcepath, destpath, &options)?;
} else {
bail!("{}", "No such file or directory".red());
}

View File

@ -17,7 +17,8 @@
*
*/
mod common;
pub mod common;
pub mod error;
pub mod help;
pub mod history;
pub mod inv;