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; extern crate lazy_static;
#[macro_use] #[macro_use]
extern crate structopt; extern crate structopt;
extern crate log;
#[macro_use] #[macro_use]
extern crate anyhow; extern crate anyhow;
#[macro_use] #[macro_use]

View file

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