Skip to main content

gammaloop_api/commands/
remove.rs

1use clap::Subcommand;
2use schemars::JsonSchema;
3use serde::{Deserialize, Serialize};
4use tracing::info;
5
6use crate::{
7    completion::CompletionArgExt,
8    state::{ProcessRef, State},
9};
10use color_eyre::Result;
11use colored::Colorize;
12use eyre::eyre;
13#[derive(Subcommand, Debug, Serialize, Deserialize, Clone, JsonSchema, PartialEq)]
14pub enum Remove {
15    /// Remove one integrand, one process, or all processes selected by the supplied target.
16    Processes {
17        /// Process reference: `#<id>`, `name:<name>`, or `<id>/<name>`
18        #[arg(
19            short = 'p',
20            long = "process",
21            value_name = "PROCESS",
22            completion_process_selector(crate::completion::SelectorKind::Any)
23        )]
24        process: Option<ProcessRef>,
25
26        /// Restrict the removal to a single integrand within the selected process
27        #[arg(
28            short = 'i',
29            long = "integrand-name",
30            value_name = "NAME",
31            completion_integrand_selector(crate::completion::SelectorKind::Any)
32        )]
33        integrand_name: Option<String>,
34    },
35}
36
37impl Remove {
38    pub fn run(&self, state: &mut State) -> Result<()> {
39        match self {
40            Self::Processes {
41                process,
42                integrand_name,
43            } => {
44                if process.is_none() && integrand_name.is_some() {
45                    return Err(eyre!(
46                        "{}",
47                        "--integrand-name requires --process for `remove processes`"
48                    ));
49                }
50
51                let removed = state
52                    .remove_selected_integrands(process.as_ref(), integrand_name.as_deref())?;
53                for removed_integrand in removed {
54                    info!(
55                        "{} {} {} {}",
56                        "Removed integrand".blue(),
57                        removed_integrand.integrand_name.green(),
58                        "from process".blue(),
59                        removed_integrand.process_name.green()
60                    );
61                }
62            }
63        }
64        Ok(())
65    }
66}