summaryrefslogtreecommitdiff
path: root/src/repo/mod.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/repo/mod.rs')
-rw-r--r--src/repo/mod.rs25
1 files changed, 16 insertions, 9 deletions
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<Db> = Lazy::new(|| {
- Config::default()
- .path(&CONFIG_INSTANCE.db_path)
- .temporary(true)
- .open()
- .expect("Couldn't open DB!")
+pub static DB_PATH_INSTANCE: Lazy<String> = Lazy::new(|| {
+ env::args()
+ .skip(1)
+ .last()
+ .expect("database directory not provided")
});
+static REPO_INSTANCE: Lazy<Db> =
+ 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(())