feat: add paths and defaults config with graceful fallback
This commit is contained in:
1 parent
a8f409d2ff
commit
8d8cf4805e
3 files changed
+131
-13
No files matched your search
@@ -1,6 +1,14 @@
|
|||||||
[general]
|
[general]
|
||||||
debug = false
|
debug = false
|
||||||
|
|
||||||
|
[paths]
|
||||||
|
instances_dir = "~/.local/share/mcl/instances"
|
||||||
|
# java_path is optional, leave commented out by default
|
||||||
|
|
||||||
|
[defaults]
|
||||||
|
memory_min = "512M"
|
||||||
|
memory_max = "2G"
|
||||||
|
|
||||||
[colors]
|
[colors]
|
||||||
background = "#1e1e1e"
|
background = "#1e1e1e"
|
||||||
foreground = "white"
|
foreground = "white"
|
||||||
|
|||||||
+66
-13
@@ -8,8 +8,13 @@ use types::Config;
|
|||||||
pub mod types;
|
pub mod types;
|
||||||
|
|
||||||
pub fn get_config_path() -> PathBuf {
|
pub fn get_config_path() -> PathBuf {
|
||||||
let base_dir = config_dir().unwrap();
|
match config_dir() {
|
||||||
base_dir.join("mcl/")
|
Some(base_dir) => base_dir.join("mcl/"),
|
||||||
|
None => {
|
||||||
|
tracing::error!("Could not determine config directory, falling back to ./config/");
|
||||||
|
PathBuf::from("./config/")
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn ensure_config_exists(default_path: &str) -> PathBuf {
|
fn ensure_config_exists(default_path: &str) -> PathBuf {
|
||||||
@@ -17,27 +22,75 @@ fn ensure_config_exists(default_path: &str) -> PathBuf {
|
|||||||
|
|
||||||
if !config_path.exists() {
|
if !config_path.exists() {
|
||||||
if let Some(parent_dir) = config_path.parent() {
|
if let Some(parent_dir) = config_path.parent() {
|
||||||
fs::create_dir_all(parent_dir).expect("Failed to create configuration directory");
|
match fs::create_dir_all(parent_dir) {
|
||||||
|
Ok(_) => {}
|
||||||
|
Err(e) => {
|
||||||
|
tracing::error!("Failed to create configuration directory: {}", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fs::copy(default_path, &config_path).expect("Failed to copy default configuration file");
|
match fs::copy(default_path, &config_path) {
|
||||||
tracing::debug!(
|
Ok(_) => {
|
||||||
"Default configuration copied to '{}'",
|
tracing::debug!(
|
||||||
config_path.display()
|
"Default configuration copied to '{}'",
|
||||||
);
|
config_path.display()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
tracing::error!(
|
||||||
|
"Failed to copy default config from '{}' to '{}': {}",
|
||||||
|
default_path,
|
||||||
|
config_path.display(),
|
||||||
|
e
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
config_path
|
config_path
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn load_config(config_path: &PathBuf) -> Result<Config, ConfigError> {
|
pub fn load_config(config_path: &PathBuf) -> Result<Config, ConfigError> {
|
||||||
ConfigLoader::builder()
|
let built = match ConfigLoader::builder()
|
||||||
.add_source(File::from(config_path.clone()))
|
.add_source(File::from(config_path.clone()).required(false))
|
||||||
.build()?
|
.build()
|
||||||
.try_deserialize()
|
{
|
||||||
|
Ok(c) => c,
|
||||||
|
Err(e) => {
|
||||||
|
tracing::error!(
|
||||||
|
"Failed to build config from '{}': {}",
|
||||||
|
config_path.display(),
|
||||||
|
e
|
||||||
|
);
|
||||||
|
return Err(e);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
match built.try_deserialize() {
|
||||||
|
Ok(config) => Ok(config),
|
||||||
|
Err(e) => {
|
||||||
|
tracing::error!(
|
||||||
|
"Failed to deserialize config from '{}': {}",
|
||||||
|
config_path.display(),
|
||||||
|
e
|
||||||
|
);
|
||||||
|
Err(e)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub static SETTINGS: Lazy<Config> = Lazy::new(|| {
|
pub static SETTINGS: Lazy<Config> = Lazy::new(|| {
|
||||||
let path: PathBuf = ensure_config_exists("assets/default.toml");
|
let path: PathBuf = ensure_config_exists("assets/default.toml");
|
||||||
load_config(&path).expect("Failed to load configuration")
|
match load_config(&path) {
|
||||||
|
Ok(config) => config,
|
||||||
|
Err(e) => {
|
||||||
|
tracing::error!("Config load failed, using defaults: {}", e);
|
||||||
|
Config {
|
||||||
|
general: types::General::default(),
|
||||||
|
paths: types::Paths::default(),
|
||||||
|
defaults: types::Defaults::default(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
@@ -2,10 +2,67 @@ use serde::Deserialize;
|
|||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
pub struct General {
|
pub struct General {
|
||||||
|
#[serde(default)]
|
||||||
pub debug: bool,
|
pub debug: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Default for General {
|
||||||
|
fn default() -> Self {
|
||||||
|
General { debug: false }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize)]
|
||||||
|
pub struct Paths {
|
||||||
|
#[serde(default = "default_instances_dir")]
|
||||||
|
pub instances_dir: String,
|
||||||
|
pub java_path: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn default_instances_dir() -> String {
|
||||||
|
"~/.local/share/mcl/instances".to_string()
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for Paths {
|
||||||
|
fn default() -> Self {
|
||||||
|
Paths {
|
||||||
|
instances_dir: default_instances_dir(),
|
||||||
|
java_path: None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize)]
|
||||||
|
pub struct Defaults {
|
||||||
|
#[serde(default = "default_memory_min")]
|
||||||
|
pub memory_min: String,
|
||||||
|
#[serde(default = "default_memory_max")]
|
||||||
|
pub memory_max: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn default_memory_min() -> String {
|
||||||
|
"512M".to_string()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn default_memory_max() -> String {
|
||||||
|
"2G".to_string()
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for Defaults {
|
||||||
|
fn default() -> Self {
|
||||||
|
Defaults {
|
||||||
|
memory_min: default_memory_min(),
|
||||||
|
memory_max: default_memory_max(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
pub struct Config {
|
pub struct Config {
|
||||||
|
#[serde(default)]
|
||||||
pub general: General,
|
pub general: General,
|
||||||
|
#[serde(default)]
|
||||||
|
pub paths: Paths,
|
||||||
|
#[serde(default)]
|
||||||
|
pub defaults: Defaults,
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user