Skip to content

Commit 749024e

Browse files
author
replydev
committed
--info argument
1 parent 419cee4 commit 749024e

File tree

5 files changed

+55
-14
lines changed

5 files changed

+55
-14
lines changed

src/argument_functions.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ pub fn help(){
1515
println!(" -ex,--export | Export the entire database in a plaintext json format");
1616
println!(" -j,--json | Print results in json format");
1717
println!(" -s,--single | Print OTP codes in single mode");
18+
println!(" -in,--info [ID] | Print info of choosen OTP code");
1819
println!(" -h,--help | Print this help");
1920
}
2021

@@ -160,4 +161,17 @@ pub fn single(args: Vec<String>){
160161
else{
161162
println!("Invalid argument, type cotp --single");
162163
}
163-
}
164+
}
165+
166+
pub fn info(args: Vec<String>){
167+
if args.len() == 3{
168+
let id = args[2].parse::<usize>().unwrap();
169+
match otp_helper::print_json_result(id){
170+
Ok(()) => {},
171+
Err(e) => eprintln!("An error occurred: {}",e),
172+
}
173+
}
174+
else{
175+
println!("Invalid arguments, type cotp --info");
176+
}
177+
}

src/database_loader.rs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::fs::{File,read_to_string};
22
use std::io::prelude::*;
33
use serde_json;
44
use crate::utils;
5-
use utils::get_db_path;
5+
use utils::{get_db_path,check_elements};
66
use crate::cryptograpy;
77
use crate::otp::otp_element::OTPElement;
88

@@ -155,15 +155,3 @@ pub fn overwrite_database_json(json: &str) -> Result<(),std::io::Error>{
155155
utils::write_to_file(&encrypted, &mut file)
156156
}
157157

158-
fn check_elements(id: usize,elements: &Vec<OTPElement>) -> Result<(),String>{
159-
if elements.len() == 0{
160-
return Err(String::from("there are no elements in your database. Type \"cotp -h\" to get help."));
161-
}
162-
163-
if id >= elements.len(){
164-
return Err(format!("{} is a bad index",id+1));
165-
}
166-
167-
Ok(())
168-
}
169-

src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ fn args_parser(args: Vec<String>) -> bool {
115115
"-ex"| "--export" => argument_functions::export(args),
116116
"-j" | "--json" => argument_functions::json(args),
117117
"-s" | "--single" => argument_functions::single(args),
118+
"-in" | "--info" => argument_functions::info(args),
118119
_=>{
119120
println!("Invalid argument: {}, type cotp -h to get command options", args[1]);
120121
return true;

src/otp/otp_helper.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use serde_json;
33
use serde::{Deserialize, Serialize};
44
use crate::otp::otp_element::OTPElement;
55
use crate::otp::otp_maker::make_totp;
6+
use crate::utils::check_elements;
67

78

89
#[derive(Serialize, Deserialize)]
@@ -78,4 +79,28 @@ pub fn get_json_results() -> Result<String,String>{
7879
let json_string: &str = &serde_json::to_string_pretty(&results).unwrap();
7980

8081
Ok(json_string.to_string())
82+
}
83+
84+
pub fn print_json_result(index: usize) -> Result<(),String>{
85+
let elements: Vec<OTPElement>;
86+
87+
match database_loader::read_from_file(){
88+
Ok(result) => elements = result,
89+
Err(e) => return Err(e),
90+
}
91+
92+
match check_elements(index, &elements){
93+
Ok(()) => {},
94+
Err(e) => {
95+
return Err(e);
96+
}
97+
}
98+
99+
let choosed_element: &OTPElement = &elements[index];
100+
101+
println!("Issuer: {}",choosed_element.issuer());
102+
println!("Label: {}",choosed_element.label());
103+
println!("Algoritmh: {}",choosed_element.algorithm());
104+
println!("Digits: {}",choosed_element.digits());
105+
Ok(())
81106
}

src/utils.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::io::prelude::*;
33
use std::path::PathBuf;
44
use std::time::{SystemTime, UNIX_EPOCH};
55
use dirs::home_dir;
6+
use crate::otp::otp_element::OTPElement;
67

78
#[cfg(debug_assertions)]
89
pub fn get_db_path() -> PathBuf{
@@ -78,6 +79,18 @@ pub fn pow(base: f64,exp: i64) -> f64{
7879
tot
7980
}
8081

82+
pub fn check_elements(id: usize,elements: &Vec<OTPElement>) -> Result<(),String>{
83+
if elements.len() == 0{
84+
return Err(String::from("there are no elements in your database. Type \"cotp -h\" to get help."));
85+
}
86+
87+
if id >= elements.len(){
88+
return Err(format!("{} is a bad index",id+1));
89+
}
90+
91+
Ok(())
92+
}
93+
8194

8295
#[cfg(test)]
8396
mod tests{

0 commit comments

Comments
 (0)