This commit is contained in:
Michael Zhang 2019-12-26 19:33:13 -06:00
parent 8ddfd41fa6
commit 544877aed8
No known key found for this signature in database
GPG key ID: 5BAEFE5D04F0CE6C
4 changed files with 25 additions and 16 deletions

2
Cargo.lock generated
View file

@ -88,7 +88,7 @@ dependencies = [
[[package]] [[package]]
name = "garbage" name = "garbage"
version = "0.1.3" version = "0.1.4"
dependencies = [ dependencies = [
"anyhow 1.0.25 (registry+https://github.com/rust-lang/crates.io-index)", "anyhow 1.0.25 (registry+https://github.com/rust-lang/crates.io-index)",
"chrono 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)", "chrono 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)",

View file

@ -1,6 +1,6 @@
[package] [package]
name = "garbage" name = "garbage"
version = "0.1.3" version = "0.1.4"
authors = ["Michael Zhang <iptq@protonmail.com>"] authors = ["Michael Zhang <iptq@protonmail.com>"]
description = "cli tool for interacting with the freedesktop trashcan" description = "cli tool for interacting with the freedesktop trashcan"
license = "MIT" license = "MIT"

View file

@ -5,7 +5,7 @@ use std::fs;
use std::io; use std::io;
use std::path::PathBuf; use std::path::PathBuf;
use anyhow::Error; use anyhow::Result;
use structopt::StructOpt; use structopt::StructOpt;
use garbage::*; use garbage::*;
@ -46,15 +46,12 @@ enum Command {
Restore, Restore,
} }
fn main() -> Result<(), Error> { fn run() -> Result<()> {
env_logger::init(); env_logger::init();
let cmd = Command::from_args(); let cmd = Command::from_args();
match cmd { match cmd {
Command::Empty { dry, days } => match ops::empty(dry, days) { Command::Empty { dry, days } => ops::empty(dry, days),
Ok(_) => (),
Err(err) => eprintln!("error: {:?}", err),
},
Command::List => { Command::List => {
let home_trash = TrashDir::get_home_trash(); let home_trash = TrashDir::get_home_trash();
let mut files = home_trash let mut files = home_trash
@ -72,12 +69,13 @@ fn main() -> Result<(), Error> {
for info in files { for info in files {
println!("{}\t{}", info.deletion_date, info.path.to_str().unwrap()); println!("{}\t{}", info.deletion_date, info.path.to_str().unwrap());
} }
Ok(())
} }
Command::Put { Command::Put {
paths, recursive, force paths,
} => { recursive,
ops::put(paths, recursive); force,
} } => ops::put(paths, recursive),
Command::Restore => { Command::Restore => {
let home_trash = TrashDir::get_home_trash(); let home_trash = TrashDir::get_home_trash();
let mut files = home_trash let mut files = home_trash
@ -114,8 +112,19 @@ fn main() -> Result<(), Error> {
} }
_ => println!("Invalid number."), _ => println!("Invalid number."),
} }
Ok(())
}
} }
} }
Ok(()) fn main() {
match run() {
Ok(_) => (),
Err(err) => {
eprintln!("Error: {:?}", err);
for cause in err.chain() {
eprintln!("- {:?}", cause);
}
}
}
} }

View file

@ -1,12 +1,12 @@
use std::fs; use std::fs;
use anyhow::Error; use anyhow::Result;
use chrono::{Duration, Local}; use chrono::{Duration, Local};
use crate::TrashDir; use crate::TrashDir;
use crate::TrashInfo; use crate::TrashInfo;
pub fn empty(dry: bool, days: Option<u32>) -> Result<(), Error> { pub fn empty(dry: bool, days: Option<u32>) -> Result<()> {
let home_trash = TrashDir::get_home_trash(); let home_trash = TrashDir::get_home_trash();
let cutoff = if let Some(days) = days { let cutoff = if let Some(days) = days {
Local::now() - Duration::days(days.into()) Local::now() - Duration::days(days.into())