first commit
This commit is contained in:
60
src/config/mod.rs
Normal file
60
src/config/mod.rs
Normal file
@@ -0,0 +1,60 @@
|
||||
pub mod model;
|
||||
|
||||
pub use model::{Config, HeroDbConfig, NamespaceConfig};
|
||||
|
||||
use crate::error::{Error, Result};
|
||||
use std::collections::HashMap;
|
||||
use std::fs;
|
||||
use std::path::PathBuf;
|
||||
|
||||
/// Load configuration from file
|
||||
pub fn load_config(path: Option<PathBuf>) -> Result<Config> {
|
||||
let config_path = path.unwrap_or_else(default_config_path);
|
||||
|
||||
if !config_path.exists() {
|
||||
return Err(Error::Config(format!(
|
||||
"Configuration file not found: {}",
|
||||
config_path.display()
|
||||
)));
|
||||
}
|
||||
|
||||
let content = fs::read_to_string(&config_path)?;
|
||||
let config: Config = toml::from_str(&content)
|
||||
.map_err(|e| Error::Config(format!("Failed to parse config: {}", e)))?;
|
||||
|
||||
Ok(config)
|
||||
}
|
||||
|
||||
/// Save configuration to file
|
||||
pub fn save_config(config: &Config, path: Option<PathBuf>) -> Result<()> {
|
||||
let config_path = path.unwrap_or_else(default_config_path);
|
||||
|
||||
// Create parent directory if it doesn't exist
|
||||
if let Some(parent) = config_path.parent() {
|
||||
fs::create_dir_all(parent)?;
|
||||
}
|
||||
|
||||
let content = toml::to_string_pretty(config)
|
||||
.map_err(|e| Error::Config(format!("Failed to serialize config: {}", e)))?;
|
||||
|
||||
fs::write(&config_path, content)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Get the default configuration file path
|
||||
pub fn default_config_path() -> PathBuf {
|
||||
let home = std::env::var("HOME").unwrap_or_else(|_| ".".to_string());
|
||||
PathBuf::from(home)
|
||||
.join(".config")
|
||||
.join("osiris")
|
||||
.join("config.toml")
|
||||
}
|
||||
|
||||
/// Create a default configuration
|
||||
pub fn create_default_config(herodb_url: String) -> Config {
|
||||
Config {
|
||||
herodb: HeroDbConfig { url: herodb_url },
|
||||
namespaces: HashMap::new(),
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user