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/cli.rs | 18 ++++++++++++++++++ src/main.rs | 39 +++++++++++++++++++++++++++------------ 2 files changed, 45 insertions(+), 12 deletions(-) create mode 100644 src/cli.rs (limited to 'src') 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 @@ +use std::path::PathBuf; + +use clap::Parser; + +#[derive(Parser, Clone)] +#[command(author, version, about, long_about = None)] +pub struct Args { + pub dir: Option, + + #[arg(short, long, default_value_t = 2)] + pub level: u8, + + #[arg(short, long)] + pub doc: bool, + + #[arg(long, default_value_t = true)] + pub dry_run: bool, +} 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