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 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
use crate::config::config_file::{AuthData, ConfigFile};
use rpassword::read_password;
use std::{fs, path::Path};
/// ConfigCmdRunner is a struct that holds the configuration file path
/// and provides methods to initialize, set, and show the configuration file.
pub struct ConfigCmdRunner {
cfg_file: String,
}
/// Implementation of ConfigCmdRunner
///
/// # Methods
///
/// * `new(cfg_file: String) -> ConfigCmdRunner` - creates a new instance of ConfigCmdRunner
/// * `init_file() -> Result<(), std::io::Error>` - initializes the configuration file
/// * `set_cfg_auth(cfg: ConfigFile) -> Result<ConfigFile, std::io::Error>` - sets the authentication data in the configuration file
/// * `set_cfg_jira(cfg: ConfigFile) -> Result<ConfigFile, std::io::Error>` - sets the Jira URL in the configuration file
/// * `setup_cfg(cfg: ConfigFile) -> Result<(), std::io::Error>` - sets up the configuration file
/// * `show_cfg(cfg: ConfigFile)` - shows the configuration file
impl ConfigCmdRunner {
/// Creates a new instance of ConfigCmdRunner
///
/// # Arguments
///
/// * `cfg_file` - a String that holds the path to the configuration file
///
/// # Returns
///
/// * `ConfigCmdRunner` - a new instance of ConfigCmdRunner
///
/// # Examples
///
/// ```
/// let cfg_runner = ConfigCmdRunner::new("path/to/config/file".to_string());
/// ```
pub fn new(cfg_file: String) -> ConfigCmdRunner {
ConfigCmdRunner { cfg_file }
}
/// Initializes the configuration file
///
/// # Returns
///
/// * `Result<(), std::io::Error>` - a Result that returns an empty tuple or an error
///
/// # Examples
///
/// ```
/// let cfg_runner = ConfigCmdRunner::new("path/to/config/file".to_string());
/// cfg_runner.init_file();
/// ```
pub fn init_file(&self) -> Result<(), std::io::Error> {
let path = Path::new(&self.cfg_file);
fs::create_dir_all(path.parent().unwrap())?;
fs::File::create(path)?;
Ok(())
}
/// Sets the authentication data in the configuration file
///
/// # Arguments
///
/// * `cfg` - a ConfigFile that holds the configuration data
///
/// # Returns
///
/// * `Result<ConfigFile, std::io::Error>` - a Result that returns the updated ConfigFile or an error
///
/// # Examples
///
/// ```
/// use jirust_cli::config::config_file::ConfigFile;
/// use jirust_cli::runners::cfg_cmd_runner::ConfigCmdRunner;
///
/// let cfg_runner = ConfigCmdRunner::new("path/to/config/file".to_string());
/// let cfg = ConfigFile::default();
///
/// cfg_runner.set_cfg_auth(cfg);
/// ```
pub fn set_cfg_auth(&self, mut cfg: ConfigFile) -> Result<ConfigFile, std::io::Error> {
let input = std::io::stdin();
let mut user = String::new();
println!("Your username: ");
input.read_line(&mut user)?;
println!("Your apikey: ");
let apikey = read_password()?;
let config_data = AuthData::new(user, apikey);
cfg.set_auth_key(config_data.to_base64());
cfg.write_to_file(&self.cfg_file.as_str())?;
Ok(cfg)
}
/// Sets the Jira URL in the configuration file
///
/// # Arguments
///
/// * `cfg` - a ConfigFile that holds the configuration data
///
/// # Returns
///
/// * `Result<ConfigFile, std::io::Error>` - a Result that returns the updated ConfigFile or an error
///
/// # Examples
///
/// ```
/// let cfg_runner = ConfigCmdRunner::new("path/to/config/file".to_string());
/// let cfg = ConfigFile::new();
/// cfg_runner.set_cfg_jira(cfg);
/// ```
pub fn set_cfg_jira(&self, mut cfg: ConfigFile) -> Result<ConfigFile, std::io::Error> {
let input = std::io::stdin();
let mut url = String::new();
println!("Your Jira instance URL: ");
input.read_line(&mut url)?;
cfg.set_jira_url(url);
cfg.write_to_file(&self.cfg_file.as_str())?;
Ok(cfg)
}
/// Sets up the configuration file
///
/// # Arguments
///
/// * `cfg` - a ConfigFile that holds the configuration data
///
/// # Returns
///
/// * `Result<(), std::io::Error>` - a Result that returns an empty tuple or an error
///
/// # Examples
///
/// ```
/// let cfg_runner = ConfigCmdRunner::new("path/to/config/file".to_string());
/// let cfg = ConfigFile::new();
/// cfg_runner.setup_cfg(cfg);
/// ```
pub fn setup_cfg(&self, mut cfg: ConfigFile) -> Result<(), std::io::Error> {
self.init_file()?;
cfg = self.set_cfg_jira(cfg)?;
self.set_cfg_auth(cfg)?;
Ok(())
}
/// Shows the configuration file data
///
/// # Arguments
///
/// * `cfg` - a ConfigFile that holds the configuration data
///
/// # Examples
///
/// ```
/// let cfg_runner = ConfigCmdRunner::new("path/to/config/file".to_string());
/// let cfg = ConfigFile::new();
/// cfg_runner.show_cfg(cfg);
/// ```
pub fn show_cfg(&self, cfg: ConfigFile) {
println!("Auth token: {}", cfg.get_auth_key());
println!("Jira URL: {}", cfg.get_jira_url());
}
}