1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
use crate::args::commands::ConfigActionValues;
use crate::config::config_file::ConfigFile;
use crate::runners::cfg_cmd_runner::ConfigCmdRunner;

/// ConfigExecutor struct
pub struct ConfigExecutor {
    config_cmd_runner: ConfigCmdRunner,
    config_action: ConfigActionValues,
}

/// ConfigExecutor implementation
///
/// # Methods
///
/// * `new(cfg_file: String, config_action: ConfigActionValues) -> Self` - returns a new ConfigExecutor instance
/// * `exec_config_command(cfg_data: ConfigFile) -> Result<(), Box<dyn std::error::Error>>` - executes the configuration command
impl ConfigExecutor {
    /// Returns a new ConfigExecutor instance
    ///
    /// # Arguments
    ///
    /// * `cfg_file: String` - configuration file path
    /// * `config_action: ConfigActionValues` - configuration action
    ///
    /// # Returns
    ///
    /// * `Self` - a new ConfigExecutor instance
    ///
    /// # Examples
    ///
    /// ```
    /// use jirust_cli::executors::config_executor::ConfigExecutor;
    /// use jirust_cli::args::commands::ConfigArgs;
    ///
    /// let args = ConfigArgs {
    ///    cfg_act: ConfigActionValues::Setup,
    /// };
    ///
    /// let config_executor = ConfigExecutor::new("config_file_path".to_string(), args.cfg_act);
    /// ```
    pub fn new(cfg_file: String, config_action: ConfigActionValues) -> Self {
        let config_cmd_runner = ConfigCmdRunner::new(cfg_file.clone());
        Self {
            config_cmd_runner,
            config_action,
        }
    }

    /// Executes the selected configuration command
    ///
    /// # Arguments
    ///
    /// * `cfg_data: ConfigFile` - configuration file data
    ///
    /// # Returns
    ///
    /// * `Result<(), Box<dyn std::error::Error>>` - Result with the execution status
    ///
    /// # Examples
    ///
    /// ```
    /// use jirust_cli::executors::config_executor::ConfigExecutor;
    ///
    /// # tokio_test::block_on(async {
    /// let config_executor = ConfigExecutor::new(config_file_path, args.cfg_act);
    /// config_executor.exec_config_command(cfg_data).await?;
    /// # })
    /// ```
    pub async fn exec_config_command(
        &self,
        cfg_data: ConfigFile,
    ) -> Result<(), Box<dyn std::error::Error>> {
        match self.config_action {
            ConfigActionValues::Auth => {
                let _cfg_res = match self.config_cmd_runner.set_cfg_auth(cfg_data) {
                    Ok(_) => println!("Authentication configuration stored successfully"),
                    Err(err) => {
                        eprintln!("Error storing authentication configuration: {}", err);
                    }
                };
            }
            ConfigActionValues::Jira => {
                let _cfg_res = match self.config_cmd_runner.set_cfg_jira(cfg_data) {
                    Ok(_) => println!("Initialization configuration stored successfully"),
                    Err(err) => {
                        eprintln!("Error storing initialization configuration: {}", err);
                    }
                };
            }
            ConfigActionValues::Setup => {
                let _cfg_res = match self.config_cmd_runner.setup_cfg(cfg_data) {
                    Ok(_) => println!("Configuration setup successfully"),
                    Err(err) => {
                        eprintln!("Error setting up configuration: {}", err);
                    }
                };
            }
            ConfigActionValues::Show => {
                self.config_cmd_runner.show_cfg(cfg_data);
            }
        }
        Ok(())
    }
}