Skip to content

Commit

Permalink
Move password-searching code from commands::add into list::search_and…
Browse files Browse the repository at this point in the history
…_choose_password

Also, make breaking change into list::request_password_index_from_stdin.
Before it was returning 1-based index of a password, as entered by the user.
Now it returnes 0-based index, as layed out in the vector.
  • Loading branch information
yamnikov-oleg committed Aug 1, 2017
1 parent 945eca0 commit 784fd56
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 26 deletions.
29 changes: 6 additions & 23 deletions src/commands/get.rs
Expand Up @@ -48,39 +48,22 @@ pub fn callback_exec(
check_args(matches)?;

let show = matches.opt_present("show");
let query = matches.free[1].clone();

let passwords = store.search_passwords(query.as_str());
if passwords.len() == 0 {
println_stderr!("I can't find any passwords for \"{}\"", query);
return Ok(());
}

if let Some(ref password) =
passwords.iter().find(|p| {
p.name.to_lowercase() == query.to_lowercase()
})
{
confirm_password_retrieved(show, password);
return Ok(());
}
let query = &matches.free[1];

let prompt = format!(
"Which password would you like {} (1 to {}) ? ",
"Which password would you like {}? ",
if show {
"to see"
} else {
"to copy to your clipboard"
},
passwords.len()
);
println_stderr!("");
let index = list::choose_password_in_list(&passwords, list::WITH_NUMBERS, prompt.as_str());
let password = list::search_and_choose_password(
store, query, list::WITH_NUMBERS, &prompt,
).ok_or(1)?;

// This whould never fail, since we've just checked that this password exists
let password = store
.get_password(passwords[index - 1].name.as_str())
.unwrap();
confirm_password_retrieved(show, &password);

Ok(())
Expand All @@ -98,7 +81,7 @@ fn confirm_password_retrieved(show: bool, password: &password::v2::Password) {
println_ok!(
"Hmm, I tried to copy your new password to your clipboard, but \
something went wrong. You can see it with `rooster get '{}' --show`",
password.name
password.name,
);
} else {
println_ok!(
Expand Down
29 changes: 26 additions & 3 deletions src/list.rs
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use password::v2::Password;
use password::v2::{Password, PasswordStore};
use std::io::Write;
use std::io::stdin;

Expand Down Expand Up @@ -72,18 +72,19 @@ fn request_password_index_from_stdin(passwords: &Vec<&Password>, prompt: &str) -
let mut line = String::new();
loop {
println_stderr!("{}", prompt);
println_stderr!("Type in number from 1 to {}", passwords.len());

line.clear();
match stdin().read_line(&mut line) {
Ok(_) => {
match line.trim().parse() {
match line.trim().parse::<usize>() {
Ok(index) => {
if index == 0 || index > passwords.len() {
println_err!("I need a number between 1 and {}. Let's try again:", passwords.len());
continue;
}

return index;
return index - 1;
}
Err(err) => {
println_err!("This isn't a valid number (reason: {}). Let's try again (1 to {}): ", err, passwords.len());
Expand All @@ -107,3 +108,25 @@ pub fn choose_password_in_list(
println_stderr!("");
request_password_index_from_stdin(passwords, prompt)
}

pub fn search_and_choose_password<'a>(
store: &'a PasswordStore,
query: &str,
with_numbers: bool,
prompt: &str,
) -> Option<&'a Password> {
let passwords = store.search_passwords(query);
if passwords.len() == 0 {
println_stderr!("I can't find any passwords for \"{}\"", query);
return None;
}

if let Some(&password) = passwords.iter().find(|p| {
p.name.to_lowercase() == query.to_lowercase()
}) {
return Some(&password)
}

let index = choose_password_in_list(&passwords, with_numbers, prompt);
Some(passwords[index])
}

0 comments on commit 784fd56

Please sign in to comment.