Implemented a function to give the configuration folder based on XDG_CONFIG_HOME and by default ~/.config/ suffixed with the program name
Signed-off-by: Louis Vallat <louis@louis-vallat.xyz>
This commit is contained in:
parent
8520b1e8a3
commit
c37beb32c2
35
src/main.rs
35
src/main.rs
@ -10,13 +10,20 @@ mod albums;
|
|||||||
pub struct LycheeClient {
|
pub struct LycheeClient {
|
||||||
client: Client<HttpsConnector<HttpConnector>>,
|
client: Client<HttpsConnector<HttpConnector>>,
|
||||||
endpoint: String,
|
endpoint: String,
|
||||||
api_key: String
|
api_key: String,
|
||||||
|
config_path: String
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn body_to_str(res: Body) -> String {
|
pub async fn body_to_str(res: Body) -> String {
|
||||||
return String::from_utf8(body::to_bytes(res).await.unwrap().to_vec()).unwrap();
|
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]
|
#[tokio::main]
|
||||||
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
||||||
@ -47,7 +54,8 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
|||||||
let client = LycheeClient {
|
let client = LycheeClient {
|
||||||
client: Client::builder().build::<_, hyper::Body>(HttpsConnector::new()),
|
client: Client::builder().build::<_, hyper::Body>(HttpsConnector::new()),
|
||||||
endpoint: std::env::var("LYCHEE_ENDPOINT").unwrap(),
|
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<dyn std::error::Error + Send + Sync>> {
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests_main {
|
mod tests_main {
|
||||||
use hyper::Body;
|
use hyper::Body;
|
||||||
use crate::body_to_str;
|
use crate::{body_to_str, get_config_folder};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn body_to_str_empty() {
|
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"))));
|
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());
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -46,7 +46,8 @@ mod session_tests {
|
|||||||
return LycheeClient {
|
return LycheeClient {
|
||||||
client: Client::builder().build::<_, hyper::Body>(https),
|
client: Client::builder().build::<_, hyper::Body>(https),
|
||||||
endpoint: mockito::server_url(),
|
endpoint: mockito::server_url(),
|
||||||
api_key: "value".to_string()
|
api_key: "value".to_string(),
|
||||||
|
config_path: "".to_string()
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user