jirust_cli/executors/
config_executor.rs

1use crate::config::config_file::ConfigFile;
2use crate::runners::cfg_cmd_runner::ConfigCmdRunner;
3use crate::{args::commands::ConfigActionValues, utils::PrintableData};
4
5use std::io::{Error, ErrorKind};
6
7/// ConfigExecutor struct
8///
9/// # Fields
10///
11/// * `config_cmd_runner: ConfigCmdRunner` - configuration command runner
12/// * `config_action: ConfigActionValues` - configuration action
13pub struct ConfigExecutor {
14    /// Configuration command runner
15    config_cmd_runner: ConfigCmdRunner,
16    /// Configuration action
17    config_action: ConfigActionValues,
18}
19
20/// ConfigExecutor implementation
21///
22/// # Methods
23///
24/// * `new(cfg_file: String, config_action: ConfigActionValues) -> Self` - returns a new ConfigExecutor instance
25/// * `exec_config_command(cfg_data: ConfigFile) -> Result<(), Box<dyn std::error::Error>>` - executes the configuration command
26impl ConfigExecutor {
27    /// Returns a new ConfigExecutor instance
28    ///
29    /// # Arguments
30    ///
31    /// * `cfg_file: String` - configuration file path
32    /// * `config_action: ConfigActionValues` - configuration action
33    ///
34    /// # Returns
35    ///
36    /// * `Self` - a new ConfigExecutor instance
37    ///
38    /// # Examples
39    ///
40    /// ```
41    /// use jirust_cli::executors::config_executor::ConfigExecutor;
42    /// use jirust_cli::args::commands::ConfigArgs;
43    /// use jirust_cli::args::commands::ConfigActionValues;
44    ///
45    /// let args = ConfigArgs {
46    ///    cfg_act: ConfigActionValues::Setup,
47    /// };
48    ///
49    /// let config_executor = ConfigExecutor::new("config_file_path".to_string(), args.cfg_act);
50    /// ```
51    pub fn new(cfg_file: String, config_action: ConfigActionValues) -> Self {
52        let config_cmd_runner = ConfigCmdRunner::new(cfg_file.clone());
53        Self {
54            config_cmd_runner,
55            config_action,
56        }
57    }
58
59    /// Executes the selected configuration command
60    ///
61    /// # Arguments
62    ///
63    /// * `cfg_data: ConfigFile` - configuration file data
64    ///
65    /// # Returns
66    ///
67    /// * `Result<(), Box<dyn std::error::Error>>` - Result with the execution status
68    ///
69    /// # Examples
70    ///
71    /// ```no_run
72    /// use jirust_cli::executors::config_executor::ConfigExecutor;
73    /// use jirust_cli::config::config_file::ConfigFile;
74    /// use jirust_cli::args::commands::ConfigArgs;
75    /// use jirust_cli::args::commands::ConfigActionValues;
76    /// # use std::error::Error;
77    ///
78    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
79    /// # tokio_test::block_on(async {
80    /// let args = ConfigArgs {
81    ///    cfg_act: ConfigActionValues::Setup,
82    /// };
83    /// let cfg_data = ConfigFile::default();
84    /// let config_executor = ConfigExecutor::new("config_file_path".to_string(), args.cfg_act);
85    ///
86    ///
87    /// config_executor.exec_config_command(cfg_data).await?;
88    /// # Ok(())
89    /// # })
90    /// # }
91    /// ```
92    pub async fn exec_config_command(
93        &self,
94        cfg_data: ConfigFile,
95    ) -> Result<Vec<PrintableData>, Box<dyn std::error::Error>> {
96        match self.config_action {
97            ConfigActionValues::Auth => match self.config_cmd_runner.set_cfg_auth(cfg_data) {
98                Ok(_) => Ok(vec![PrintableData::Generic {
99                    data: vec![serde_json::Value::String(
100                        "Authentication configuration stored successfully".to_string(),
101                    )],
102                }]),
103                Err(err) => Err(Box::new(Error::new(
104                    ErrorKind::Other,
105                    format!("Error storing authentication configuration: {}", err),
106                ))),
107            },
108            ConfigActionValues::Jira => match self.config_cmd_runner.set_cfg_jira(cfg_data) {
109                Ok(_) => Ok(vec![PrintableData::Generic {
110                    data: vec![serde_json::Value::String(
111                        "Initialization configuration stored successfully".to_string(),
112                    )],
113                }]),
114                Err(err) => Err(Box::new(Error::new(
115                    ErrorKind::Other,
116                    format!("Error storing initialization configuration: {}", err),
117                ))),
118            },
119            ConfigActionValues::Setup => match self.config_cmd_runner.setup_cfg(cfg_data) {
120                Ok(_) => Ok(vec![PrintableData::Generic {
121                    data: vec![serde_json::Value::String(
122                        "Configuration setup successfully".to_string(),
123                    )],
124                }]),
125                Err(err) => Err(Box::new(Error::new(
126                    ErrorKind::Other,
127                    format!("Error setting up configuration: {}", err),
128                ))),
129            },
130            ConfigActionValues::Show => {
131                self.config_cmd_runner.show_cfg(cfg_data);
132                Ok(vec![PrintableData::Generic {
133                    data: vec![serde_json::Value::String("DONE!".to_string())],
134                }])
135            }
136        }
137    }
138}