Skip to content

Commit

Permalink
Add argument to allow a particular path to be excluded. Implements #11
Browse files Browse the repository at this point in the history
  • Loading branch information
mihai-dinculescu committed Jan 11, 2021
1 parent 0b12692 commit efbba64
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 59 deletions.
5 changes: 4 additions & 1 deletion src/command.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{fmt, io, str};
use std::{fmt, io, path, str};
use structopt::StructOpt;

#[derive(Debug, StructOpt)]
Expand All @@ -16,6 +16,9 @@ pub struct Args {
/// Caution! If set it will wipe all folders found! Unset by default
#[structopt(short, long)]
pub wipe: bool,
/// Paths to ignore
#[structopt(short, long, parse(from_os_str))]
pub ignores: Vec<path::PathBuf>,
}

#[derive(Debug, PartialEq, Clone, StructOpt)]
Expand Down
10 changes: 6 additions & 4 deletions src/tests/wipe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ fn run_with_hits(folder_name: FolderNameEnum, wipe: bool) {
let test_path = TestPath::new(3, &folder_name);

let params = WipeParams {
folder_name: folder_name.clone(),
path: PathBuf::from(&test_path),
wipe,
path: PathBuf::from(&test_path),
folder_name: folder_name.clone(),
ignores: Vec::new(),
};

let mut buff = Cursor::new(Vec::new());
Expand Down Expand Up @@ -94,9 +95,10 @@ fn run_no_hits(folder_name: FolderNameEnum, wipe: bool) {
let test_path = TestPath::new(0, &folder_name);

let params = WipeParams {
folder_name,
path: PathBuf::from(&test_path),
wipe,
path: PathBuf::from(&test_path),
folder_name: folder_name.clone(),
ignores: Vec::new(),
};

let mut buff = Cursor::new(Vec::new());
Expand Down
26 changes: 14 additions & 12 deletions src/tests/wipe_params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ use crate::wipe::WipeParams;

#[parameterized(
args = {
Args { folder_name: FolderNameEnum::Node, wipe: false },
Args { folder_name: FolderNameEnum::Node, wipe: true },
Args { folder_name: FolderNameEnum::NodeModules, wipe: false },
Args { folder_name: FolderNameEnum::NodeModules, wipe: true },
Args { wipe: false, folder_name: FolderNameEnum::Node, ignores: Vec::new(), },
Args { wipe: true, folder_name: FolderNameEnum::Node, ignores: Vec::new(), },
Args { wipe: false, folder_name: FolderNameEnum::NodeModules, ignores: Vec::new(), },
Args { wipe: true, folder_name: FolderNameEnum::NodeModules, ignores: Vec::new(), },
},
)]
fn node(args: Args) {
Expand All @@ -17,19 +17,20 @@ fn node(args: Args) {
assert_eq!(
params,
WipeParams {
folder_name: FolderNameEnum::NodeModules,
path: std::env::current_dir().unwrap(),
wipe: args.wipe,
path: std::env::current_dir().unwrap(),
folder_name: FolderNameEnum::NodeModules,
ignores: args.ignores,
}
);
}

#[parameterized(
args = {
Args { folder_name: FolderNameEnum::Rust, wipe: false },
Args { folder_name: FolderNameEnum::Rust, wipe: true },
Args { folder_name: FolderNameEnum::Target, wipe: false },
Args { folder_name: FolderNameEnum::Target, wipe: true },
Args { wipe: false, folder_name: FolderNameEnum::Rust, ignores: Vec::new(), },
Args { wipe: true, folder_name: FolderNameEnum::Rust, ignores: Vec::new(), },
Args { wipe: false, folder_name: FolderNameEnum::Target, ignores: Vec::new(), },
Args { wipe: true, folder_name: FolderNameEnum::Target, ignores: Vec::new(), },
},
)]
fn rust(args: Args) {
Expand All @@ -38,9 +39,10 @@ fn rust(args: Args) {
assert_eq!(
params,
WipeParams {
folder_name: FolderNameEnum::Target,
path: std::env::current_dir().unwrap(),
wipe: args.wipe,
path: std::env::current_dir().unwrap(),
folder_name: FolderNameEnum::Target,
ignores: args.ignores,
}
);
}
10 changes: 6 additions & 4 deletions src/tests/wipe_permissions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ mod wipe_permissions_tests {
let test_path = TestPath::new(3, &folder_name);

let params = WipeParams {
folder_name,
path: PathBuf::from(&test_path),
wipe,
path: PathBuf::from(&test_path),
folder_name,
ignores: Vec::new(),
};

let first_hit = test_path.hits.first().unwrap().clone();
Expand Down Expand Up @@ -68,9 +69,10 @@ mod wipe_permissions_tests {
let test_path = TestPath::new(3, &folder_name);

let params = WipeParams {
folder_name,
path: PathBuf::from(&test_path),
wipe,
path: PathBuf::from(&test_path),
folder_name,
ignores: Vec::new(),
};

let first_hit = test_path.hits.first().unwrap().clone();
Expand Down
108 changes: 70 additions & 38 deletions src/wipe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,21 @@ pub struct WipeParams {
pub wipe: bool,
pub path: PathBuf,
pub folder_name: FolderNameEnum,
pub ignores: Vec<PathBuf>,
}

impl WipeParams {
pub fn new(args: &Args) -> io::Result<Self> {
let path = env::current_dir()?;

Ok(Self {
wipe: args.wipe,
path,
folder_name: match args.folder_name {
FolderNameEnum::Node | FolderNameEnum::NodeModules => FolderNameEnum::NodeModules,
FolderNameEnum::Rust | FolderNameEnum::Target => FolderNameEnum::Target,
},
path,
wipe: args.wipe,
ignores: args.ignores.clone(),
})
}
}
Expand All @@ -42,6 +44,7 @@ where
params: &'a WipeParams,
previous_info: Option<DirInfo>,
wipe_info: Option<DirInfo>,
ignore_info: Option<DirInfo>,
}

impl<'a, W> Wipe<'a, W>
Expand All @@ -54,6 +57,7 @@ where
params,
previous_info: None,
wipe_info: None,
ignore_info: None,
}
}

Expand Down Expand Up @@ -107,13 +111,22 @@ where
self.previous_info = Some(dir_size(&self.params.path)?);
}

let dir_count = &paths_to_delete.len();
let mut file_count = 0_usize;
let mut size = 0_usize;
let mut wipe_info = DirInfo::new(paths_to_delete.len(), 0, 0);
let mut ignore_info = DirInfo::new(0, 0, 0);
let paths_ignored = self
.params
.ignores
.iter()
.map(|p| p.display().to_string().to_lowercase())
.collect::<Vec<_>>();

for path in paths_to_delete {
let dir_info = dir_size(&path);

let ignored = paths_ignored
.iter()
.any(|p| path.to_lowercase().starts_with(p));

if let Ok(dir_info) = dir_info {
self.write_spaced_line(
dir_info.file_count_formatted(),
Expand All @@ -122,17 +135,25 @@ where
&path,
)?;

file_count += dir_info.file_count;
size += dir_info.size;
if ignored {
ignore_info.dir_count += 1;
ignore_info.file_count += dir_info.file_count;
ignore_info.size += dir_info.size;
} else {
wipe_info.file_count += dir_info.file_count;
wipe_info.size += dir_info.size;
}
} else {
self.write_spaced_line("?", "?", "", &path)?;
}

if self.params.wipe {
if ignored {
write!(self.stdout, " {}", Paint::yellow("[Ignored]"))?;
} else if self.params.wipe {
let r = fs::remove_dir_all(path);

if let Err(e) = r {
write!(self.stdout, " {}", Paint::red(e))?;
write!(self.stdout, " {}", Paint::red(&format!("[{}]", e)))?;
}
}

Expand All @@ -141,41 +162,16 @@ where
self.stdout.flush()?;
}

self.wipe_info = Some(DirInfo::new(*dir_count, file_count, size));

Ok(())
}

fn write_footer(&mut self) -> io::Result<()> {
let wipe_info = self.wipe_info.as_ref().expect("this should never be None");

writeln!(self.stdout)?;

if wipe_info.dir_count > 0 {
self.write_summary()?;

if !self.params.wipe {
writeln!(
self.stdout,
"Run {} to wipe all folders found. {}",
Paint::red(format!("cargo wipe {} -w", self.params.folder_name)),
Paint::red("USE WITH CAUTION!")
)?;
} else {
writeln!(self.stdout, "{}", Paint::green("All clear!"))?
}
} else {
writeln!(self.stdout, "{}", Paint::green("Nothing found!"))?
}

self.stdout.flush()?;
self.wipe_info = Some(wipe_info);
self.ignore_info = Some(ignore_info);

Ok(())
}

fn write_summary(&mut self) -> io::Result<()> {
let wipe_info = self.wipe_info.expect("this should never be None");
let previous_info = self.previous_info.expect("this should never be None");
let wipe_info = self.wipe_info.expect("this should never be None");
let ignore_info = self.ignore_info.expect("this should never be None");

let after = DirInfo {
dir_count: previous_info.dir_count - wipe_info.dir_count,
Expand Down Expand Up @@ -203,6 +199,15 @@ where
Paint::default(label),
)?;

if ignore_info.dir_count > 0 {
self.writeln_spaced_line(
Paint::yellow(ignore_info.file_count_formatted()),
Paint::yellow(ignore_info.size_formatted_flex()),
"",
Paint::yellow("Ignored"),
)?;
}

let label = if self.params.wipe {
"Wiped"
} else {
Expand Down Expand Up @@ -236,6 +241,33 @@ where
Ok(())
}

fn write_footer(&mut self) -> io::Result<()> {
let wipe_info = self.wipe_info.as_ref().expect("this should never be None");

writeln!(self.stdout)?;

if wipe_info.dir_count > 0 {
self.write_summary()?;

if !self.params.wipe {
writeln!(
self.stdout,
"Run {} to wipe all folders found. {}",
Paint::red(format!("cargo wipe {} -w", self.params.folder_name)),
Paint::red("USE WITH CAUTION!")
)?;
} else {
writeln!(self.stdout, "{}", Paint::green("All clear!"))?
}
} else {
writeln!(self.stdout, "{}", Paint::green("Nothing found!"))?
}

self.stdout.flush()?;

Ok(())
}

fn write_spaced_line(
&mut self,
column_1: impl Display,
Expand Down

0 comments on commit efbba64

Please sign in to comment.