diff --git a/src/main.rs b/src/main.rs index 34cfb36..9ed1316 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,13 +10,20 @@ mod albums; pub struct LycheeClient { client: Client>, endpoint: String, - api_key: String + api_key: String, + config_path: String } pub async fn body_to_str(res: Body) -> String { return String::from_utf8(body::to_bytes(res).await.unwrap().to_vec()).unwrap(); } +fn get_config_folder() -> String { + let xdg = std::env::var("XDG_CONFIG_HOME"); + let mut xdg_val = if xdg.is_ok() { xdg.clone().ok().unwrap() } else { "~/.config".to_string() }; + if xdg_val.ends_with("/") { xdg_val.remove(xdg_val.len() - 1); } + return xdg_val + "/Lychee_CLIent"; +} #[tokio::main] async fn main() -> Result<(), Box> { @@ -47,7 +54,8 @@ async fn main() -> Result<(), Box> { let client = LycheeClient { client: Client::builder().build::<_, hyper::Body>(HttpsConnector::new()), endpoint: std::env::var("LYCHEE_ENDPOINT").unwrap(), - api_key: std::env::var("LYCHEE_API_KEY").unwrap() + api_key: std::env::var("LYCHEE_API_KEY").unwrap(), + config_path: get_config_folder() }; @@ -63,7 +71,7 @@ async fn main() -> Result<(), Box> { #[cfg(test)] mod tests_main { use hyper::Body; - use crate::body_to_str; + use crate::{body_to_str, get_config_folder}; #[test] fn body_to_str_empty() { @@ -76,4 +84,25 @@ mod tests_main { assert_eq!("hello world", tokio_test::block_on(body_to_str(Body::from("hello world")))); } + #[test] + fn get_config_folder_from_env() { + std::env::set_var("XDG_CONFIG_HOME", "~"); + assert_eq!("~/Lychee_CLIent", get_config_folder()); + + std::env::set_var("XDG_CONFIG_HOME", "~/.conf"); + assert_eq!("~/.conf/Lychee_CLIent", get_config_folder()); + } + + #[test] + fn get_config_folder_trailing_slash() { + std::env::set_var("XDG_CONFIG_HOME", "~/"); + assert_eq!("~/Lychee_CLIent", get_config_folder()); + } + + #[test] + fn get_config_folder_no_env() { + std::env::remove_var("XDG_CONFIG_HOME"); + assert_eq!("~/.config/Lychee_CLIent", get_config_folder()); + } + } diff --git a/src/session.rs b/src/session.rs index 72c6194..a7e3f45 100644 --- a/src/session.rs +++ b/src/session.rs @@ -46,7 +46,8 @@ mod session_tests { return LycheeClient { client: Client::builder().build::<_, hyper::Body>(https), endpoint: mockito::server_url(), - api_key: "value".to_string() + api_key: "value".to_string(), + config_path: "".to_string() }; }