jirust_cli/runners/
cfg_cmd_runner.rs

1use crate::config::config_file::{AuthData, ConfigFile};
2
3use rpassword::read_password;
4use std::{fs, path::Path};
5
6/// ConfigCmdRunner is a struct that holds the configuration file path
7/// and provides methods to initialize, set, and show the configuration file.
8pub struct ConfigCmdRunner {
9    cfg_file: String,
10}
11
12/// Implementation of ConfigCmdRunner
13///
14/// # Methods
15///
16/// * `new(cfg_file: String) -> ConfigCmdRunner` - creates a new instance of ConfigCmdRunner
17/// * `init_file() -> Result<(), std::io::Error>` - initializes the configuration file
18/// * `set_cfg_auth(cfg: ConfigFile) -> Result<ConfigFile, std::io::Error>` - sets the authentication data in the configuration file
19/// * `set_cfg_jira(cfg: ConfigFile) -> Result<ConfigFile, std::io::Error>` - sets the Jira URL in the configuration file
20/// * `setup_cfg(cfg: ConfigFile) -> Result<(), std::io::Error>` - sets up the configuration file
21/// * `show_cfg(cfg: ConfigFile)` - shows the configuration file
22impl ConfigCmdRunner {
23    /// Creates a new instance of ConfigCmdRunner
24    ///
25    /// # Arguments
26    ///
27    /// * `cfg_file` - a String that holds the path to the configuration file
28    ///
29    /// # Returns
30    ///
31    /// * `ConfigCmdRunner` - a new instance of ConfigCmdRunner
32    ///
33    /// # Examples
34    ///
35    /// ```
36    /// use jirust_cli::runners::cfg_cmd_runner::ConfigCmdRunner;
37    ///
38    /// let cfg_runner = ConfigCmdRunner::new("test_path/to/config/file".to_string());
39    /// ```
40    pub fn new(cfg_file: String) -> ConfigCmdRunner {
41        ConfigCmdRunner { cfg_file }
42    }
43
44    /// Initializes the configuration file
45    ///
46    /// # Returns
47    ///
48    /// * `Result<(), std::io::Error>` - a Result that returns an empty tuple or an error
49    ///
50    /// # Examples
51    ///
52    /// ```
53    /// use jirust_cli::runners::cfg_cmd_runner::ConfigCmdRunner;
54    ///
55    /// let cfg_runner = ConfigCmdRunner::new("test_path/to/config/file".to_string());
56    /// cfg_runner.init_file();
57    /// ```
58    pub fn init_file(&self) -> Result<(), std::io::Error> {
59        let path = Path::new(&self.cfg_file);
60        fs::create_dir_all(path.parent().unwrap())?;
61        fs::File::create(path)?;
62        Ok(())
63    }
64
65    /// Sets the authentication data in the configuration file
66    ///
67    /// # Arguments
68    ///
69    /// * `cfg` - a ConfigFile that holds the configuration data
70    ///
71    /// # Returns
72    ///
73    /// * `Result<ConfigFile, std::io::Error>` - a Result that returns the updated ConfigFile or an error
74    ///
75    /// # Examples
76    ///
77    /// ```no_run
78    /// use jirust_cli::config::config_file::ConfigFile;
79    /// use jirust_cli::runners::cfg_cmd_runner::ConfigCmdRunner;
80    ///
81    /// let cfg_runner = ConfigCmdRunner::new("test_path/to/config/file".to_string());
82    /// let cfg = ConfigFile::default();
83    ///
84    /// cfg_runner.set_cfg_auth(cfg);
85    /// ```
86    pub fn set_cfg_auth(&self, mut cfg: ConfigFile) -> Result<ConfigFile, std::io::Error> {
87        let input = std::io::stdin();
88        let mut user = String::new();
89        println!("Your username: ");
90        input.read_line(&mut user)?;
91        println!("Your apikey: ");
92        let apikey = read_password()?;
93        let config_data = AuthData::new(user, apikey);
94        cfg.set_auth_key(config_data.to_base64());
95        cfg.write_to_file(self.cfg_file.as_str())?;
96        Ok(cfg)
97    }
98
99    /// Sets the Jira URL in the configuration file
100    ///
101    /// # Arguments
102    ///
103    /// * `cfg` - a ConfigFile that holds the configuration data
104    ///
105    /// # Returns
106    ///
107    /// * `Result<ConfigFile, std::io::Error>` - a Result that returns the updated ConfigFile or an error
108    ///
109    /// # Examples
110    ///
111    /// ```no_run
112    /// use jirust_cli::config::config_file::ConfigFile;
113    /// use jirust_cli::runners::cfg_cmd_runner::ConfigCmdRunner;
114    ///
115    /// let cfg_runner = ConfigCmdRunner::new("test_path/to/config/file".to_string());
116    /// let cfg = ConfigFile::default();
117    /// cfg_runner.set_cfg_jira(cfg);
118    /// ```
119    pub fn set_cfg_jira(&self, mut cfg: ConfigFile) -> Result<ConfigFile, std::io::Error> {
120        let input = std::io::stdin();
121        let mut read_data = String::new();
122        println!("Your Jira instance URL: ");
123        input.read_line(&mut read_data)?;
124        cfg.set_jira_url(read_data.clone());
125        read_data.clear();
126        println!("Default Jira issue resolution JSON Value: ");
127        input.read_line(&mut read_data)?;
128        cfg.set_standard_resolution(read_data.clone());
129        read_data.clear();
130        println!("Default Jira issue resolution comment JSON: ");
131        input.read_line(&mut read_data)?;
132        cfg.set_standard_resolution_comment(read_data);
133        cfg.write_to_file(self.cfg_file.as_str())?;
134        Ok(cfg)
135    }
136
137    /// Sets up the configuration file
138    ///
139    /// # Arguments
140    ///
141    /// * `cfg` - a ConfigFile that holds the configuration data
142    ///
143    /// # Returns
144    ///
145    /// * `Result<(), std::io::Error>` - a Result that returns an empty tuple or an error
146    ///
147    /// # Examples
148    ///
149    /// ```no_run
150    /// use jirust_cli::config::config_file::ConfigFile;
151    /// use jirust_cli::runners::cfg_cmd_runner::ConfigCmdRunner;
152    ///
153    /// let cfg_runner = ConfigCmdRunner::new("test_path/to/config/file".to_string());
154    /// let cfg = ConfigFile::default();
155    /// cfg_runner.setup_cfg(cfg);
156    /// ```
157    pub fn setup_cfg(&self, mut cfg: ConfigFile) -> Result<(), std::io::Error> {
158        self.init_file()?;
159        cfg = self.set_cfg_jira(cfg)?;
160        self.set_cfg_auth(cfg)?;
161        Ok(())
162    }
163
164    /// Shows the configuration file data
165    ///
166    /// # Arguments
167    ///
168    /// * `cfg` - a ConfigFile that holds the configuration data
169    ///
170    /// # Examples
171    ///
172    /// ```
173    /// use jirust_cli::config::config_file::ConfigFile;
174    /// use jirust_cli::runners::cfg_cmd_runner::ConfigCmdRunner;
175    ///
176    /// let cfg = ConfigFile::default();
177    ///
178    /// let cfg_runner = ConfigCmdRunner::new("test_path/to/config/file".to_string());
179    /// cfg_runner.show_cfg(cfg);
180    /// ```
181    pub fn show_cfg(&self, cfg: ConfigFile) {
182        println!("Auth token: {}", cfg.get_auth_key());
183        println!("Jira URL: {}", cfg.get_jira_url());
184        println!(
185            "Jira default resolution: {:?}",
186            cfg.get_standard_resolution()
187        );
188        println!(
189            "Jira default resolution comment: {:?}",
190            cfg.get_standard_resolution_comment()
191        );
192    }
193}