Skip to content

Commit

Permalink
fix: better error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
millotp committed May 28, 2024
1 parent 39edfce commit 2bfe0f9
Showing 1 changed file with 22 additions and 10 deletions.
32 changes: 22 additions & 10 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ mod client;
mod error;

use std::{
fmt::{self},
fmt,
fs::read_to_string,
io::{stdin, stdout},
};
Expand Down Expand Up @@ -469,7 +469,9 @@ impl Vaultwalker {
KeyCode::Char('u') => {
let entry = &self.current_list[self.selected_item];
if entry.is_dir {
return Ok(());
return Err(Error::Application(
"cannot update a directory, please select a key".to_owned(),
));
}

self.mode = Mode::TypingSecret(EditMode::Update);
Expand All @@ -479,14 +481,22 @@ impl Vaultwalker {
KeyCode::Char('r') => {
let entry = &self.current_list[self.selected_item];
if entry.is_dir {
return Ok(());
return Err(Error::Application(
"cannot rename a directory, please select a key".to_owned(),
));
}

self.mode = Mode::TypingKey(EditMode::Update);

needs_refresh = true;
}
KeyCode::Char('d') => {
let entry = &self.current_list[self.selected_item];
if entry.is_dir {
return Err(Error::Application(
"cannot delete a directory, please select a key".to_owned(),
));
}
self.mode = Mode::DeletingKey;

needs_refresh = true;
Expand Down Expand Up @@ -580,8 +590,8 @@ impl Vaultwalker {
let answer = read_line()?;
// because the user presses on new line, we need to reset it
self.print()?;
self.mode = Mode::Navigation;

let mut result_message = format!("received '{}', the key was not deleted", answer);
if answer == "yes" {
let mut path = self.path.join();
path.push_str(&self.current_list[self.selected_item].name);
Expand All @@ -600,13 +610,15 @@ impl Vaultwalker {
}

self.refresh_all()?;

result_message = format!("deleted the key '{}'", path);
self.print()?;
self.print_info(&format!("deleted the key '{}'", path))
} else {
self.print()?;
self.print_error(Error::Application(format!(
"received '{}', the key was not deleted",
answer
)))
}

self.mode = Mode::Navigation;
self.print()?;
self.print_info(&result_message)
}

fn input_loop(&mut self) -> Result<()> {
Expand Down

0 comments on commit 2bfe0f9

Please sign in to comment.