diff --git a/atuin-client/src/encryption.rs b/atuin-client/src/encryption.rs index ecb8a0c7..5721c2f9 100644 --- a/atuin-client/src/encryption.rs +++ b/atuin-client/src/encryption.rs @@ -133,14 +133,11 @@ mod test { // test decryption works // this should pass match decrypt(&e1, &key1) { - Err(e) => assert!(false, "failed to decrypt, got {}", e), + Err(e) => panic!("failed to decrypt, got {}", e), Ok(h) => assert_eq!(h, history), }; // this should err - match decrypt(&e2, &key1) { - Ok(_) => assert!(false, "expected an error decrypting with invalid key"), - Err(_) => {} - }; + decrypt(&e2, &key1).expect_err("expected an error decrypting with invalid key"); } } diff --git a/atuin-client/src/import/resh.rs b/atuin-client/src/import/resh.rs index fa55300b..c55220ca 100644 --- a/atuin-client/src/import/resh.rs +++ b/atuin-client/src/import/resh.rs @@ -73,7 +73,6 @@ pub struct Resh { file: BufReader, strbuf: String, loc: usize, - counter: i64, } impl Importer for Resh { @@ -95,7 +94,6 @@ impl Importer for Resh { file: buf, strbuf: String::new(), loc, - counter: 0, }) } } diff --git a/src/command/login.rs b/src/command/login.rs index 57d99009..c4817a5f 100644 --- a/src/command/login.rs +++ b/src/command/login.rs @@ -40,34 +40,13 @@ impl Cmd { return Ok(()); } - // TODO: Maybe get rid of clone - let username = if let Some(username) = self.username.clone() { - username - } else { - eprint!("Please enter username: "); - get_input().expect("Failed to read username from input") - }; - - let password = if let Some(password) = self.password.clone() { - password - } else { - eprint!("Please enter password: "); - get_input().expect("Failed to read email from input") - }; - - let key = if let Some(key) = self.key.clone() { - key - } else { - eprint!("Please enter encryption key: "); - get_input().expect("Failed to read password from input") - }; + let username = or_user_input(&self.username, "username"); + let password = or_user_input(&self.password, "password"); + let key = or_user_input(&self.key, "encryption key"); let session = api_client::login( settings.sync_address.as_str(), - LoginRequest { - username: Cow::Borrowed(&username), - password: Cow::Borrowed(&password), - }, + LoginRequest { username, password }, )?; let session_path = settings.session_path.as_str(); @@ -83,3 +62,14 @@ impl Cmd { Ok(()) } } + +pub(super) fn or_user_input<'a>(value: &'a Option, name: &'static str) -> Cow<'a, str> { + value + .as_deref() + .map_or_else(|| Cow::Owned(read_user_input(name)), Cow::Borrowed) +} + +fn read_user_input(name: &'static str) -> String { + eprint!("Please enter {}: ", name); + get_input().expect("Failed to read from input") +} diff --git a/src/command/mod.rs b/src/command/mod.rs index 5b03e360..6a79a32f 100644 --- a/src/command/mod.rs +++ b/src/command/mod.rs @@ -147,7 +147,9 @@ impl AtuinCmd { logout::run(); Ok(()) } - Self::Register(r) => register::run(&client_settings, r.username, r.email, r.password), + Self::Register(r) => { + register::run(&client_settings, &r.username, &r.email, &r.password) + } Self::Key => { let key = atuin_client::encryption::load_key(&client_settings)?; println!("{}", atuin_client::encryption::encode_key(key)?); diff --git a/src/command/register.rs b/src/command/register.rs index c242e8ab..13177463 100644 --- a/src/command/register.rs +++ b/src/command/register.rs @@ -1,5 +1,4 @@ use std::fs::File; -use std::io; use std::io::prelude::*; use eyre::Result; @@ -21,45 +20,19 @@ pub struct Cmd { pub password: Option, } -fn get_input() -> Result { - let mut input = String::new(); - io::stdin().read_line(&mut input)?; - Ok(input.trim_end_matches(&['\r', '\n'][..]).to_string()) -} - pub fn run( settings: &Settings, - username: Option, - email: Option, - password: Option, + username: &Option, + email: &Option, + password: &Option, ) -> Result<()> { - let username = if let Some(username) = username { - username - } else { - eprint!("Please enter username: "); - get_input().expect("Failed to read username from input") - }; + use super::login::or_user_input; + let username = or_user_input(username, "username"); + let email = or_user_input(email, "email"); + let password = or_user_input(password, "password"); - let email = if let Some(email) = email { - email - } else { - eprint!("Please enter email: "); - get_input().expect("Failed to read email from input") - }; - - let password = if let Some(password) = password { - password - } else { - eprint!("Please enter password: "); - get_input().expect("Failed to read password from input") - }; - - let session = api_client::register( - settings.sync_address.as_str(), - username.as_str(), - email.as_str(), - password.as_str(), - )?; + let session = + api_client::register(settings.sync_address.as_str(), &username, &email, &password)?; let path = settings.session_path.as_str(); let mut file = File::create(path)?;