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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
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
    ///
    /// ```
    /// use jirust_cli::runners::cfg_cmd_runner::ConfigCmdRunner;
    ///
    /// let cfg_runner = ConfigCmdRunner::new("test_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
    ///
    /// ```
    /// use jirust_cli::runners::cfg_cmd_runner::ConfigCmdRunner;
    ///
    /// let cfg_runner = ConfigCmdRunner::new("test_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
    ///
    /// ```no_run
    /// use jirust_cli::config::config_file::ConfigFile;
    /// use jirust_cli::runners::cfg_cmd_runner::ConfigCmdRunner;
    ///
    /// let cfg_runner = ConfigCmdRunner::new("test_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
    ///
    /// ```no_run
    /// use jirust_cli::config::config_file::ConfigFile;
    /// use jirust_cli::runners::cfg_cmd_runner::ConfigCmdRunner;
    ///
    /// let cfg_runner = ConfigCmdRunner::new("test_path/to/config/file".to_string());
    /// let cfg = ConfigFile::default();
    /// 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 read_data = String::new();
        println!("Your Jira instance URL: ");
        input.read_line(&mut read_data)?;
        cfg.set_jira_url(read_data.clone());
        read_data.clear();
        println!("Default Jira issue resolution JSON Value: ");
        input.read_line(&mut read_data)?;
        cfg.set_standard_resolution(read_data.clone());
        read_data.clear();
        println!("Default Jira issue resolution comment JSON: ");
        input.read_line(&mut read_data)?;
        cfg.set_standard_resolution_comment(read_data);
        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
    ///
    /// ```no_run
    /// use jirust_cli::config::config_file::ConfigFile;
    /// use jirust_cli::runners::cfg_cmd_runner::ConfigCmdRunner;
    ///
    /// let cfg_runner = ConfigCmdRunner::new("test_path/to/config/file".to_string());
    /// let cfg = ConfigFile::default();
    /// 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
    ///
    /// ```
    /// use jirust_cli::config::config_file::ConfigFile;
    /// use jirust_cli::runners::cfg_cmd_runner::ConfigCmdRunner;
    ///
    /// let cfg = ConfigFile::default();
    ///
    /// let cfg_runner = ConfigCmdRunner::new("test_path/to/config/file".to_string());
    /// 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());
        println!(
            "Jira default resolution: {:?}",
            cfg.get_standard_resolution()
        );
        println!(
            "Jira default resolution comment: {:?}",
            cfg.get_standard_resolution_comment()
        );
    }
}