intelli_shell/process/
import.rs

1use color_eyre::eyre::Result;
2
3use super::{Process, ProcessOutput};
4use crate::{
5    cli::ImportCommandsProcess, config::Config, errors::ImportExportError, format_error, format_msg,
6    service::IntelliShellService,
7};
8
9impl Process for ImportCommandsProcess {
10    async fn execute(self, config: Config, service: IntelliShellService) -> Result<ProcessOutput> {
11        let dry_run = self.dry_run;
12        match service.import_commands(self, config.gist).await {
13            Ok((0, 0)) => Ok(ProcessOutput::fail().stderr(format_error!(config.theme, "No commands were found"))),
14            Ok((0, skipped)) => {
15                if dry_run {
16                    Ok(ProcessOutput::success())
17                } else {
18                    Ok(ProcessOutput::success().stderr(format_msg!(
19                        config.theme,
20                        "No commands imported, {skipped} already existed"
21                    )))
22                }
23            }
24            Ok((imported, 0)) => {
25                if dry_run {
26                    Ok(ProcessOutput::success())
27                } else {
28                    Ok(ProcessOutput::success().stderr(format_msg!(config.theme, "Imported {imported} new commands")))
29                }
30            }
31            Ok((imported, skipped)) => {
32                if dry_run {
33                    Ok(ProcessOutput::success())
34                } else {
35                    Ok(ProcessOutput::success().stderr(format_msg!(
36                        config.theme,
37                        "Imported {imported} new commands {}",
38                        config.theme.secondary.apply(format!("({skipped} already existed)"))
39                    )))
40                }
41            }
42            Err(ImportExportError::NotAFile) => Ok(ProcessOutput::fail().stderr(format_error!(
43                config.theme,
44                "Symlinks and directories are not supported, provide a file instead"
45            ))),
46            Err(ImportExportError::FileNotFound) => {
47                Ok(ProcessOutput::fail().stderr(format_error!(config.theme, "File not found")))
48            }
49            Err(ImportExportError::FileNotAccessible) => Ok(ProcessOutput::fail().stderr(format_error!(
50                config.theme,
51                "Cannot access the file, check read permissions"
52            ))),
53            Err(ImportExportError::FileBrokenPipe) => unreachable!(),
54            Err(ImportExportError::HttpInvalidUrl) => Ok(ProcessOutput::fail().stderr(format_error!(
55                config.theme,
56                "The provided URL is invalid, please provide a valid HTTP/S URL"
57            ))),
58            Err(ImportExportError::HttpRequestFailed(msg)) => {
59                Ok(ProcessOutput::fail().stderr(format_error!(config.theme, "HTTP request failed: {msg}")))
60            }
61            Err(ImportExportError::GistMissingId) => Ok(ProcessOutput::fail().stderr(format_error!(
62                config.theme,
63                "A gist id must be provided either on the arguments or the config file"
64            ))),
65            Err(ImportExportError::GistInvalidLocation) => Ok(ProcessOutput::fail().stderr(format_error!(
66                config.theme,
67                "The provided gist is not valid, please provide a valid id or URL"
68            ))),
69            Err(ImportExportError::GistFileNotFound) => {
70                Ok(ProcessOutput::fail().stderr(format_error!(config.theme, "The provided gist file was not found")))
71            }
72            Err(ImportExportError::GistLocationHasSha) => unreachable!(),
73            Err(ImportExportError::GistMissingToken) => unreachable!(),
74            Err(ImportExportError::GistRequestFailed(msg)) => {
75                Ok(ProcessOutput::fail().stderr(format_error!(config.theme, "Gist request failed: {msg}")))
76            }
77            Err(ImportExportError::Unexpected(report)) => Err(report),
78        }
79    }
80}