1
1
use crate :: args:: { AddArgs , EditArgs , ExportArgs , ImportArgs } ;
2
2
use crate :: exporters:: do_export;
3
+ use crate :: exporters:: otp_uri:: OtpUriList ;
3
4
use crate :: importers:: aegis:: AegisJson ;
4
5
use crate :: importers:: aegis_encrypted:: AegisEncryptedDatabase ;
5
6
use crate :: importers:: authy_remote_debug:: AuthyExportedList ;
6
7
use crate :: importers:: converted:: ConvertedJsonList ;
7
8
use crate :: importers:: freeotp_plus:: FreeOTPPlusJson ;
9
+ use crate :: importers:: importer:: import_from_path;
8
10
use crate :: otp:: from_otp_uri:: FromOtpUri ;
9
11
use crate :: otp:: otp_element:: { OTPDatabase , OTPElement } ;
10
- use crate :: { importers, utils} ;
12
+ use crate :: utils;
13
+ use color_eyre:: eyre:: { eyre, ErrReport } ;
11
14
use zeroize:: Zeroize ;
12
15
13
- pub fn import ( matches : ImportArgs , mut database : OTPDatabase ) -> Result < OTPDatabase , String > {
16
+ pub fn import ( matches : ImportArgs , mut database : OTPDatabase ) -> color_eyre :: Result < OTPDatabase > {
14
17
let path = matches. path ;
15
18
16
19
let backup_type = matches. backup_type ;
17
20
18
21
let result = if backup_type. cotp {
19
- importers :: importer :: import_from_path :: < OTPDatabase > ( path)
22
+ import_from_path :: < OTPDatabase > ( path)
20
23
} else if backup_type. andotp {
21
- importers :: importer :: import_from_path :: < Vec < OTPElement > > ( path)
24
+ import_from_path :: < Vec < OTPElement > > ( path)
22
25
} else if backup_type. aegis {
23
- importers :: importer :: import_from_path :: < AegisJson > ( path)
26
+ import_from_path :: < AegisJson > ( path)
24
27
} else if backup_type. aegis_encrypted {
25
- importers :: importer :: import_from_path :: < AegisEncryptedDatabase > ( path)
28
+ import_from_path :: < AegisEncryptedDatabase > ( path)
26
29
} else if backup_type. freeotp_plus {
27
- importers :: importer :: import_from_path :: < FreeOTPPlusJson > ( path)
30
+ import_from_path :: < FreeOTPPlusJson > ( path)
28
31
} else if backup_type. authy_exported {
29
- importers :: importer :: import_from_path :: < AuthyExportedList > ( path)
32
+ import_from_path :: < AuthyExportedList > ( path)
30
33
} else if backup_type. google_authenticator
31
34
|| backup_type. authy
32
35
|| backup_type. microsoft_authenticator
33
36
|| backup_type. freeotp
34
37
{
35
- importers:: importer:: import_from_path :: < ConvertedJsonList > ( path)
38
+ import_from_path :: < ConvertedJsonList > ( path)
39
+ } else if backup_type. otp_uri {
40
+ import_from_path :: < OtpUriList > ( path)
36
41
} else {
37
- return Err ( String :: from ( "Invalid arguments provided" ) ) ;
42
+ return Err ( eyre ! ( "Invalid arguments provided" ) ) ;
38
43
} ;
39
44
40
- let elements = result. map_err ( |e| format ! ( "An error occurred: {e}" ) ) ?;
45
+ let elements = result. map_err ( |e| eyre ! ( "{e}" ) ) ?;
41
46
42
47
database. add_all ( elements) ;
43
48
Ok ( database)
44
49
}
45
50
46
- pub fn add ( matches : AddArgs , mut database : OTPDatabase ) -> Result < OTPDatabase , String > {
51
+ pub fn add ( matches : AddArgs , mut database : OTPDatabase ) -> color_eyre :: Result < OTPDatabase > {
47
52
let otp_element = if matches. otp_uri {
48
53
let mut otp_uri = rpassword:: prompt_password ( "Insert the otp uri: " ) . unwrap ( ) ;
49
54
let result = OTPElement :: from_otp_uri ( otp_uri. as_str ( ) ) ;
@@ -53,24 +58,23 @@ pub fn add(matches: AddArgs, mut database: OTPDatabase) -> Result<OTPDatabase, S
53
58
get_from_args ( matches) ?
54
59
} ;
55
60
if !otp_element. valid_secret ( ) {
56
- return Err ( String :: from ( "Invalid secret." ) ) ;
61
+ return Err ( ErrReport :: msg ( "Invalid secret." ) ) ;
57
62
}
58
63
59
64
database. add_element ( otp_element) ;
60
65
Ok ( database)
61
66
}
62
67
63
- fn get_from_args ( matches : AddArgs ) -> Result < OTPElement , String > {
64
- let secret = rpassword:: prompt_password ( "Insert the secret: " )
65
- . map_err ( |e| format ! ( "Error during password insertion: {:?}" , e) ) ?;
68
+ fn get_from_args ( matches : AddArgs ) -> color_eyre:: Result < OTPElement > {
69
+ let secret = rpassword:: prompt_password ( "Insert the secret: " ) . map_err ( ErrReport :: from) ?;
66
70
Ok ( map_args_to_code ( secret, matches) )
67
71
}
68
72
69
73
fn map_args_to_code ( secret : String , matches : AddArgs ) -> OTPElement {
70
74
OTPElement {
71
75
secret,
72
- issuer : matches. issuer . unwrap ( ) ,
73
- label : matches. label ,
76
+ issuer : matches. issuer ,
77
+ label : matches. label . unwrap ( ) ,
74
78
digits : matches. digits ,
75
79
type_ : matches. otp_type ,
76
80
algorithm : matches. algorithm ,
@@ -80,7 +84,7 @@ fn map_args_to_code(secret: String, matches: AddArgs) -> OTPElement {
80
84
}
81
85
}
82
86
83
- pub fn edit ( matches : EditArgs , mut database : OTPDatabase ) -> Result < OTPDatabase , String > {
87
+ pub fn edit ( matches : EditArgs , mut database : OTPDatabase ) -> color_eyre :: Result < OTPDatabase > {
84
88
let secret = matches
85
89
. change_secret
86
90
. then ( || rpassword:: prompt_password ( "Insert the secret: " ) . unwrap ( ) ) ;
@@ -90,7 +94,7 @@ pub fn edit(matches: EditArgs, mut database: OTPDatabase) -> Result<OTPDatabase,
90
94
91
95
if let Some ( real_index) = index. checked_sub ( 1 ) {
92
96
if real_index >= database. elements_ref ( ) . len ( ) {
93
- return Err ( format ! ( "{index} is an invalid index" ) ) ;
97
+ return Err ( eyre ! ( "{index} is an invalid index" ) ) ;
94
98
}
95
99
96
100
match database. mut_element ( real_index) {
@@ -121,15 +125,15 @@ pub fn edit(matches: EditArgs, mut database: OTPDatabase) -> Result<OTPDatabase,
121
125
}
122
126
database. mark_modified ( ) ;
123
127
}
124
- None => return Err ( format ! ( "No element found at index {index}" ) ) ,
128
+ None => return Err ( eyre ! ( "No element found at index {index}" ) ) ,
125
129
}
126
130
Ok ( database)
127
131
} else {
128
- Err ( format ! { "{index} is an invalid index" } )
132
+ Err ( eyre ! ( "{index} is an invalid index" ) )
129
133
}
130
134
}
131
135
132
- pub fn export ( matches : ExportArgs , database : OTPDatabase ) -> Result < OTPDatabase , String > {
136
+ pub fn export ( matches : ExportArgs , database : OTPDatabase ) -> color_eyre :: Result < OTPDatabase > {
133
137
let export_format = matches. format . unwrap_or_default ( ) ;
134
138
let exported_path = if matches. path . is_dir ( ) {
135
139
matches. path . join ( "exported.cotp" )
@@ -142,6 +146,9 @@ pub fn export(matches: ExportArgs, database: OTPDatabase) -> Result<OTPDatabase,
142
146
} else if export_format. andotp {
143
147
let andotp: & Vec < OTPElement > = ( & database) . into ( ) ;
144
148
do_export ( & andotp, exported_path)
149
+ } else if export_format. otp_uri {
150
+ let otp_uri_list: OtpUriList = ( & database) . into ( ) ;
151
+ do_export ( & otp_uri_list, exported_path)
145
152
} else {
146
153
unreachable ! ( "Unreachable code" ) ;
147
154
}
@@ -152,14 +159,14 @@ pub fn export(matches: ExportArgs, database: OTPDatabase) -> Result<OTPDatabase,
152
159
) ;
153
160
database
154
161
} )
155
- . map_err ( |e| format ! ( "An error occurred while exporting database: {e}" ) )
162
+ . map_err ( |e| eyre ! ( "An error occurred while exporting database: {e}" ) )
156
163
}
157
164
158
- pub fn change_password ( mut database : OTPDatabase ) -> Result < OTPDatabase , String > {
165
+ pub fn change_password ( mut database : OTPDatabase ) -> color_eyre :: Result < OTPDatabase > {
159
166
let mut new_password = utils:: verified_password ( "New password: " , 8 ) ;
160
167
database
161
168
. save_with_pw ( & new_password)
162
- . map_err ( |e| format ! ( "An error has occurred: {e}" ) ) ?;
169
+ . map_err ( ErrReport :: from ) ?;
163
170
new_password. zeroize ( ) ;
164
171
Ok ( database)
165
172
}
0 commit comments