codeberg_cli/actions/label/
delete.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
use forgejo_api::structs::IssueListLabelsQuery;

use crate::actions::label::display_label;

use crate::actions::GeneralArgs;
use crate::render::spinner::spin_until_ready;
use crate::render::ui::multi_fuzzy_select_with_key;

use crate::actions::text_manipulation::select_prompt_for;
use crate::types::context::BergContext;
use crate::types::git::OwnerRepo;

use clap::Parser;

/// Delete a label
#[derive(Parser, Debug)]
pub struct DeleteLabelArgs {}

impl DeleteLabelArgs {
    pub async fn run(self, general_args: GeneralArgs) -> anyhow::Result<()> {
        let _ = general_args;
        let ctx = BergContext::new(self).await?;

        let OwnerRepo { repo, owner } = ctx.owner_repo()?;
        let labels_list = spin_until_ready(ctx.client.issue_list_labels(
            owner.as_str(),
            repo.as_str(),
            IssueListLabelsQuery::default(),
        ))
        .await?;

        let selected_labels = multi_fuzzy_select_with_key(
            &labels_list,
            select_prompt_for("label"),
            |_| false,
            display_label,
        )?;

        for id in selected_labels.into_iter().filter_map(|label| label.id) {
            ctx.client
                .issue_delete_label(owner.as_str(), repo.as_str(), id as u64)
                .await?;
        }

        Ok(())
    }
}