Skip to content

Commit eded696

Browse files
author
replydev
committed
Json results for GUIs
1 parent 8d72c85 commit eded696

File tree

3 files changed

+60
-1
lines changed

3 files changed

+60
-1
lines changed

src/argument_functions.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::io::prelude::*;
33
use super::utils::get_db_path;
44
use super::database_loader;
55
use super::cryptograpy;
6+
use super::otp_helper;
67

78
pub fn import(args: Vec<String>){
89
import_database(&args[2]);
@@ -71,6 +72,15 @@ pub fn export(args: Vec<String>){
7172
}
7273
}
7374

75+
pub fn json(args: Vec<String>){
76+
if args.len() == 2{
77+
println!("{}",otp_helper::get_json_results())
78+
}
79+
else{
80+
println!("Invalid argument, type cotp --json");
81+
}
82+
}
83+
7484
fn import_database(filename: &String){
7585
let mut unencrypted_content = read_to_string(filename).unwrap();
7686
let encrypted_content = cryptograpy::encrypt_string(&mut unencrypted_content,&cryptograpy::prompt_for_passwords("Insert password for database encryption: "));

src/main.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ fn main() {
2929
print_title(version);
3030
let init_result = init();
3131
match init_result {
32-
Ok(()) => println!("sodiumoxide init succeeded"),
32+
Ok(()) => {},
3333
Err(()) => {
3434
println!("Failed to init sodiumoxide");
3535
return;
@@ -54,6 +54,7 @@ fn args_parser(args: Vec<String>) -> bool {
5454
"-r" | "--remove" => argument_functions::remove(args),
5555
"-e" | "--edit" => argument_functions::edit(args),
5656
"-ex" | "--export" => argument_functions::export(args),
57+
"-j" | "--json" => argument_functions::json(args),
5758
_=>{
5859
println!("Invalid argument: {}, type cotp -h to get command options", args[1]);
5960
return true;

src/otp_helper.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,39 @@
11
use otp::make_totp;
22
use super::database_loader;
3+
use serde_json;
4+
use serde::{Deserialize, Serialize};
35

6+
#[derive(Serialize, Deserialize)]
7+
struct JsonResult{
8+
index: usize,
9+
issuer: String,
10+
label: String,
11+
otp_code: String,
12+
}
13+
14+
impl JsonResult {
15+
pub fn new(index: usize, issuer: String, label: String,otp_code: String) -> JsonResult {
16+
JsonResult{
17+
index: index,
18+
issuer: issuer,
19+
label: label,
20+
otp_code: otp_code
21+
}
22+
}
23+
24+
pub fn set_index(&mut self, index: usize) {
25+
self.index = index;
26+
}
27+
pub fn set_issuer(&mut self,issuer: String){
28+
self.issuer = issuer;
29+
}
30+
pub fn set_label(&mut self,label: String){
31+
self.label = label;
32+
}
33+
pub fn set_otp_code(&mut self,otp_code: String){
34+
self.otp_code = otp_code;
35+
}
36+
}
437

538
pub fn show_codes(){
639
let elements: Vec<database_loader::OTPElement> = database_loader::read_from_file();
@@ -27,4 +60,19 @@ fn get_good_otp_code(element: &database_loader::OTPElement) -> String {
2760
s_otp = String::from("0") + &s_otp;
2861
}
2962
s_otp
63+
}
64+
65+
pub fn get_json_results() -> String{
66+
let elements: Vec<database_loader::OTPElement> = database_loader::read_from_file();
67+
68+
let mut results: Vec<JsonResult> = Vec::new();
69+
70+
for i in 0..elements.len() {
71+
let otp_code = get_good_otp_code(&elements[i]);
72+
results.push(JsonResult::new(i+1,elements[i].issuer(),elements[i].label(),otp_code))
73+
}
74+
75+
let json_string: &str = &serde_json::to_string_pretty(&elements).unwrap();
76+
77+
json_string.to_string()
3078
}

0 commit comments

Comments
 (0)