Skip to content

Commit

Permalink
Improve program feetback output
Browse files Browse the repository at this point in the history
  • Loading branch information
duckysmacky committed Aug 9, 2024
1 parent 0829d2e commit ca2246b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
3 changes: 3 additions & 0 deletions src/commands/key.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::io;
use crate::log_success;
use crate::storage::keys;

pub struct NewOptions {
Expand All @@ -10,11 +11,13 @@ pub struct DeleteOptions {
}

pub fn new_key(_options: &NewOptions) -> io::Result<()> {
log_success!("Generating a new encryption key");
keys::generate_new_key();
Ok(())
}

pub fn delete_key(_options: &DeleteOptions) -> io::Result<()> {
log_success!("Deleting encryption key");
keys::delete_key();
Ok(())
}
20 changes: 16 additions & 4 deletions src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ use std::collections::VecDeque;
use clap::ArgMatches;
use crate::{log_error, log_success};

pub fn r#box(args: &ArgMatches) {
pub fn r#box(args: &ArgMatches) -> u32 {
let mut total_files: u32 = 0;
let mut file_paths: Vec<PathBuf> = Vec::new();

let options = path::PathOptions {
Expand All @@ -24,13 +25,19 @@ pub fn r#box(args: &ArgMatches) {

for path in file_paths {
match r#box::encrypt(path.as_path(), &mut options) {
Ok(_) => log_success!("Successfully encrypted {:?}", path.file_name().unwrap().to_os_string()),
Ok(_) => {
log_success!("Successfully encrypted {:?}", path.file_name().unwrap().to_os_string());
total_files += 1;
},
Err(err) => log_error!("An error has occurred while trying to encrypt {:?}: {}", path.file_name().unwrap().to_os_string(), err),
}
}

total_files
}

pub fn unbox(args: &ArgMatches) {
pub fn unbox(args: &ArgMatches) -> u32 {
let mut total_files: u32 = 0;
let mut file_paths: Vec<PathBuf> = Vec::new();

let options = path::PathOptions {
Expand All @@ -45,10 +52,15 @@ pub fn unbox(args: &ArgMatches) {

for path in file_paths {
match unbox::decrypt(path.as_path(), &mut options) {
Ok(_) => log_success!("Successfully decrypted {:?}", path.file_name().unwrap().to_os_string()),
Ok(_) => {
log_success!("Successfully decrypted {:?}", path.file_name().unwrap().to_os_string());
total_files += 1;
},
Err(err) => log_error!("An error has occurred while trying to decrypt {:?}: {}", path.file_name().unwrap().to_os_string(), err),
}
}

total_files
}

pub fn key(args: &ArgMatches) {
Expand Down
10 changes: 8 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,34 @@ mod logger;
mod storage;

use std::io::{self};
use std::time::Instant;
use clap::{command, Arg, ArgAction, Command};

fn main() -> io::Result<()> {
let start_time = Instant::now();
let args = get_command().get_matches();

logger::configure_logger(&args);

/* BOX */
if let Some(args) = args.subcommand_matches("box") {
commands::r#box(args)
let file_count = commands::r#box(args);
log_success!("Total files encrypted: {}", file_count);
}

/* UNBOX */
if let Some(args) = args.subcommand_matches("unbox") {
commands::unbox(args)
let file_count = commands::unbox(args);
log_success!("Total files decrypted: {}", file_count);
}

/* KEY */
if let Some(args) = args.subcommand_matches("key") {
commands::key(args)
}

let duration = start_time.elapsed();
log_success!("Time taken: {:.2?}", duration);
Ok(())
}

Expand Down

0 comments on commit ca2246b

Please sign in to comment.