2022-09-14 21:45:47 +00:00
|
|
|
/*
|
|
|
|
* Vento, a CLI inventory for your files.
|
|
|
|
* Copyright (C) 2022 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/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2022-09-17 08:44:25 +00:00
|
|
|
use super::common;
|
2022-09-19 03:23:54 +00:00
|
|
|
use anyhow::{bail, Context, Result};
|
2022-09-17 08:44:25 +00:00
|
|
|
use colored::Colorize;
|
|
|
|
use fs_extra::dir::{move_dir, CopyOptions};
|
2022-09-15 02:20:17 +00:00
|
|
|
use std::fs;
|
|
|
|
use std::path::{Path, PathBuf};
|
|
|
|
|
2022-09-19 03:23:54 +00:00
|
|
|
pub fn take(file: &String) -> Result<()> {
|
2022-09-17 08:44:25 +00:00
|
|
|
// Takes a file or directory
|
2022-09-19 03:23:54 +00:00
|
|
|
let active = &common::env_config()?[1];
|
2022-09-17 08:44:25 +00:00
|
|
|
|
2022-09-15 02:20:17 +00:00
|
|
|
let sourcepath: PathBuf = Path::new(&file).to_path_buf();
|
|
|
|
let destpath: PathBuf = [&active, &Path::new(file).to_path_buf()].iter().collect();
|
2022-09-17 08:44:25 +00:00
|
|
|
|
2022-09-15 02:59:57 +00:00
|
|
|
if Path::exists(&destpath) {
|
2022-09-19 03:23:54 +00:00
|
|
|
bail!(
|
2022-09-17 08:44:25 +00:00
|
|
|
"❌ {}",
|
|
|
|
format!("A file with the same name already exists in your inventory!").red()
|
|
|
|
);
|
2022-09-15 02:20:17 +00:00
|
|
|
} else if sourcepath.is_file() | sourcepath.is_symlink() {
|
|
|
|
fs::copy(&file, &destpath).expect("❌ Vento was unable to copy the file.");
|
|
|
|
fs::remove_file(&file).expect("❌ Vento was unable to remove the file.");
|
|
|
|
} else if sourcepath.is_dir() {
|
|
|
|
let options = CopyOptions::new();
|
|
|
|
move_dir(&file, &active, &options).expect("❌ Vento was unable to move the directory.");
|
|
|
|
} else {
|
|
|
|
println!("❌ {}", format!("No such file or directory.").red());
|
|
|
|
}
|
2022-09-19 03:23:54 +00:00
|
|
|
Ok(())
|
2022-09-14 12:36:34 +00:00
|
|
|
}
|
|
|
|
|
2022-09-19 03:23:54 +00:00
|
|
|
pub fn drop(file: &String, dest: PathBuf) -> Result<()> {
|
2022-09-17 08:44:25 +00:00
|
|
|
// Drops a file or directory
|
2022-09-19 03:23:54 +00:00
|
|
|
let active = &common::env_config()?[1];
|
2022-09-17 08:44:25 +00:00
|
|
|
|
2022-09-15 02:52:00 +00:00
|
|
|
let sourcepath: PathBuf = [&active, &Path::new(file).to_path_buf()].iter().collect();
|
2022-09-17 08:44:25 +00:00
|
|
|
let destpath: PathBuf = [
|
|
|
|
Path::new(&dest).to_path_buf(),
|
|
|
|
Path::new(file).to_path_buf(),
|
|
|
|
]
|
|
|
|
.iter()
|
|
|
|
.collect();
|
2022-09-15 02:52:00 +00:00
|
|
|
|
2022-09-17 08:44:25 +00:00
|
|
|
if Path::exists(&destpath) {
|
|
|
|
// HAHA YANDEREDEV MOMENT. This checks what method to use for the file/directory the user has picked
|
2022-09-19 03:23:54 +00:00
|
|
|
bail!("❌ {}", format!("A file with the same name already exists in the destination! Try renaming it or dropping this file somewhere else.").red());
|
2022-09-15 02:52:00 +00:00
|
|
|
} else if sourcepath.is_file() | sourcepath.is_symlink() {
|
2022-09-19 03:23:54 +00:00
|
|
|
fs::copy(&sourcepath, &destpath).context("❌ Vento was unable to copy the file.")?;
|
|
|
|
fs::remove_file(&sourcepath).context("❌ Vento was unable to remove the file.")?;
|
2022-09-15 02:52:00 +00:00
|
|
|
} else if sourcepath.is_dir() {
|
|
|
|
let destpath: PathBuf = Path::new(&dest).to_path_buf();
|
|
|
|
let options = CopyOptions::new();
|
2022-09-17 08:44:25 +00:00
|
|
|
move_dir(&sourcepath, &destpath, &options)
|
|
|
|
.expect("❌ Vento was unable to move the directory.");
|
2022-09-15 02:52:00 +00:00
|
|
|
} else {
|
2022-09-19 03:23:54 +00:00
|
|
|
bail!("❌ {}", format!("No such file or directory.").red());
|
2022-09-15 02:52:00 +00:00
|
|
|
}
|
2022-09-19 03:23:54 +00:00
|
|
|
Ok(())
|
2022-09-14 12:36:34 +00:00
|
|
|
}
|