summaryrefslogtreecommitdiff
path: root/src/config.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/config.rs')
-rw-r--r--src/config.rs60
1 files changed, 47 insertions, 13 deletions
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<Config> {
- 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<Config> {
+ 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<Config> {
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::<u16>()?;
+ }
+
+ 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<PathBuf> {
+ 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)
+ }
}