ableos_userland/programs/undelete/src/main.rs

56 lines
1.7 KiB
Rust

use std::{env, fs::OpenOptions, path::PathBuf};
use fs_extra::{dir, file};
fn main() -> anyhow::Result<()> {
let ret = clparse::Arguments::parse_from_args()?;
let args = ret.1;
let trash_path = env::var("HOME")? + "/.trash/";
let manifest = trash_manifest::Manifest::new(
OpenOptions::new()
.create(true)
.write(true)
.read(true)
.open(trash_path.clone() + ".manifest.ron")?,
)?;
if !args.is_truthy("list") || !args.is_truthy("l") {
let name = {
let n = args.arguments.get("n");
let name = args.arguments.get("name");
match (n, name) {
(None, None) => anyhow::bail!("No name or operation specified"),
(None, Some(name)) => name,
(Some(name), None) => name,
(Some(_), Some(_)) => anyhow::bail!("Option n and name conflict"),
}
};
let file = manifest
.get(name.to_string())
.ok_or(anyhow::anyhow!("File not found in trash manifest"))?;
let from = PathBuf::from(trash_path.clone() + &file.name);
let to = if args.is_truthy("origin") || args.is_truthy("o") {
file.origin
} else {
env::current_dir()?
};
if from.is_dir() {
fs_extra::dir::move_dir(from, &to, &dir::CopyOptions::new())?;
} else {
fs_extra::file::move_file(from, &to, &file::CopyOptions::new())?;
}
manifest.remove(file.name.clone())?;
} else {
let mut output = String::new();
for file in manifest.get_all() {
output.push_str(&(file.name + "\n"));
}
println!("{}", output);
}
Ok(())
}