Add the --all option to restore.

This commit is contained in:
Michael Zhang 2020-10-31 20:55:43 -05:00
parent 8379405bb6
commit 2deb42f613
Signed by: michael
GPG key ID: BDA47A31A3C8EE6B
2 changed files with 19 additions and 6 deletions

View file

@ -6,7 +6,6 @@
extern crate lazy_static;
#[macro_use]
extern crate structopt;
extern crate log;
#[macro_use]
extern crate anyhow;
#[macro_use]

View file

@ -1,8 +1,10 @@
use std::env;
use std::fs;
use std::io;
use std::path::PathBuf;
use anyhow::Result;
use chrono_humanize::HumanTime;
use crate::utils;
use crate::TrashDir;
@ -14,11 +16,16 @@ pub struct RestoreOptions {
/// By default, this is your home directory's trash ($XDG_DATA_HOME/Trash)
#[structopt(long = "trash-dir", parse(from_os_str))]
trash_dir: Option<PathBuf>,
/// List all files in the trash (by default, only files in the current
/// directory are listed)
#[structopt(short = "a", long = "all")]
all: bool,
}
/// Restore files from a trash directory
pub fn restore(options: RestoreOptions) -> Result<()> {
let trash_dir = TrashDir::from_opt(options.trash_dir);
let trash_dir = TrashDir::from_opt(options.trash_dir.as_ref());
if trash_dir.check_info_dir()?.is_none() {
bail!("There's no trash directory here.");
@ -26,18 +33,24 @@ pub fn restore(options: RestoreOptions) -> Result<()> {
// get list of files sorted by deletion date
// TODO: possible to get this to be streaming?
let current_dir = env::current_dir()?;
let files = {
let mut files = trash_dir
.iter()
.unwrap()
.iter()?
.filter_map(|entry| match entry {
Ok(info) => Some(info),
Ok(info) => {
if !options.all && !info.path.starts_with(&current_dir) {
return None;
}
Some(info)
}
Err(err) => {
eprintln!("failed to get file info: {:?}", err);
None
}
})
.collect::<Vec<_>>();
files.sort_unstable_by_key(|info| info.deletion_date);
files
};
@ -48,9 +61,10 @@ pub fn restore(options: RestoreOptions) -> Result<()> {
for (i, info) in files.iter().enumerate() {
println!(
"[{}]\t{}\t{}",
"[{}]\t{}\t{}\t{}",
i,
info.deletion_date,
HumanTime::from(info.deletion_date),
utils::percent_encode(&info.path)
);
}