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