@@ -3,16 +3,26 @@ use std::path::PathBuf;
3
3
use std:: sync:: OnceLock ;
4
4
use std:: { env, fs} ;
5
5
6
+ use crate :: arguments:: CotpArgs ;
7
+
6
8
const CURRENT_DB_PATH : & str = "./db.cotp" ;
7
9
const XDG_PATH : & str = "cotp/db.cotp" ;
8
10
const HOME_PATH : & str = ".cotp/db.cotp" ;
9
11
10
- static ONCE_COMPUTED_PATH : OnceLock < PathBuf > = OnceLock :: new ( ) ;
12
+ pub static DATABASE_PATH : OnceLock < PathBuf > = OnceLock :: new ( ) ;
11
13
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 ( )
16
26
}
17
27
18
28
// 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 {
26
36
27
37
let home_path = home_dir ( ) . map ( |path| path. join ( HOME_PATH ) ) ;
28
38
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" ) ;
44
49
}
45
- xdg
46
- } )
47
- . or ( home_path)
48
- . unwrap_or ( portable_path)
50
+ }
51
+ }
52
+ xdg
49
53
} )
50
- . to_owned ( )
54
+ . or ( home_path)
55
+ . unwrap_or ( portable_path)
51
56
}
0 commit comments