summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/cli.rs18
-rw-r--r--src/main.rs39
2 files changed, 45 insertions, 12 deletions
diff --git a/src/cli.rs b/src/cli.rs
new file mode 100644
index 0000000..6b1a043
--- /dev/null
+++ b/src/cli.rs
@@ -0,0 +1,18 @@
1use std::path::PathBuf;
2
3use clap::Parser;
4
5#[derive(Parser, Clone)]
6#[command(author, version, about, long_about = None)]
7pub struct Args {
8 pub dir: Option<PathBuf>,
9
10 #[arg(short, long, default_value_t = 2)]
11 pub level: u8,
12
13 #[arg(short, long)]
14 pub doc: bool,
15
16 #[arg(long, default_value_t = true)]
17 pub dry_run: bool,
18}
diff --git a/src/main.rs b/src/main.rs
index cdc7d03..beac641 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,12 +1,24 @@
1use std::path::Path; 1use std::path::{Path, PathBuf};
2 2
3use cargo::{core::Workspace, ops::{clean, CleanOptions}, util::{context::GlobalContext, interning::InternedString}, CargoResult}; 3use cargo::{
4 core::Workspace,
5 ops::{clean, CleanOptions},
6 util::{context::GlobalContext, interning::InternedString},
7 CargoResult,
8};
9use clap::Parser;
10
11use cli::Args;
12
13mod cli;
4 14
5fn main() { 15fn main() {
6 let paths = std::fs::read_dir("./").unwrap(); 16 let cli = Args::parse();
17
18 let paths = std::fs::read_dir(cli.clone().dir.unwrap_or(PathBuf::from("./"))).unwrap();
7 for path in paths { 19 for path in paths {
8 let p = path.unwrap(); 20 let p = path.unwrap();
9 handle_path(&p.path(), 0, 2); 21 handle_path(&p.path(), 0, cli.clone());
10 } 22 }
11} 23}
12 24
@@ -14,24 +26,26 @@ fn is_cargo_toml(path: &Path) -> bool {
14 path.is_file() && (path.file_name().unwrap() == "Cargo.toml") 26 path.is_file() && (path.file_name().unwrap() == "Cargo.toml")
15} 27}
16 28
17fn handle_path(path: &Path, iter: u8, max_iter: u8) { 29fn handle_path(path: &Path, iter: u8, cli: Args) {
18 if is_cargo_toml(path) { 30 if is_cargo_toml(path) {
19 let abs_path = std::fs::canonicalize(path).unwrap(); 31 let abs_path = std::fs::canonicalize(path).unwrap();
20 println!("Clean: {}", abs_path.as_path().to_str().unwrap()); 32 println!("Clean: {}", abs_path.as_path().to_str().unwrap());
21 clean_project(abs_path.as_path()).unwrap(); 33 clean_project(abs_path.as_path(), cli).unwrap();
22 return; 34 return;
23 }; 35 };
24 if path.is_dir() { 36 if path.is_dir() {
25 if iter > max_iter { return; }; 37 if iter >= cli.level {
38 return;
39 };
26 let paths = std::fs::read_dir(path).unwrap(); 40 let paths = std::fs::read_dir(path).unwrap();
27 for path in paths { 41 for path in paths {
28 let p = path.unwrap(); 42 let p = path.unwrap();
29 handle_path(&p.path(), iter + 1, 1); 43 handle_path(&p.path(), iter + 1, cli.clone());
30 }; 44 }
31 } 45 }
32} 46}
33 47
34fn clean_project(path: &Path) -> CargoResult<()> { 48fn clean_project(path: &Path, cli: Args) -> CargoResult<()> {
35 let gctx = GlobalContext::default()?; 49 let gctx = GlobalContext::default()?;
36 50
37 let workspace = Workspace::new(path, &gctx)?; 51 let workspace = Workspace::new(path, &gctx)?;
@@ -42,9 +56,10 @@ fn clean_project(path: &Path) -> CargoResult<()> {
42 targets: vec![], 56 targets: vec![],
43 profile_specified: false, 57 profile_specified: false,
44 requested_profile: InternedString::new("dev"), 58 requested_profile: InternedString::new("dev"),
45 doc: false, 59 doc: cli.doc,
46 dry_run: true, 60 dry_run: cli.dry_run,
47 }; 61 };
48 62
63 // return Ok(());
49 clean(&workspace, &opts) 64 clean(&workspace, &opts)
50} 65}