Skip to content

Commit

Permalink
refactor(config): rename config::Config into config::CodSpeedConfig a…
Browse files Browse the repository at this point in the history
…nd make it sync
  • Loading branch information
adriencaccia committed Jun 17, 2024
1 parent c78dbe0 commit 919d0ef
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 17 deletions.
8 changes: 4 additions & 4 deletions src/auth.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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");
Expand Down
26 changes: 13 additions & 13 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{env, path::PathBuf};
use std::{env, fs, path::PathBuf};

use crate::prelude::*;
use nestify::nest;
Expand All @@ -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<String>,
}
}
}
Expand All @@ -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<Self> {
pub fn load() -> Result<Self> {
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 {}",
Expand All @@ -51,21 +51,21 @@ 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),
}
}

/// 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(())
Expand Down

0 comments on commit 919d0ef

Please sign in to comment.