Settings, took care of warnings

This commit is contained in:
2024-11-15 12:33:11 +02:00
parent 14ee1e69bc
commit a060161c64
26 changed files with 323 additions and 126 deletions

View File

@@ -1,18 +1,75 @@
use std::path::{Path, PathBuf};
use std::{path::PathBuf, str::FromStr};
#[derive(Debug, clap::Parser)]
pub struct CliArgs {
/// Manifest path
#[arg(long, short)]
manifest: Option<camino::Utf8PathBuf>,
#[arg(long, short, default_value_t=get_default_manifest_path())]
manifest: camino::Utf8PathBuf,
/// settings file path
#[arg(long, short, default_value_t=get_default_settings_path())]
settings: camino::Utf8PathBuf,
/// Cache dir path
#[arg(long, short, default_value_t=get_default_cache_path())]
cache: camino::Utf8PathBuf,
/// Debug mode
#[arg(long, short)]
pub debug: bool,
}
impl CliArgs {
pub fn manifest_path(&self) -> Option<PathBuf> {
Some(self.manifest.clone()?.into_std_path_buf())
pub fn manifest_path(&self) -> PathBuf {
self.manifest.clone().into_std_path_buf()
}
pub fn settings_path(&self) -> PathBuf {
self.settings.clone().into_std_path_buf()
}
pub fn cache_path(&self) -> PathBuf {
self.cache.clone().into_std_path_buf()
}
}
#[allow(irrefutable_let_patterns)] // Broken?
fn get_default_settings_path() -> camino::Utf8PathBuf {
if let Ok(p) = std::env::var("XMPD_SETTINGS_PATH") {
if let Ok(p) = camino::Utf8PathBuf::from_str(&p) {
return p;
}
}
if let Some(mut p) = dirs::config_dir() {
p.push("xmpd");
p.push("config.toml");
return camino::Utf8PathBuf::from_path_buf(p).expect("Invalid os path");
}
unreachable!()
}
#[allow(irrefutable_let_patterns)] // Broken?
fn get_default_manifest_path() -> camino::Utf8PathBuf {
if let Ok(p) = std::env::var("XMPD_MANIFEST_PATH") {
if let Ok(p) = camino::Utf8PathBuf::from_str(&p) {
return p;
}
}
if let Some(mut p) = dirs::config_dir() {
p.push("xmpd");
p.push("manifest.json");
return camino::Utf8PathBuf::from_path_buf(p).expect("Invalid os path");
}
unreachable!()
}
#[allow(irrefutable_let_patterns)] // Broken?
fn get_default_cache_path() -> camino::Utf8PathBuf {
if let Ok(p) = std::env::var("XMPD_CACHE_PATH") {
if let Ok(p) = camino::Utf8PathBuf::from_str(&p) {
return p;
}
}
if let Some(mut p) = dirs::cache_dir() {
p.push("xmpd");
return camino::Utf8PathBuf::from_path_buf(p).expect("Invalid os path");
}
unreachable!()
}

View File

@@ -8,10 +8,11 @@ pub fn init(cliargs: &CliArgs) {
let level = if cliargs.debug { LevelFilter::Debug } else { LevelFilter::Info };
env_logger::builder()
.format_timestamp(None)
.filter(Some("xmpd_core"), level)
.filter(Some("xmpd"), level)
.filter(Some("xmpd_cli"), level)
.filter(Some("xmpd_gui"), level)
.filter(Some("xmpd_manifest"), level)
.filter(Some("xmpd_config"), level)
.filter(Some("xmpd_dl"), level)
.init();
}

View File

@@ -1,5 +1,3 @@
use std::path::{Path, PathBuf};
use clap::Parser;
mod cli;
@@ -10,12 +8,8 @@ type Result<T> = anyhow::Result<T>;
fn main() -> Result<()> {
let cliargs = cli::CliArgs::parse();
logger::init(&cliargs);
let manifest_path;
if let Some(mp) = cliargs.manifest_path() {
manifest_path = mp;
} else {
manifest_path = PathBuf::from("manifest.json");
};
xmpd_gui::start(manifest_path)?;
log::debug!("Cli: {cliargs:?}");
xmpd_settings::Settings::get()?.load(Some(cliargs.settings_path()))?;
xmpd_gui::start(cliargs.manifest_path())?;
Ok(())
}