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 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
use base64::prelude::*;
use serde::{Deserialize, Serialize};
use std::{fs, io::Write};
use toml;
/// This struct holds the username and api_key for the Jira API.
#[derive(Debug)]
pub struct AuthData {
username: String,
api_key: String,
}
/// This struct holds the configuration data to use the Jira API (authentication info and Jira base_url).
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct ConfigFile {
auth: AuthSection,
jira: JiraSection,
}
/// This struct holds the authentication token to be used with the Jira API.
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct AuthSection {
auth_token: String,
}
/// This struct holds the Jira base_url.
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct JiraSection {
jira_url: String,
}
/// Implementation of AuthData
///
/// # Methods
/// * `new(username: String, api_key: String) -> AuthData` - creates a new instance of AuthData
/// * `set_username(username: String)` - sets the username
/// * `set_api_key(api_key: String)` - sets the api_key
/// * `get_username() -> String` - gets the username
/// * `get_api_key() -> String` - gets the api_key
/// * `to_base64() -> String` - converts the AuthData to a base64 string
/// * `from_base64(base64_str: String) -> AuthData` - converts a base64 string to an AuthData
/// * `write_to_file(file: &str) -> Result<(), std::io::Error>` - writes the AuthData to a file
/// * `read_from_file(file: &str) -> Result<AuthData, std::io::Error>` - reads the AuthData from a file
impl AuthData {
/// Create a new AuthData struct.
///
/// # Arguments
/// * username - The username to be used for authentication.
/// * api_key - The api_key to be used for authentication.
///
/// # Returns
/// * A new AuthData struct.
///
/// # Examples
///
/// ```
/// use jirust_cli::config::config_file::AuthData;
///
/// let auth_data = AuthData::new("username".to_string(), "api_key".to_string());
/// ```
pub fn new(username: String, api_key: String) -> AuthData {
AuthData { username, api_key }
}
/// Set the username for the AuthData struct.
///
/// # Arguments
/// * username - The username to be used for authentication.
///
/// # Examples
///
/// ```
/// use jirust_cli::config::config_file::AuthData;
///
/// let mut auth_data = AuthData::new("username".to_string(), "api_key".to_string());
/// auth_data.set_username("new_username".to_string());
/// ```
pub fn set_username(&mut self, username: String) {
self.username = username.replace("\n", "");
}
/// Set the api_key for the AuthData struct.
///
/// # Arguments
/// * api_key - The api_key to be used for authentication.
///
/// # Examples
///
/// ```
/// use jirust_cli::config::config_file::AuthData;
///
/// let mut auth_data = AuthData::new("username".to_string(), "api_key".to_string());
/// auth_data.set_api_key("new_api_key".to_string());
/// ```
pub fn set_api_key(&mut self, api_key: String) {
self.api_key = api_key.replace("\n", "");
}
/// Encode the username and api_key to base64 to be used in the Authorization header of the request.
///
/// # Returns
/// * A base64 encoded string of the username and api_key.
///
/// # Examples
///
/// ```
/// use jirust_cli::config::config_file::AuthData;
///
/// let auth_data = AuthData::new("username".to_string(), "api_key".to_string());
/// let base64_encoded = auth_data.to_base64();
///
/// assert_eq!(base64_encoded, "dXNlcm5hbWU6YXBpX2tleQ==");
/// ```
pub fn to_base64(&self) -> String {
BASE64_STANDARD.encode(format!("{}:{}", self.username, self.api_key).replace("\n", ""))
}
/// Decode a base64 encoded string to get the username and api_key.
///
/// # Arguments
/// * encoded - The base64 encoded string to be decoded.
///
/// # Returns
/// * A tuple containing the username and api_key.
///
/// # Examples
///
/// ```
/// use jirust_cli::config::config_file::AuthData;
///
/// let (username, api_key) = AuthData::from_base64("dXNlcm5hbWU6YXBpX2tleQ==");
///
/// assert_eq!(username, "username");
/// assert_eq!(api_key, "api_key");
/// ```
pub fn from_base64(encoded: &str) -> (String, String) {
let decoded = BASE64_STANDARD
.decode(encoded)
.expect("Failed to decode base64");
let decoded_str = String::from_utf8(decoded).expect("Failed to convert to string");
let parts: Vec<&str> = decoded_str.split(':').collect();
(parts[0].to_string(), parts[1].to_string())
}
}
/// Implementation of ConfigFile
///
/// # Methods
/// * `new(auth_token: String, jira_url: String) -> ConfigFile` - creates a new instance of ConfigFile
/// * default() -> ConfigFile - creates a new instance of ConfigFile with default values
/// * `write_to_file(file: &str) -> Result<(), std::io::Error>` - writes the ConfigFile to a file
/// * `read_from_file(file: &str) -> Result<ConfigFile, std::io::Error>` - reads the ConfigFile from a file
/// * `get_auth() -> AuthSection` - gets the AuthSection from the ConfigFile
/// * `get_jira() -> JiraSection` - gets the JiraSection from the ConfigFile
/// * `set_auth(auth: AuthSection)` - sets the AuthSection in the ConfigFile
/// * `set_jira(jira: JiraSection)` - sets the JiraSection in the ConfigFile
impl ConfigFile {
/// Create a new ConfigFile struct.
///
/// # Arguments
/// * auth_token - The authentication token to be used with the Jira API.
/// * jira_url - The base_url for the Jira API.
///
/// # Returns
/// * A new ConfigFile struct.
///
/// # Examples
///
/// ```
/// use jirust_cli::config::config_file::ConfigFile;
///
/// let config = ConfigFile::new("auth_token".to_string(), "jira_url".to_string());
///
/// assert_eq!(config.get_auth_key(), "auth_token");
/// assert_eq!(config.get_jira_url(), "jira_url");
/// ```
pub fn new(auth_token: String, jira_url: String) -> ConfigFile {
ConfigFile {
auth: AuthSection { auth_token },
jira: JiraSection { jira_url },
}
}
/// Create a new ConfigFile struct with default values.
/// This is useful for creating a new configuration file.
/// The default values can be set using the set methods.
/// The default values are:
/// - auth_token: ""
/// - jira_url: ""
///
/// # Returns
/// * A new ConfigFile struct with default values.
///
/// # Examples
///
/// ```
/// use jirust_cli::config::config_file::ConfigFile;
///
/// let config = ConfigFile::default();
///
/// assert_eq!(config.get_auth_key(), "");
/// assert_eq!(config.get_jira_url(), "");
/// ```
pub fn default() -> ConfigFile {
ConfigFile {
auth: AuthSection {
auth_token: String::from(""),
},
jira: JiraSection {
jira_url: String::from(""),
},
}
}
/// Set the authentication token for the ConfigFile struct.
/// This is the token that will be used to authenticate with the Jira API.
///
/// # Arguments
/// * auth_token - The authentication token to be used with the Jira API.
///
/// # Examples
///
/// ```
/// use jirust_cli::config::config_file::ConfigFile;
///
/// let mut config = ConfigFile::default();
/// config.set_auth_key("auth_key".to_string());
///
/// assert_eq!(config.get_auth_key(), "auth_key");
/// ```
pub fn set_auth_key(&mut self, auth_token: String) {
self.auth.auth_token = auth_token.replace("\n", "");
}
/// Get the authentication token for the ConfigFile struct.
/// This is the token that will be used to authenticate with the Jira API.
/// This is useful for getting the current value of the authentication token.
///
/// # Returns
/// * The authentication token to be used with the Jira API.
///
/// # Examples
///
/// ```
/// use jirust_cli::config::config_file::ConfigFile;
///
/// let config = ConfigFile::new("auth_key".to_string(), "jira_url".to_string());
/// let auth_key = config.get_auth_key();
///
/// assert_eq!(auth_key, "auth_key");
/// ```
pub fn get_auth_key(&self) -> &str {
&self.auth.auth_token
}
/// Set the Jira URL for the ConfigFile struct.
/// This is the base URL for the Jira API.
///
/// # Arguments
/// * jira_url - The base URL for the Jira API.
///
/// # Examples
///
/// ```
/// use jirust_cli::config::config_file::ConfigFile;
///
/// let mut config = ConfigFile::default();
/// config.set_jira_url("jira_url".to_string());
///
/// assert_eq!(config.get_jira_url(), "jira_url");
/// ```
pub fn set_jira_url(&mut self, jira_url: String) {
self.jira.jira_url = jira_url.replace("\n", "");
}
/// Get the Jira URL for the ConfigFile struct.
/// This is the base URL for the Jira API.
///
/// # Returns
/// * The base URL for the Jira API.
///
/// # Examples
///
/// ```
/// use jirust_cli::config::config_file::ConfigFile;
///
/// let config = ConfigFile::new("auth_key".to_string(), "jira_url".to_string());
/// let jira_url = config.get_jira_url();
///
/// assert_eq!(jira_url, "jira_url");
/// ```
pub fn get_jira_url(&self) -> &str {
&self.jira.jira_url
}
/// Stores the configuration to a file.
/// This will overwrite the file if it already exists.
///
/// # Arguments
/// * file_path - The path to the file to write the configuration to.
///
/// # Returns
/// * A Result containing either an empty Ok or an error.
///
/// # Examples
///
/// ```
/// use jirust_cli::config::config_file::ConfigFile;
///
/// let config = ConfigFile::new("auth_key".to_string(), "jira_url".to_string());
/// let result = config.write_to_file("config.toml");
///
/// assert!(result.is_ok());
/// ```
pub fn write_to_file(&self, file_path: &str) -> Result<(), std::io::Error> {
let mut file = std::fs::OpenOptions::new()
.write(true)
.create(true)
.truncate(true)
.open(file_path)
.expect("Failed to open file");
let toml_str = toml::to_string(self).expect("Failed to serialize toml");
file.write_all(toml_str.as_bytes())
}
/// Loads the configuration from a file.
/// If the file does not exist, it will return a ConfigFile with default values.
/// If the file is not valid toml, it will return an error.
/// If the file is valid toml, it will return the ConfigFile.
///
/// # Arguments
/// * file_path - The path to the file to read the configuration from.
///
/// # Returns
/// * A Result containing either the ConfigFile or an error.
///
/// # Examples
///
/// ```
/// use jirust_cli::config::config_file::ConfigFile;
///
/// let config = ConfigFile::read_from_file("config_example.toml");
///
/// assert!(config.clone().is_ok());
/// assert_eq!(config.clone().unwrap().get_auth_key(), "auth_key");
/// assert_eq!(config.clone().unwrap().get_jira_url(), "jira_url");
/// ```
pub fn read_from_file(file_path: &str) -> Result<ConfigFile, toml::de::Error> {
let config_file_str = fs::read_to_string(file_path)
.unwrap_or(toml::to_string(&ConfigFile::default()).unwrap_or("".to_string()));
toml::from_str(&config_file_str)
}
}