From bbce03f95d82d5373f2b6a1c28b03eb25d7bb94c Mon Sep 17 00:00:00 2001 From: Naz Date: Sat, 11 Oct 2025 14:44:12 +0100 Subject: =?UTF-8?q?=E2=9C=A8feat:=20add=20flags=20to=20override=20values?= =?UTF-8?q?=20in=20config=20file?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/config.rs | 60 ++++++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 47 insertions(+), 13 deletions(-) (limited to 'src/config.rs') diff --git a/src/config.rs b/src/config.rs index 6345ffa..2f1bbb7 100644 --- a/src/config.rs +++ b/src/config.rs @@ -2,7 +2,7 @@ use std::path::PathBuf; use serde::{Deserialize, Serialize}; -use crate::{Error, Result}; +use crate::{Error, Result, args::ServArgs}; const STYLE: &str = include_str!("../examples/simple-gruvbox.css"); const BOOKMARKS: &str = include_str!("../examples/bookmarks.json"); @@ -17,22 +17,20 @@ pub struct Config { } impl Config { - pub fn new() -> Result { - let config_home = if let Ok(xdg_config_home) = std::env::var("XDG_CONFIG_HOME") { - PathBuf::from(xdg_config_home).join("sbm-rs") - } else if let Ok(home) = std::env::var("HOME") { - PathBuf::from(home).join(".config/sbm-rs") + pub fn new(args: ServArgs) -> Result { + let config_home = Config::get_config_home()?; + + let config_path = &config_home.join("config.toml"); + + let mut config = if config_path.exists() { + Config::load_config(config_path)? } else { - return Err(Error::ConfigNotFound); + Config::generate_defaults(config_home)? }; - let config = &config_home.join("config.toml"); + config.override_with_serv_args(args)?; - if config.exists() { - Config::load_config(config) - } else { - Config::generate_defaults(config_home) - } + Ok(config) } pub fn load_config(config: &PathBuf) -> Result { let config = std::fs::read_to_string(&config)?; @@ -65,4 +63,40 @@ impl Config { Ok(config) } + pub fn override_with_serv_args(&mut self, args: ServArgs) -> Result<()> { + if let Some(port) = &args.port { + self.port = port.parse::()?; + } + + if let Some(style_file) = &args.style_file { + if PathBuf::from(style_file).exists() { + self.style_file = PathBuf::from(style_file) + } + } + + if let Some(bookmarks_file) = &args.bookmarks_file { + if PathBuf::from(bookmarks_file).exists() { + self.bookmarks_file = PathBuf::from(bookmarks_file) + } + } + + if let Some(favicon_file) = &args.favicon_file { + if PathBuf::from(favicon_file).exists() { + self.favicon_file = PathBuf::from(favicon_file) + } + } + + Ok(()) + } + fn get_config_home() -> Result { + let config_home = if let Ok(xdg_config_home) = std::env::var("XDG_CONFIG_HOME") { + PathBuf::from(xdg_config_home).join("sbm-rs") + } else if let Ok(home) = std::env::var("HOME") { + PathBuf::from(home).join(".config/sbm-rs") + } else { + return Err(Error::ConfigHomeNotFound); + }; + + Ok(config_home) + } } -- cgit v1.2.3