From d83fe68ed51016bbb87d83aa512ef8b9d3f0780e Mon Sep 17 00:00:00 2001 From: "Adam T. Carpenter" Date: Thu, 15 Apr 2021 20:03:01 -0400 Subject: split config into modules that actually need it, started parsing args --- src/repo/mod.rs | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) (limited to 'src/repo') diff --git a/src/repo/mod.rs b/src/repo/mod.rs index 45e9ce3..c7992c9 100644 --- a/src/repo/mod.rs +++ b/src/repo/mod.rs @@ -1,4 +1,3 @@ -use crate::config::CONFIG_INSTANCE; use crate::error::TwinHError; use crate::models::Car; use crate::models::Part; @@ -6,20 +5,28 @@ use bincode::deserialize; use constants::*; use once_cell::sync::Lazy; use sled::{Config, Db}; +use std::env; mod constants; -static REPO_INSTANCE: Lazy = Lazy::new(|| { - Config::default() - .path(&CONFIG_INSTANCE.db_path) - .temporary(true) - .open() - .expect("Couldn't open DB!") +pub static DB_PATH_INSTANCE: Lazy = Lazy::new(|| { + env::args() + .skip(1) + .last() + .expect("database directory not provided") }); +static REPO_INSTANCE: Lazy = + Lazy::new( + || match Config::default().path(DB_PATH_INSTANCE.as_str()).open() { + Err(e) => panic!("failed to open database: {}", e), + Ok(db) => db, + }, + ); + pub fn create_demo_db() -> Result<(), TwinHError> { let db = sled::Config::default() - .path(&CONFIG_INSTANCE.db_path) + .path(DB_PATH_INSTANCE.as_str()) .create_new(true) .open()?; let cars_tree = db.open_tree(CARS_TREE)?; @@ -28,7 +35,7 @@ pub fn create_demo_db() -> Result<(), TwinHError> { pub fn create_new_db() -> Result<(), TwinHError> { sled::Config::default() - .path(&CONFIG_INSTANCE.db_path) + .path(DB_PATH_INSTANCE.as_str()) .create_new(true) .open()?; Ok(()) -- cgit v1.2.3