From 8f91b1410c2bc67e6b3da8a1927da31a9dfb09ab Mon Sep 17 00:00:00 2001 From: Conrad Ludgate Date: Sat, 13 Nov 2021 22:40:24 +0000 Subject: [PATCH] chore: some new linting (#201) * chore: some new linting * chore: some more linting * chore: rustfmt --- atuin-client/src/encryption.rs | 7 ++--- atuin-client/src/import/resh.rs | 2 -- src/command/login.rs | 40 +++++++++++------------------ src/command/mod.rs | 4 ++- src/command/register.rs | 45 +++++++-------------------------- 5 files changed, 29 insertions(+), 69 deletions(-) diff --git a/atuin-client/src/encryption.rs b/atuin-client/src/encryption.rs index ecb8a0c..5721c2f 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 fa55300..c55220c 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 57d9900..c4817a5 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 5b03e36..6a79a32 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 c242e8a..1317746 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)?;