Skip to content

Commit d3c3e1f

Browse files
authored
Merge pull request #30 from berserkk/custom-db-path
Update get_db_path to read DB path from an environment variable
2 parents 62c195e + 5b249d0 commit d3c3e1f

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

src/utils.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::env;
12
use std::fs::File;
23
use std::io::prelude::*;
34
use std::path::PathBuf;
@@ -9,11 +10,14 @@ use dirs::home_dir;
910
use crate::otp::otp_element::OTPElement;
1011

1112
pub fn get_db_path() -> PathBuf {
12-
get_cotp_folder().join("db.cotp")
13+
match env::var("COTP_DB_PATH") {
14+
Ok(value) => PathBuf::from(value),
15+
Err(_e) => get_default_db_path(),
16+
}
1317
}
1418

1519
// Pushing an absolute path to a PathBuf replaces the entire PathBuf: https://doc.rust-lang.org/std/path/struct.PathBuf.html#method.push
16-
pub fn get_cotp_folder() -> PathBuf {
20+
pub fn get_default_db_path() -> PathBuf {
1721
let result: Option<PathBuf> = {
1822
#[cfg(not(debug_assertions))]{
1923
home_dir()
@@ -33,18 +37,18 @@ pub fn get_cotp_folder() -> PathBuf {
3337
}
3438
current_dir
3539
},
36-
}.join(".cotp")
40+
}.join(".cotp/db.cotp")
3741
}
3842

3943
pub fn create_db_if_needed() -> Result<bool, ()> {
40-
let cotp_folder = get_cotp_folder();
41-
if !cotp_folder.exists() {
42-
match std::fs::create_dir(cotp_folder) {
44+
let db_path = get_db_path();
45+
let db_dir = db_path.parent().unwrap();
46+
if !db_dir.exists() {
47+
match std::fs::create_dir(db_dir) {
4348
Ok(()) => {}
4449
Err(_e) => {}
4550
}
4651
}
47-
let db_path = get_db_path();
4852
if !db_path.exists() {
4953
return match std::fs::File::create(db_path) {
5054
Ok(_f) => Ok(true),

0 commit comments

Comments
 (0)