Skip to content

Commit

Permalink
update configuration management
Browse files Browse the repository at this point in the history
  • Loading branch information
keinsell committed Feb 3, 2025
1 parent ddf12b4 commit 90f5c1e
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 26 deletions.
13 changes: 13 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
1 change: 1 addition & 0 deletions src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
77 changes: 51 additions & 26 deletions src/core/config.rs
Original file line number Diff line number Diff line change
@@ -1,42 +1,67 @@
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<Path> = 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<Path> = 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<Path> = 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<u32>,
}

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: default_db,
version: Some(1),
}
}
}

Config {
sqlite_path: journal_path,
impl Config {
pub fn load() -> Self {
// Load config from file; if not available, use default
let mut cfg: Config = confy::load(NAME, None).unwrap_or_default();

if let Ok(env_sqlite) = env::var("NEURONEK_SQLITE_PATH") {
cfg.sqlite_path = PathBuf::from(env_sqlite);
}

// TODO: Add config schema migration

confy::store(NAME, None, &cfg).expect("Failed to store configuration");
cfg
}
}

0 comments on commit 90f5c1e

Please sign in to comment.