jirust_cli/executors/config_executor.rs
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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
use crate::args::commands::ConfigActionValues;
use crate::config::config_file::ConfigFile;
use crate::runners::cfg_cmd_runner::ConfigCmdRunner;
/// ConfigExecutor struct
///
/// # Fields
///
/// * `config_cmd_runner: ConfigCmdRunner` - configuration command runner
/// * `config_action: ConfigActionValues` - configuration action
pub struct ConfigExecutor {
/// Configuration command runner
config_cmd_runner: ConfigCmdRunner,
/// Configuration action
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;
/// use jirust_cli::args::commands::ConfigActionValues;
///
/// 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
///
/// ```no_run
/// use jirust_cli::executors::config_executor::ConfigExecutor;
/// use jirust_cli::config::config_file::ConfigFile;
/// use jirust_cli::args::commands::ConfigArgs;
/// use jirust_cli::args::commands::ConfigActionValues;
/// # use std::error::Error;
///
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// # tokio_test::block_on(async {
/// let args = ConfigArgs {
/// cfg_act: ConfigActionValues::Setup,
/// };
/// let cfg_data = ConfigFile::default();
/// let config_executor = ConfigExecutor::new("config_file_path".to_string(), args.cfg_act);
///
///
/// config_executor.exec_config_command(cfg_data).await?;
/// # Ok(())
/// # })
/// # }
/// ```
pub async fn exec_config_command(
&self,
cfg_data: ConfigFile,
) -> Result<(), Box<dyn std::error::Error>> {
match self.config_action {
ConfigActionValues::Auth => {
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 => {
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 => {
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(())
}
}