intelli_shell/process/
export.rs

1use color_eyre::Result;
2
3use super::{Process, ProcessOutput};
4use crate::{
5    cli::ExportCommandsProcess,
6    component::{
7        Component,
8        pick::{CommandsPickerComponent, CommandsPickerComponentMode},
9    },
10    config::Config,
11    errors::AppError,
12    format_error, format_msg,
13    process::InteractiveProcess,
14    service::IntelliShellService,
15};
16
17impl Process for ExportCommandsProcess {
18    async fn execute(self, config: Config, service: IntelliShellService) -> color_eyre::Result<ProcessOutput> {
19        let commands = match service.prepare_commands_export(self.filter.clone()).await {
20            Ok(s) => s,
21            Err(AppError::UserFacing(err)) => {
22                return Ok(ProcessOutput::fail().stderr(format_error!(config.theme, "{err}")));
23            }
24            Err(AppError::Unexpected(report)) => return Err(report),
25        };
26        match service.export_commands(commands, self, config.gist).await {
27            Ok((0, _)) => Ok(ProcessOutput::fail().stderr(format_error!(config.theme, "No commands to export"))),
28            Ok((exported, None)) => {
29                Ok(ProcessOutput::success().stderr(format_msg!(config.theme, "Exported {exported} commands")))
30            }
31            Ok((exported, Some(stdout))) => Ok(ProcessOutput::success()
32                .stdout(stdout)
33                .stderr(format_msg!(config.theme, "Exported {exported} commands"))),
34            Err(AppError::UserFacing(err)) => Ok(ProcessOutput::fail().stderr(format_error!(config.theme, "{err}"))),
35            Err(AppError::Unexpected(report)) => Err(report),
36        }
37    }
38}
39impl InteractiveProcess for ExportCommandsProcess {
40    fn into_component(self, config: Config, service: IntelliShellService, inline: bool) -> Result<Box<dyn Component>> {
41        Ok(Box::new(CommandsPickerComponent::new(
42            service,
43            config,
44            inline,
45            CommandsPickerComponentMode::Export { input: self },
46        )))
47    }
48}