From 919d0ef9eba2e801aaa86916a0fb5345ca4015eb Mon Sep 17 00:00:00 2001 From: Adrien Cacciaguerra Date: Fri, 31 May 2024 18:02:26 -0400 Subject: [PATCH] refactor(config): rename config::Config into config::CodSpeedConfig and make it sync --- src/auth.rs | 8 ++++---- src/config.rs | 26 +++++++++++++------------- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/auth.rs b/src/auth.rs index 02b7efe..bf51727 100644 --- a/src/auth.rs +++ b/src/auth.rs @@ -1,7 +1,7 @@ use std::time::Duration; use crate::logger::get_local_logger; -use crate::{api_client::CodSpeedAPIClient, config::Config, prelude::*}; +use crate::{api_client::CodSpeedAPIClient, config::CodSpeedConfig, prelude::*}; use clap::{Args, Subcommand}; use simplelog::CombinedLogger; use tokio::time::{sleep, Instant}; @@ -67,9 +67,9 @@ async fn login(api_client: &CodSpeedAPIClient) -> Result<()> { } debug!("Login completed"); - let mut config = Config::load().await?; - config.auth.token = token; - config.persist().await?; + let mut config = CodSpeedConfig::load()?; + config.auth.token = Some(token); + config.persist()?; debug!("Token saved to configuration file"); info!("Login successful, your are now authenticated on CodSpeed"); diff --git a/src/config.rs b/src/config.rs index 7cff667..3fc6435 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,4 +1,4 @@ -use std::{env, path::PathBuf}; +use std::{env, fs, path::PathBuf}; use crate::prelude::*; use nestify::nest; @@ -7,9 +7,9 @@ use serde::{Deserialize, Serialize}; nest! { #[derive(Debug, Deserialize, Serialize)]* #[serde(rename_all = "kebab-case")]* - pub struct Config { + pub struct CodSpeedConfig { pub auth: pub struct AuthConfig { - pub token: String, + pub token: Option, } } } @@ -27,20 +27,20 @@ fn get_configuration_file_path() -> PathBuf { config_dir.join("config.yaml") } -impl Default for Config { +impl Default for CodSpeedConfig { fn default() -> Self { Self { - auth: AuthConfig { token: "".into() }, + auth: AuthConfig { token: None }, } } } -impl Config { +impl CodSpeedConfig { /// Load the configuration. If it does not exist, store and return a default configuration - pub async fn load() -> Result { + pub fn load() -> Result { let config_path = get_configuration_file_path(); - match tokio::fs::read(&config_path).await { + match fs::read(&config_path) { Ok(config_str) => { let config = serde_yaml::from_slice(&config_str).context(format!( "Failed to parse CodSpeed config at {}", @@ -51,8 +51,8 @@ impl Config { } Err(e) if e.kind() == std::io::ErrorKind::NotFound => { debug!("Config file not found at {}", config_path.display()); - let config = Config::default(); - config.persist().await?; + let config = CodSpeedConfig::default(); + config.persist()?; Ok(config) } Err(e) => bail!("Failed to load config: {}", e), @@ -60,12 +60,12 @@ impl Config { } /// Persist changes to the configuration - pub async fn persist(&self) -> Result<()> { + pub fn persist(&self) -> Result<()> { let config_path = get_configuration_file_path(); - tokio::fs::create_dir_all(config_path.parent().unwrap()).await?; + fs::create_dir_all(config_path.parent().unwrap())?; let config_str = serde_yaml::to_string(self)?; - tokio::fs::write(&config_path, config_str).await?; + fs::write(&config_path, config_str)?; debug!("Config written to {}", config_path.display()); Ok(())