intelli_shell/process/
replace.rs

1use color_eyre::eyre::Result;
2use tokio_util::sync::CancellationToken;
3use tracing::instrument;
4
5use super::{InteractiveProcess, Process, ProcessOutput};
6use crate::{
7    cli::VariableReplaceProcess,
8    component::{Component, variable::VariableReplacementComponent},
9    config::Config,
10    format_error,
11    model::CommandTemplate,
12    service::IntelliShellService,
13};
14
15impl Process for VariableReplaceProcess {
16    async fn execute(
17        self,
18        config: Config,
19        service: IntelliShellService,
20        _cancellation_token: CancellationToken,
21    ) -> Result<ProcessOutput> {
22        match service.replace_command_variables(self.command.into_inner(), self.values, self.use_env) {
23            Ok(command) => Ok(ProcessOutput::success().stdout(&command).fileout(command)),
24            Err(missing) => Ok(ProcessOutput::fail().stderr(format_error!(
25                config.theme,
26                "Missing variable values: {}",
27                missing.join(", ")
28            ))),
29        }
30    }
31}
32
33impl InteractiveProcess for VariableReplaceProcess {
34    #[instrument(skip_all)]
35    fn into_component(
36        self,
37        config: Config,
38        service: IntelliShellService,
39        inline: bool,
40        cancellation_token: CancellationToken,
41    ) -> Result<Box<dyn Component>> {
42        let command = CommandTemplate::parse(self.command.into_inner(), true);
43        Ok(Box::new(VariableReplacementComponent::new(
44            service,
45            config.theme,
46            inline,
47            false,
48            true,
49            command,
50            cancellation_token,
51        )))
52    }
53}