intelli_shell/process/
export.rs1use color_eyre::eyre::Result;
2
3use super::{Process, ProcessOutput};
4use crate::{
5 cli::ExportCommandsProcess, config::Config, errors::ImportExportError, format_error, format_msg,
6 service::IntelliShellService,
7};
8
9impl Process for ExportCommandsProcess {
10 async fn execute(self, config: Config, service: IntelliShellService) -> Result<ProcessOutput> {
11 match service.export_commands(self, config.gist).await {
12 Ok(0) => Ok(ProcessOutput::fail().stderr(format_error!(config.theme, "No commands to export"))),
13 Ok(exported) => {
14 Ok(ProcessOutput::success().stderr(format_msg!(config.theme, "Exported {exported} commands")))
15 }
16 Err(ImportExportError::NotAFile) => Ok(ProcessOutput::fail().stderr(format_error!(
17 config.theme,
18 "The path already exists and it's not a file"
19 ))),
20 Err(ImportExportError::FileNotFound) => Ok(
21 ProcessOutput::fail().stderr(format_error!(config.theme, "The path of the file must already exist"))
22 ),
23 Err(ImportExportError::FileNotAccessible) => Ok(ProcessOutput::fail().stderr(format_error!(
24 config.theme,
25 "Cannot access the file, check write permissions"
26 ))),
27 Err(ImportExportError::FileBrokenPipe) => Ok(ProcessOutput::success()),
28 Err(ImportExportError::HttpInvalidUrl) => Ok(ProcessOutput::fail().stderr(format_error!(
29 config.theme,
30 "The provided URL is invalid, please provide a valid HTTP/S URL"
31 ))),
32 Err(ImportExportError::HttpRequestFailed(msg)) => {
33 Ok(ProcessOutput::fail().stderr(format_error!(config.theme, "HTTP request failed: {msg}")))
34 }
35 Err(ImportExportError::GistMissingId) => Ok(ProcessOutput::fail().stderr(format_error!(
36 config.theme,
37 "A gist id must be provided either on the arguments or the config file"
38 ))),
39 Err(ImportExportError::GistInvalidLocation) => Ok(ProcessOutput::fail().stderr(format_error!(
40 config.theme,
41 "The provided gist is not valid, please provide a valid id or URL"
42 ))),
43 Err(ImportExportError::GistLocationHasSha) => Ok(ProcessOutput::fail().stderr(format_error!(
44 config.theme,
45 "Cannot export to a gist revision, provide a gist without a revision"
46 ))),
47 Err(ImportExportError::GistFileNotFound) => unreachable!(),
48 Err(ImportExportError::GistMissingToken) => Ok(ProcessOutput::fail().stderr(format_error!(
49 config.theme,
50 "A GitHub token is required to export to a gist, set it in the config or GIST_TOKEN env variable"
51 ))),
52 Err(ImportExportError::GistRequestFailed(msg)) => {
53 Ok(ProcessOutput::fail().stderr(format_error!(config.theme, "Gist request failed: {msg}")))
54 }
55 Err(ImportExportError::Unexpected(report)) => Err(report),
56 }
57 }
58}