1crate::ix!();
3
4#[derive(Debug, StructOpt)]
8pub enum PruneSubcommand {
9 #[structopt(name = "crate")]
11 Crate(PruneCrateCommand),
12
13 #[structopt(name = "workspace")]
15 Workspace(PruneWorkspaceCommand),
16}
17
18impl PruneSubcommand {
19 pub async fn run(&self) -> Result<(), WorkspaceError> {
21 match self {
22 PruneSubcommand::Crate(cmd) => cmd.run().await,
23 PruneSubcommand::Workspace(cmd) => cmd.run().await,
24 }
25 }
26}
27
28#[derive(Debug, StructOpt, Getters, Setters)]
30#[getset(get="pub")]
31pub struct PruneCrateCommand {
32 #[structopt(long = "crate")]
34 crate_name: String,
35
36 #[structopt(long = "workspace")]
38 workspace_path: Option<PathBuf>,
39
40 #[structopt(long = "skip-git-check")]
42 skip_git_check: bool,
43}
44
45impl PruneCrateCommand {
46 pub async fn run(&self) -> Result<(), WorkspaceError> {
47 let crate_name_owned = self.crate_name().clone();
48
49 run_with_workspace_and_crate_name(
52 self.workspace_path().clone(),
53 *self.skip_git_check(),
54 crate_name_owned,
55 |ws, crate_name_str| {
56 Box::pin(async move {
57 let arc_crate = ws.find_crate_by_name(crate_name_str).await
59 .ok_or_else(|| {
60 error!("No crate named '{}' found in workspace", crate_name_str);
61 CrateError::CrateNotFoundInWorkspace {
62 crate_name: crate_name_str.to_string(),
63 }
64 })?;
65 let mut guard = arc_crate.lock().await;
67 let removed = guard.prune_invalid_category_slugs().await?;
69
70 info!("Prune done for crate='{}': removed {} invalid category/keyword entries.", crate_name_str, removed);
71 println!("Removed {} invalid category or keyword entries from crate='{}'.", removed, crate_name_str);
72 Ok(())
73 })
74 },
75 )
76 .await
77 }
78}
79
80#[derive(Debug, StructOpt, Getters, Setters)]
82#[getset(get="pub")]
83pub struct PruneWorkspaceCommand {
84 #[structopt(long = "path")]
86 workspace_path: Option<PathBuf>,
87
88 #[structopt(long = "skip-git-check")]
90 skip_git_check: bool,
91}
92
93impl PruneWorkspaceCommand {
94 pub async fn run(&self) -> Result<(), WorkspaceError> {
95 let ws_path = self.workspace_path.clone();
98 let skip_flag = self.skip_git_check;
99
100 run_with_workspace(ws_path, skip_flag, move |ws| {
101 Box::pin(async move {
102 let removed_total = ws.prune_invalid_category_slugs_from_members().await?;
104 info!("Successfully pruned categories in the entire workspace, total removed={}", removed_total);
105 println!("Removed {} invalid category/keyword entries across the entire workspace.", removed_total);
106 Ok(())
107 })
108 })
109 .await
110 }
111}