diff --git a/Cargo.lock b/Cargo.lock index 28afe11f..1041fabb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -826,6 +826,18 @@ dependencies = [ "crossbeam-utils", ] +[[package]] +name = "confy" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "45b1f4c00870f07dc34adcac82bb6a72cc5aabca8536ba1797e01df51d2ce9a0" +dependencies = [ + "directories", + "serde", + "thiserror 1.0.69", + "toml", +] + [[package]] name = "console" version = "0.15.10" @@ -2138,6 +2150,7 @@ dependencies = [ "clap-verbosity-flag", "clap_complete", "clap_mangen", + "confy", "crossbeam", "crossterm", "date_time_parser", diff --git a/Cargo.toml b/Cargo.toml index ae763751..5697b07f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -82,6 +82,7 @@ crossbeam = { version = "0.8.4", features = ["crossbeam-channel"] } uuid = { version = "1.12.1", features = ["v4"] } etcetera = "0.8.0" tracing-appender = "0.2.3" +confy = "0.6.1" [features] default = ["tui"] diff --git a/src/cli/mod.rs b/src/cli/mod.rs index cb8e139b..da9e79ee 100755 --- a/src/cli/mod.rs +++ b/src/cli/mod.rs @@ -7,6 +7,7 @@ use ingestion::IngestionCommand; use miette::IntoDiagnostic; use sea_orm::prelude::async_trait::async_trait; use substance::SubstanceCommand; +use std::path::PathBuf; use crate::core::CommandHandler; use crate::utils::AppContext; diff --git a/src/core/config.rs b/src/core/config.rs index 0de5b65d..f0e0cccb 100644 --- a/src/core/config.rs +++ b/src/core/config.rs @@ -1,42 +1,61 @@ -use lazy_static::lazy_static; +use serde::{Deserialize, Serialize}; +use std::env; use std::env::temp_dir; -use std::path::Path; use std::path::PathBuf; +use directories::ProjectDirs; +use lazy_static::lazy_static; +use crate::cli::Cli; pub const NAME: &str = env!("CARGO_PKG_NAME"); pub const VERSION: &str = env!("CARGO_PKG_VERSION"); -lazy_static::lazy_static! { - pub static ref CONFIG: Config = Config::default(); - /// Returns the path to the project's data directory. - pub static ref DATA_DIR: Box = directories::ProjectDirs::from("com", "keinsell", NAME).unwrap_or_else(|| panic!("project data directory not found")).data_dir().into(); - /// Returns the path to the project's cache directory. - pub static ref CACHE_DIR: Box = directories::ProjectDirs::from("com", "keinsell", NAME).unwrap_or_else(|| panic!("project data directory not found")).cache_dir().into(); - /// Returns the path to the project's config directory. - pub static ref CONFIG_DIR: Box = directories::ProjectDirs::from("com", "keinsell", NAME).unwrap_or_else(|| panic!("project data directory not found")).config_dir().into(); +lazy_static! { + pub static ref DATA_DIR: PathBuf = { + ProjectDirs::from("com", "keinsell", NAME) + .expect("project data directory not found") + .data_dir() + .to_path_buf() + }; + pub static ref CACHE_DIR: PathBuf = { + ProjectDirs::from("com", "keinsell", NAME) + .expect("project cache directory not found") + .cache_dir() + .to_path_buf() + }; + pub static ref CONFIG_DIR: PathBuf = { + ProjectDirs::from("com", "keinsell", NAME) + .expect("project config directory not found") + .config_dir() + .to_path_buf() + }; } - -// TODO(NEU-1): Implement -#[derive(Debug, Clone)] -pub struct Config -{ +#[derive(Debug, Serialize, Deserialize, Clone)] +pub struct Config { pub sqlite_path: PathBuf, + pub version: Option, } -impl Default for Config -{ - fn default() -> Self - { - let mut journal_path = DATA_DIR.join("journal.db").clone(); - - if cfg!(test) || cfg!(debug_assertions) - { - journal_path = temp_dir().join("neuronek.sqlite"); - } - +impl Default for Config { + fn default() -> Self { + let default_db = if cfg!(test) || cfg!(debug_assertions) { + temp_dir().join("neuronek.sqlite") + } else { + DATA_DIR.join("journal.db") + }; Config { - sqlite_path: journal_path, + sqlite_path: default_db, + version: Some(1), } } } + +impl Config { + pub fn load() -> Self { + confy::load(NAME, None).unwrap_or_default() + } +} + +lazy_static! { + pub static ref CONFIG: Config = Config::load(); +} diff --git a/src/utils.rs b/src/utils.rs index 5d82b49b..d7e253c6 100755 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,5 +1,5 @@ use crate::cli::OutputFormat; -use crate::core::config::CONFIG; +use crate::core::config::{Config, CONFIG}; use crate::database::Migrator; use async_std::task::block_on; use atty::Stream;