1
0
Fork 0
mirror of https://git.sr.ht/~nixgoat/vento synced 2024-11-23 23:33:40 +00:00

Compare commits

...

4 commits

Author SHA1 Message Date
Lux Aliaga d0ddc93b47 Merge pull request 'Refactoring' (#9) from refactor into master
Reviewed-on: https://codeberg.org/nixgoat/vento/pulls/9
2022-10-18 01:22:37 +02:00
Lux Aliaga 3b9a405614
build: Use struct for page data
The tuple approach was a bit complicated, so I decided to switch it out
for a Page struct. Yes, I have an obsession with structs.
2022-10-17 20:09:44 -03:00
Lux Aliaga 5e05bfa2ef
src: Removed unnecessary contexts
This commit should remove some contexts that actually made some errors
more difficult to read. They will now display the idiomatic errors
generated by anyhow.
2022-10-17 19:56:23 -03:00
Lux Aliaga d51163ef09
common: Use struct to deliver settings
After learning more about structs, I've decided to try and use them in
the common module. It replaces the original behavior of delivering the
data using a vector in favor of the Settings struct, also defined on the
same file.
2022-10-17 18:11:38 -03:00
4 changed files with 68 additions and 46 deletions

View file

@ -23,6 +23,11 @@ use std::env;
use std::fs::{create_dir_all, File};
use std::io::Write;
struct Page {
content: String,
file: String,
}
fn main() -> Result<()> {
if cfg!(unix) {
let pages = [vento()?, take()?, drop()?, ventotoml()?];
@ -32,16 +37,16 @@ fn main() -> Result<()> {
create_dir_all(tempdir.clone())?;
for page in &pages {
let tempfile = tempdir.join(page.clone().1);
let tempfile = tempdir.join(&page.file);
let mut file = File::create(tempfile).unwrap();
write!(&mut file, "{}", page.clone().0).unwrap();
write!(&mut file, "{}", &page.content).unwrap();
}
}
Ok(())
}
fn vento() -> Result<(String, String)> {
let page = Manual::new("vento")
fn vento() -> Result<Page> {
let content = Manual::new("vento")
.about("a CLI inventory for your files")
.author(Author::new("Lux Aliaga").email("they@mint.lgbt"))
.description("List files and directories in the currently active inventory, the files in SLOT, the files in DIRECTORY or the files in DIRECTORY in SLOT.")
@ -76,11 +81,14 @@ fn vento() -> Result<(String, String)> {
)
.render();
Ok((page, String::from("vento.1")))
Ok(Page {
content,
file: String::from("vento.1"),
})
}
fn take() -> Result<(String, String)> {
let page = Manual::new("take")
fn take() -> Result<Page> {
let content = Manual::new("take")
.about("a file grabber for Vento")
.author(Author::new("Lux Aliaga").email("they@mint.lgbt"))
.description("Take FILE and put it in the inventory.")
@ -93,11 +101,14 @@ fn take() -> Result<(String, String)> {
.arg(Arg::new("FILE"))
.render();
Ok((page, String::from("take.1")))
Ok(Page {
content,
file: String::from("take.1"),
})
}
fn drop() -> Result<(String, String)> {
let page = Manual::new("drop")
fn drop() -> Result<Page> {
let content = Manual::new("drop")
.about("a file dropper for Vento")
.author(Author::new("Lux Aliaga").email("they@mint.lgbt"))
.description("Take FILE off the inventory and drop it in DESTINATION.")
@ -111,11 +122,14 @@ fn drop() -> Result<(String, String)> {
.arg(Arg::new("[DESTINATION]"))
.render();
Ok((page, String::from("drop.1")))
Ok(Page {
content,
file: String::from("drop.1"),
})
}
fn ventotoml() -> Result<(String, String)> {
let page = Manual::new("vento.toml")
fn ventotoml() -> Result<Page> {
let content = Manual::new("vento.toml")
.about("configuration file for Vento")
.author(Author::new("Lux Aliaga").email("they@mint.lgbt"))
.description("This is the configuration file for the vento(1), take(1) and drop(1) utilities. Its presence and all its directives are optional.")
@ -131,5 +145,8 @@ fn ventotoml() -> Result<(String, String)> {
)
.render();
Ok((page, String::from("vento.toml.1")))
Ok(Page {
content,
file: String::from("vento.toml.1"),
})
}

View file

@ -22,7 +22,13 @@ use colored::Colorize;
use config::Config;
use std::path::{Path, PathBuf};
pub fn env_config() -> Result<Vec<PathBuf>> {
pub struct Settings {
pub vento_dir: PathBuf,
pub active_dir: PathBuf,
pub inactive_dir: PathBuf,
}
pub fn env_config() -> Result<Settings> {
// Configures the directories for Vento
let home = match dirs::home_dir() {
Option::Some(dir) => dir,
@ -45,7 +51,11 @@ pub fn env_config() -> Result<Vec<PathBuf>> {
.iter()
.collect();
Ok(vec![vento_dir, active_dir, inactive_dir])
Ok(Settings {
vento_dir,
active_dir,
inactive_dir,
})
}
fn dir_config() -> Result<String> {

View file

@ -27,16 +27,14 @@ use std::{fs, process};
pub fn init() -> Result<()> {
// Initializes Vento
let ventodir = &common::env_config()?[0];
let ventodir = &common::env_config()?.vento_dir;
if ventodir.is_dir() {
// Checks if Vento has already been initialized and prompts the user if they want to initialize it again
let mut answer = String::new();
print!("⚠️ {} Vento has already been initialized. Reinitializing will delete all files on the directory for Vento. Do you wish to proceed? (y/N) ", "WARNING:".bold().red());
let _ = io::stdout().flush();
io::stdin()
.read_line(&mut answer)
.context("Failed to read input")?;
io::stdin().read_line(&mut answer)?;
match answer.as_str().trim() {
"y" | "Y" => fs::remove_dir_all(&ventodir)?,
_ => process::exit(0),
@ -49,7 +47,7 @@ pub fn init() -> Result<()> {
pub fn list(slot: &str, dir: &str) -> Result<()> {
// Lists files in inventory
let ventodir = &common::env_config()?[0];
let ventodir = &common::env_config()?.vento_dir;
if !ventodir.is_dir() {
// Detects if Vento hasn't been initialized and bails if so
@ -60,8 +58,8 @@ pub fn list(slot: &str, dir: &str) -> Result<()> {
}
let mut slotdir: PathBuf = match slot {
"active" | "a" => common::env_config()?[1].clone(),
"inactive" | "i" => common::env_config()?[2].clone(),
"active" | "a" => common::env_config()?.active_dir.clone(),
"inactive" | "i" => common::env_config()?.inactive_dir.clone(),
_ => PathBuf::new(),
};
@ -168,9 +166,9 @@ pub fn list(slot: &str, dir: &str) -> Result<()> {
pub fn switch() -> Result<()> {
// Switches between inventory slots
let ventodir = &common::env_config()?[0];
let active = &common::env_config()?[1];
let inactive = &common::env_config()?[2];
let ventodir = &common::env_config()?.vento_dir;
let active = &common::env_config()?.active_dir;
let inactive = &common::env_config()?.inactive_dir;
let temp: PathBuf = [ventodir.to_path_buf(), Path::new("temp").to_path_buf()]
.iter()
.collect();
@ -187,13 +185,11 @@ pub fn switch() -> Result<()> {
fn create_slots() -> Result<()> {
// Used only on init. Creates all required directories
let active = &common::env_config()?[1];
let inactive = &common::env_config()?[2];
let active = &common::env_config()?.active_dir;
let inactive = &common::env_config()?.inactive_dir;
let initialize_error = "Vento was unable to initalize. Do you have the correct permissions?";
fs::create_dir_all(active).context(initialize_error)?;
fs::create_dir_all(inactive).context(initialize_error)?;
fs::create_dir_all(active)?;
fs::create_dir_all(inactive)?;
println!("🎉 {}", "Vento has been succesfully initialized!".green());
Ok(())

View file

@ -18,7 +18,7 @@
*/
use super::common;
use anyhow::{bail, Context, Result};
use anyhow::{bail, Result};
use colored::Colorize;
use fs_extra::dir::{move_dir, CopyOptions};
use std::fs;
@ -26,7 +26,7 @@ use std::path::{Path, PathBuf};
pub fn take(file: &String, slot: &str) -> Result<()> {
// Takes a file or directory
let ventodir = &common::env_config()?[0];
let ventodir = &common::env_config()?.vento_dir;
if !ventodir.is_dir() {
// Detects if Vento hasn't been initialized and bails if so
@ -36,8 +36,8 @@ pub fn take(file: &String, slot: &str) -> Result<()> {
);
};
let slotdir: PathBuf = match slot {
"active" | "a" => common::env_config()?[1].clone(),
"inactive" | "i" => common::env_config()?[2].clone(),
"active" | "a" => common::env_config()?.active_dir.clone(),
"inactive" | "i" => common::env_config()?.inactive_dir.clone(),
_ => PathBuf::new(),
};
@ -80,11 +80,11 @@ pub fn take(file: &String, slot: &str) -> Result<()> {
if sourcepath.is_file() | sourcepath.is_symlink() {
// Checks the path's file type
fs::copy(&file, &destpath).context("Vento was unable to copy the file.")?;
fs::remove_file(&file).context("Vento was unable to remove the file.")?;
fs::copy(&file, &destpath)?;
fs::remove_file(&file)?;
} else if sourcepath.is_dir() {
let options = CopyOptions::new();
move_dir(&file, &slotdir, &options).context("Vento was unable to move the directory.")?;
move_dir(&file, &slotdir, &options)?;
} else {
bail!("{}", "No such file or directory.".red());
}
@ -94,7 +94,7 @@ pub fn take(file: &String, slot: &str) -> Result<()> {
pub fn drop(file: &String, slot: &str, dest: PathBuf) -> Result<()> {
// Drops a file or directory
let ventodir = &common::env_config()?[0];
let ventodir = &common::env_config()?.vento_dir;
if !ventodir.is_dir() {
// Detects if Vento hasn't been initialized and bails if so
@ -105,8 +105,8 @@ pub fn drop(file: &String, slot: &str, dest: PathBuf) -> Result<()> {
};
let slotdir: PathBuf = match slot {
"active" | "a" => common::env_config()?[1].clone(),
"inactive" | "i" => common::env_config()?[2].clone(),
"active" | "a" => common::env_config()?.active_dir.clone(),
"inactive" | "i" => common::env_config()?.inactive_dir.clone(),
_ => PathBuf::new(),
};
@ -138,13 +138,12 @@ pub fn drop(file: &String, slot: &str, dest: PathBuf) -> Result<()> {
if sourcepath.is_file() | sourcepath.is_symlink() {
// Checks the path's file type
fs::copy(&sourcepath, &destpath).context("Vento was unable to copy the file.")?;
fs::remove_file(&sourcepath).context("Vento was unable to remove the file.")?;
fs::copy(&sourcepath, &destpath)?;
fs::remove_file(&sourcepath)?;
} else if sourcepath.is_dir() {
let destpath: PathBuf = Path::new(&dest).to_path_buf();
let options = CopyOptions::new();
move_dir(&sourcepath, &destpath, &options)
.context("Vento was unable to move the directory.")?;
move_dir(&sourcepath, &destpath, &options)?;
} else {
bail!("{}", "No such file or directory.".red());
}