Refactored to use the dirs crate to get the config files easily

Signed-off-by: Louis Vallat <louis@louis-vallat.xyz>
This commit is contained in:
Louis Vallat 2021-10-26 11:20:07 +02:00
parent 16f52a330b
commit 41b78b3bdb
No known key found for this signature in database
GPG Key ID: 0C87282F76E61283

View File

@ -1,4 +1,4 @@
use std::{fs::{File, create_dir, create_dir_all}, io::{Read, Write}}; use std::{fs::{File, create_dir}, io::{Read, Write}};
use hyper_tls::HttpsConnector; use hyper_tls::HttpsConnector;
use hyper::{Body, Client, body, client::HttpConnector}; use hyper::{Body, Client, body, client::HttpConnector};
@ -20,10 +20,9 @@ pub async fn body_to_str(res: Body) -> String {
} }
fn get_config_folder() -> String { fn get_config_folder() -> String {
let xdg = std::env::var("XDG_CONFIG_HOME"); let mut p = dirs::config_dir().unwrap();
let mut xdg_val = if xdg.is_ok() { xdg.clone().ok().unwrap() } else { "~/.config".to_string() }; p.push("Lychee_CLIent");
if xdg_val.ends_with("/") { xdg_val.remove(xdg_val.len() - 1); } return p.to_str().unwrap().to_string();
return xdg_val + "/Lychee_CLIent";
} }
fn write_to_file(d: String, p: String) { fn write_to_file(d: String, p: String) {
@ -40,7 +39,7 @@ fn read_from_file(p: String) -> String {
#[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>> {
create_dir_all(get_config_folder()).expect("Cannot create folder."); create_dir(get_config_folder()).expect("Cannot create folder.");
let matches = App::new("Lychee CLIent") let matches = App::new("Lychee CLIent")
.version("0.1.0") .version("0.1.0")
.author("Louis Vallat <contact@louis-vallat.xyz>") .author("Louis Vallat <contact@louis-vallat.xyz>")
@ -84,7 +83,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, get_config_folder}; use crate::body_to_str;
#[test] #[test]
fn body_to_str_empty() { fn body_to_str_empty() {
@ -96,26 +95,4 @@ mod tests_main {
assert_eq!("demo", tokio_test::block_on(body_to_str(Body::from("demo")))); assert_eq!("demo", tokio_test::block_on(body_to_str(Body::from("demo"))));
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());
}
} }