@@ -3,16 +3,26 @@ use std::path::PathBuf;
33use std:: sync:: OnceLock ;
44use std:: { env, fs} ;
55
6+ use crate :: arguments:: CotpArgs ;
7+
68const CURRENT_DB_PATH : & str = "./db.cotp" ;
79const XDG_PATH : & str = "cotp/db.cotp" ;
810const HOME_PATH : & str = ".cotp/db.cotp" ;
911
10- static ONCE_COMPUTED_PATH : OnceLock < PathBuf > = OnceLock :: new ( ) ;
12+ pub static DATABASE_PATH : OnceLock < PathBuf > = OnceLock :: new ( ) ;
1113
12- pub fn get_db_path ( ) -> PathBuf {
13- env:: var ( "COTP_DB_PATH" )
14- . map ( PathBuf :: from)
15- . unwrap_or_else ( |_| get_default_db_path ( ) )
14+ /// Initialize singleton database path
15+ pub fn init_path ( args : & CotpArgs ) -> PathBuf {
16+ DATABASE_PATH
17+ . get_or_init ( || {
18+ args. database_path
19+ . as_ref ( )
20+ . map ( String :: from)
21+ . or ( env:: var ( "COTP_DB_PATH" ) . ok ( ) )
22+ . map ( PathBuf :: from)
23+ . unwrap_or_else ( get_default_db_path)
24+ } )
25+ . to_owned ( )
1626}
1727
1828// Pushing an absolute path to a PathBuf replaces the entire PathBuf: https://doc.rust-lang.org/std/path/struct.PathBuf.html#method.push
@@ -26,26 +36,21 @@ fn get_default_db_path() -> PathBuf {
2636
2737 let home_path = home_dir ( ) . map ( |path| path. join ( HOME_PATH ) ) ;
2838
29- ONCE_COMPUTED_PATH
30- . get_or_init ( || {
31- data_dir ( )
32- . map ( PathBuf :: from)
33- . map ( |p| p. join ( XDG_PATH ) )
34- . map ( |xdg| {
35- if !xdg. exists ( ) {
36- if let Some ( home) = & home_path {
37- if home. exists ( ) {
38- fs:: create_dir_all ( xdg. parent ( ) . unwrap ( ) )
39- . expect ( "Failed to create dir" ) ;
40- fs:: copy ( home, xdg. as_path ( ) )
41- . expect ( "Failed on copy from legacy dir to XDG_DATA_HOME" ) ;
42- }
43- }
39+ data_dir ( )
40+ . map ( PathBuf :: from)
41+ . map ( |p| p. join ( XDG_PATH ) )
42+ . map ( |xdg| {
43+ if !xdg. exists ( ) {
44+ if let Some ( home) = & home_path {
45+ if home. exists ( ) {
46+ fs:: create_dir_all ( xdg. parent ( ) . unwrap ( ) ) . expect ( "Failed to create dir" ) ;
47+ fs:: copy ( home, xdg. as_path ( ) )
48+ . expect ( "Failed on copy from legacy dir to XDG_DATA_HOME" ) ;
4449 }
45- xdg
46- } )
47- . or ( home_path)
48- . unwrap_or ( portable_path)
50+ }
51+ }
52+ xdg
4953 } )
50- . to_owned ( )
54+ . or ( home_path)
55+ . unwrap_or ( portable_path)
5156}
0 commit comments