1+ use std:: env;
12use std:: fs:: File ;
23use std:: io:: prelude:: * ;
34use std:: path:: PathBuf ;
@@ -9,11 +10,14 @@ use dirs::home_dir;
910use crate :: otp:: otp_element:: OTPElement ;
1011
1112pub 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
3943pub 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