workspacer_cli/
cleanup_crate.rs

1// ---------------- [ File: workspacer-cli/src/cleanup_crate.rs ]
2crate::ix!();
3
4/// For **Crate** subcommand usage: `ws cleanup crate --crate MY_CRATE [--workspace ...] [--skip-git-check]`
5#[derive(Debug, StructOpt, Getters, Setters)]
6#[getset(get="pub")]
7pub struct CleanupCrateCommand {
8    /// The name of the crate to clean up
9    #[structopt(long = "crate")]
10    crate_name: String,
11
12    /// If provided, we use this path as the workspace root instead of the current directory
13    #[structopt(long = "workspace")]
14    workspace_path: Option<PathBuf>,
15
16    /// If true, we skip the Git clean check
17    #[structopt(long = "skip-git-check")]
18    skip_git_check: bool,
19}
20
21impl CleanupCrateCommand {
22    pub async fn run(&self) -> Result<(), WorkspaceError> {
23        let crate_name_owned = self.crate_name().clone();
24
25        // 1) Load the workspace, optionally ensure Git is clean,
26        //    find the crate, and pass it to a closure that calls `cleanup_crate()`.
27        run_with_workspace_and_crate_name(
28            self.workspace_path().clone(),
29            *self.skip_git_check(),
30            crate_name_owned,
31            |ws, name| {
32                Box::pin(async move {
33                    // a) find the crate by name
34                    let arc_crate = ws.find_crate_by_name(name).await.ok_or_else(|| {
35                        error!("No crate named '{}' found in workspace", name);
36                        CrateError::CrateNotFoundInWorkspace {
37                            crate_name: name.to_owned(),
38                        }
39                    })?;
40
41                    // b) lock the crate handle
42                    let handle = arc_crate.lock().await.clone();
43
44                    // c) call `handle.cleanup_crate().await`
45                    handle.cleanup_crate().await?;
46
47                    info!("Crate '{}' cleaned up (removed target/, Cargo.lock if present).", name);
48                    Ok(())
49                })
50            },
51        )
52        .await
53    }
54}