Skip to content

Commit cc2db6e

Browse files
author
replydev
committed
Database empty checks
1 parent b958e03 commit cc2db6e

File tree

4 files changed

+72
-39
lines changed

4 files changed

+72
-39
lines changed

src/argument_functions.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ pub fn help(){
88
println!();
99
println!("ARGUMENTS:");
1010
println!(" -a,--add [SECRET] [ISSUER] [LABEL] | Add a new OTP code");
11-
println!(" -r,--remove [SECRET] [ISSUER] [LABEL] | Remove an OTP code");
1211
println!(" -e,--edit [ID] [SECRET] [ISSUER] [LABEL] | Edit an OTP code");
12+
println!(" -r,--remove [ID] | Remove an OTP code");
1313
println!(" -i,--import [APPNAME] [PATH] | Import a backup from a given application");
1414
println!(" -ex,--export | Export the entire database in a plaintext json format");
1515
println!(" -j,--json | Print results in json format");
@@ -111,7 +111,10 @@ pub fn export(args: Vec<String>){
111111

112112
pub fn json(args: Vec<String>){
113113
if args.len() == 2{
114-
println!("{}",otp_helper::get_json_results().expect("Failed to get json results"));
114+
match otp_helper::get_json_results(){
115+
Ok(results) => println!("{}",results),
116+
Err(e) => println!("An error occurred while getting json result: {}",e),
117+
}
115118
}
116119
else{
117120
println!("Invalid argument, type cotp --json");
@@ -122,7 +125,12 @@ pub fn single(args: Vec<String>){
122125
if args.len() == 2{
123126
match otp_helper::read_codes(){
124127
Ok(result) => {
125-
otp_helper::show_codes(&result);
128+
if result.len() == 0{
129+
println!("No codes, type \"cotp -h\" to get help");
130+
}
131+
else{
132+
otp_helper::show_codes(&result);
133+
}
126134
},
127135
Err(e) => println!("An error occurred: {}",e)
128136
}

src/database_loader.rs

Lines changed: 47 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -119,18 +119,19 @@ pub fn remove_element_from_db(mut id: usize) -> Result<(),String>{
119119
}
120120
}
121121

122-
if id >= elements.len(){
123-
return Err(format!("{} is a bad index",id+1));
124-
}
125-
126-
for i in 0..elements.len(){
127-
if i == id {
128-
elements.remove(i);
129-
break;
130-
}
131-
}
132-
overwrite_database(elements);
133-
Ok(())
122+
match check_elements(id, &elements){
123+
Ok(()) => {
124+
for i in 0..elements.len(){
125+
if i == id {
126+
elements.remove(i);
127+
break;
128+
}
129+
}
130+
overwrite_database(elements);
131+
Ok(())
132+
},
133+
Err(e) => Err(e)
134+
}
134135
}
135136

136137
pub fn edit_element(mut id: usize, secret: &str,issuer: &str,label: &str) -> Result<(), String> {
@@ -144,28 +145,28 @@ pub fn edit_element(mut id: usize, secret: &str,issuer: &str,label: &str) -> Res
144145
Ok(result) => elements = result,
145146
Err(_e) => return Err(String::from("Cannot decrypt existing database"))
146147
}
147-
148148

149-
if id >= elements.len() {
150-
return Err(String::from("Invalid element"));
151-
}
152-
153-
for i in 0..elements.len() {
154-
if i == id{
155-
if secret != "."{
156-
elements[i].set_secret(secret.to_string());
157-
}
158-
if issuer != "."{
159-
elements[i].set_issuer(issuer.to_string());
160-
}
161-
if label != "."{
162-
elements[i].set_label(label.to_string());
149+
match check_elements(id,&elements){
150+
Ok(()) => {
151+
for i in 0..elements.len() {
152+
if i == id{
153+
if secret != "."{
154+
elements[i].set_secret(secret.to_string());
155+
}
156+
if issuer != "."{
157+
elements[i].set_issuer(issuer.to_string());
158+
}
159+
if label != "."{
160+
elements[i].set_label(label.to_string());
161+
}
162+
break;
163+
}
163164
}
164-
break;
165-
}
165+
overwrite_database(elements);
166+
Ok(())
167+
},
168+
Err(e) => Err(e)
166169
}
167-
overwrite_database(elements);
168-
Ok(())
169170
}
170171

171172
pub fn export_database() -> Result<String, String> {
@@ -176,6 +177,9 @@ pub fn export_database() -> Result<String, String> {
176177
let contents = cryptograpy::decrypt_string(&encrypted_contents, &cryptograpy::prompt_for_passwords("Password: "));
177178
match contents {
178179
Ok(contents) => {
180+
if contents == "[]"{
181+
return Err(String::from("there are no elements in your database, type \"cotp -h\" to get help"));
182+
}
179183
file.write_all(contents.as_bytes()).expect("Failed to write contents");
180184
return Ok(exported_path);
181185
},
@@ -195,3 +199,15 @@ pub fn overwrite_database_json(json: &str){
195199
utils::write_to_file(&encrypted, &mut File::create(utils::get_db_path()).expect("Failed to open file"));
196200
}
197201

202+
fn check_elements(id: usize,elements: &Vec<OTPElement>) -> Result<(),String>{
203+
if elements.len() == 0{
204+
return Err(String::from("there are no elements in your database. Type \"cotp -h\" to get help."));
205+
}
206+
207+
if id >= elements.len(){
208+
return Err(format!("{} is a bad index",id+1));
209+
}
210+
211+
Ok(())
212+
}
213+

src/main.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,16 @@ fn dashboard(){
5454
let mut lines;
5555
match otp_helper::read_codes(){
5656
Ok(elements) => {
57-
loop{
58-
utils::print_progress_bar();
59-
lines = otp_helper::show_codes(&elements);
60-
sleep(Duration::from_millis(1000));
61-
print!("\x1B[{}A", lines + 1);
57+
if elements.len() == 0{
58+
println!("No codes, type \"cotp -h\" to get help");
59+
}
60+
else{
61+
loop{
62+
utils::print_progress_bar();
63+
lines = otp_helper::show_codes(&elements);
64+
sleep(Duration::from_millis(1000));
65+
print!("\x1B[{}A", lines + 1);
66+
}
6267
}
6368
},
6469
Err(e) => println!("An error as occurred: {}",e),

src/otp_helper.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ pub fn get_json_results() -> Result<String,String>{
6666
}
6767
let mut results: Vec<JsonResult> = Vec::new();
6868

69+
if elements.len() == 0{
70+
return Err(String::from("there are no elements in your database, type \"cotp -h\" to get help"));
71+
}
72+
6973
for i in 0..elements.len() {
7074
let otp_code = get_good_otp_code(&elements[i]);
7175
results.push(JsonResult::new(i+1,elements[i].issuer(),elements[i].label(),otp_code))

0 commit comments

Comments
 (0)