From 371a77a994aeb0beae53f24a0edbf99d70133c33 Mon Sep 17 00:00:00 2001 From: fxqnlr Date: Wed, 14 Aug 2024 23:51:43 +0200 Subject: add basic arguments --- src/main.rs | 39 +++++++++++++++++++++++++++------------ 1 file changed, 27 insertions(+), 12 deletions(-) (limited to 'src/main.rs') 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 @@ -use std::path::Path; +use std::path::{Path, PathBuf}; -use cargo::{core::Workspace, ops::{clean, CleanOptions}, util::{context::GlobalContext, interning::InternedString}, CargoResult}; +use cargo::{ + core::Workspace, + ops::{clean, CleanOptions}, + util::{context::GlobalContext, interning::InternedString}, + CargoResult, +}; +use clap::Parser; + +use cli::Args; + +mod cli; fn main() { - let paths = std::fs::read_dir("./").unwrap(); + let cli = Args::parse(); + + let paths = std::fs::read_dir(cli.clone().dir.unwrap_or(PathBuf::from("./"))).unwrap(); for path in paths { let p = path.unwrap(); - handle_path(&p.path(), 0, 2); + handle_path(&p.path(), 0, cli.clone()); } } @@ -14,24 +26,26 @@ fn is_cargo_toml(path: &Path) -> bool { path.is_file() && (path.file_name().unwrap() == "Cargo.toml") } -fn handle_path(path: &Path, iter: u8, max_iter: u8) { +fn handle_path(path: &Path, iter: u8, cli: Args) { if is_cargo_toml(path) { let abs_path = std::fs::canonicalize(path).unwrap(); println!("Clean: {}", abs_path.as_path().to_str().unwrap()); - clean_project(abs_path.as_path()).unwrap(); + clean_project(abs_path.as_path(), cli).unwrap(); return; }; if path.is_dir() { - if iter > max_iter { return; }; + if iter >= cli.level { + return; + }; let paths = std::fs::read_dir(path).unwrap(); for path in paths { let p = path.unwrap(); - handle_path(&p.path(), iter + 1, 1); - }; + handle_path(&p.path(), iter + 1, cli.clone()); + } } } -fn clean_project(path: &Path) -> CargoResult<()> { +fn clean_project(path: &Path, cli: Args) -> CargoResult<()> { let gctx = GlobalContext::default()?; let workspace = Workspace::new(path, &gctx)?; @@ -42,9 +56,10 @@ fn clean_project(path: &Path) -> CargoResult<()> { targets: vec![], profile_specified: false, requested_profile: InternedString::new("dev"), - doc: false, - dry_run: true, + doc: cli.doc, + dry_run: cli.dry_run, }; + // return Ok(()); clean(&workspace, &opts) } -- cgit v1.2.3