chore: some new linting (#201)

* chore: some new linting

* chore: some more linting

* chore: rustfmt
This commit is contained in:
Conrad Ludgate 2021-11-13 22:40:24 +00:00 committed by GitHub
parent 27d3d81afe
commit 8f91b1410c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 29 additions and 69 deletions

View file

@ -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");
}
}

View file

@ -73,7 +73,6 @@ pub struct Resh {
file: BufReader<File>,
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,
})
}
}

View file

@ -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<String>, 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")
}

View file

@ -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)?);

View file

@ -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<String>,
}
fn get_input() -> Result<String> {
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<String>,
email: Option<String>,
password: Option<String>,
username: &Option<String>,
email: &Option<String>,
password: &Option<String>,
) -> 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)?;