Skip to content

Commit

Permalink
Restrict directory trashing/untrashing to happen on recurse only.
Browse files Browse the repository at this point in the history
  • Loading branch information
Dylan Sinnott committed Jul 25, 2023
1 parent df4a175 commit 6d3897a
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 6 deletions.
24 changes: 23 additions & 1 deletion src/files/trash.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::common::hub_helper;
use crate::common::{drive_file, hub_helper};
use crate::files::info;
use crate::hub::Hub;
use std::error;
Expand All @@ -7,6 +7,7 @@ use std::fmt::Formatter;

pub struct Config {
pub file_id: String,
pub trash_directories: bool,
}

pub async fn trash(config: Config) -> Result<(), Error> {
Expand All @@ -16,6 +17,8 @@ pub async fn trash(config: Config) -> Result<(), Error> {
.await
.map_err(Error::GetFile)?;

err_if_directory(&exists, &config)?;

if exists.trashed.is_some_and(|trashed| trashed == true) {
println!("File is already trashed, exiting");
return Ok(());
Expand Down Expand Up @@ -55,6 +58,7 @@ pub enum Error {
Hub(hub_helper::Error),
GetFile(google_drive3::Error),
Update(google_drive3::Error),
IsDirectory(String),
}

impl error::Error for Error {}
Expand All @@ -65,6 +69,11 @@ impl Display for Error {
Error::Hub(err) => write!(f, "{}", err),
Error::GetFile(err) => write!(f, "Failed to get file: {}", err),
Error::Update(err) => write!(f, "Failed to trash file: {}", err),
Error::IsDirectory(f) => write!(
f,
"'{}' is a directory, use --recursive to trash directories",
name
),
}
}
}
Expand Down Expand Up @@ -101,3 +110,16 @@ impl PatchFile {
self.file.clone()
}
}

fn err_if_directory(file: &google_drive3::api::File, config: &Config) -> Result<(), Error> {
if drive_file::is_directory(file) && !config.trash_directories {
let name = file
.name
.as_ref()
.map(|s| s.to_string())
.unwrap_or_default();
Err(Error::IsDirectory(name))
} else {
Ok(())
}
}
24 changes: 23 additions & 1 deletion src/files/untrash.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::common::hub_helper;
use crate::common::{drive_file, hub_helper};
use crate::files::info;
use crate::hub::Hub;
use std::error;
Expand All @@ -7,6 +7,7 @@ use std::fmt::Formatter;

pub struct Config {
pub file_id: String,
pub untrash_directories: bool,
}

pub async fn untrash(config: Config) -> Result<(), Error> {
Expand All @@ -16,6 +17,8 @@ pub async fn untrash(config: Config) -> Result<(), Error> {
.await
.map_err(Error::GetFile)?;

err_if_directory(&exists, &config)?;

if exists.trashed.is_some_and(|trashed| trashed == false) {
println!("File is not trashed, exiting");
return Ok(());
Expand Down Expand Up @@ -55,6 +58,7 @@ pub enum Error {
Hub(hub_helper::Error),
GetFile(google_drive3::Error),
Update(google_drive3::Error),
IsDirectory(String),
}

impl error::Error for Error {}
Expand All @@ -65,6 +69,11 @@ impl Display for Error {
Error::Hub(err) => write!(f, "{}", err),
Error::GetFile(err) => write!(f, "Failed to get file: {}", err),
Error::Update(err) => write!(f, "Failed to update file: {}", err),
Error::IsDirectory(f) => write!(
f,
"'{}' is a directory, use --recursive to trash directories",
name
),
}
}
}
Expand Down Expand Up @@ -101,3 +110,16 @@ impl PatchFile {
self.file.clone()
}
}

fn err_if_directory(file: &google_drive3::api::File, config: &Config) -> Result<(), Error> {
if drive_file::is_directory(file) && !config.trash_directories {
let name = file
.name
.as_ref()
.map(|s| s.to_string())
.unwrap_or_default();
Err(Error::IsDirectory(name))
} else {
Ok(())
}
}
22 changes: 18 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,12 +257,20 @@ enum FileCommand {
Trash {
/// File id
file_id: String,

/// Trash directory and all it's content
#[arg(long)]
recursive: bool,
},

/// Untrash file
Untrash {
/// File id
file_id: String,

/// Untrash directory and all it's content
#[arg(long)]
recursive: bool,
},

/// Create directory
Expand Down Expand Up @@ -597,16 +605,22 @@ async fn main() {
.await
.unwrap_or_else(handle_error)
}
FileCommand::Trash { file_id } => {
FileCommand::Trash { file_id, recursive } => {
// fmt
files::trash(files::trash::Config { file_id })
files::trash(files::trash::Config {
file_id,
trash_directories: recursive,
})
.await
.unwrap_or_else(handle_error)
}

FileCommand::Untrash { file_id } => {
FileCommand::Untrash { file_id, recursive } => {
// fmt
files::untrash(files::untrash::Config { file_id })
files::untrash(files::untrash::Config {
file_id,
untrash_directories: recursive
})
.await
.unwrap_or_else(handle_error)
}
Expand Down

0 comments on commit 6d3897a

Please sign in to comment.