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 // test decryption works
// this should pass // this should pass
match decrypt(&e1, &key1) { 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), Ok(h) => assert_eq!(h, history),
}; };
// this should err // this should err
match decrypt(&e2, &key1) { decrypt(&e2, &key1).expect_err("expected an error decrypting with invalid key");
Ok(_) => assert!(false, "expected an error decrypting with invalid key"),
Err(_) => {}
};
} }
} }

View file

@ -73,7 +73,6 @@ pub struct Resh {
file: BufReader<File>, file: BufReader<File>,
strbuf: String, strbuf: String,
loc: usize, loc: usize,
counter: i64,
} }
impl Importer for Resh { impl Importer for Resh {
@ -95,7 +94,6 @@ impl Importer for Resh {
file: buf, file: buf,
strbuf: String::new(), strbuf: String::new(),
loc, loc,
counter: 0,
}) })
} }
} }

View file

@ -40,34 +40,13 @@ impl Cmd {
return Ok(()); return Ok(());
} }
// TODO: Maybe get rid of clone let username = or_user_input(&self.username, "username");
let username = if let Some(username) = self.username.clone() { let password = or_user_input(&self.password, "password");
username let key = or_user_input(&self.key, "encryption key");
} 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 session = api_client::login( let session = api_client::login(
settings.sync_address.as_str(), settings.sync_address.as_str(),
LoginRequest { LoginRequest { username, password },
username: Cow::Borrowed(&username),
password: Cow::Borrowed(&password),
},
)?; )?;
let session_path = settings.session_path.as_str(); let session_path = settings.session_path.as_str();
@ -83,3 +62,14 @@ impl Cmd {
Ok(()) 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(); logout::run();
Ok(()) 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 => { Self::Key => {
let key = atuin_client::encryption::load_key(&client_settings)?; let key = atuin_client::encryption::load_key(&client_settings)?;
println!("{}", atuin_client::encryption::encode_key(key)?); println!("{}", atuin_client::encryption::encode_key(key)?);

View file

@ -1,5 +1,4 @@
use std::fs::File; use std::fs::File;
use std::io;
use std::io::prelude::*; use std::io::prelude::*;
use eyre::Result; use eyre::Result;
@ -21,45 +20,19 @@ pub struct Cmd {
pub password: Option<String>, 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( pub fn run(
settings: &Settings, settings: &Settings,
username: Option<String>, username: &Option<String>,
email: Option<String>, email: &Option<String>,
password: Option<String>, password: &Option<String>,
) -> Result<()> { ) -> Result<()> {
let username = if let Some(username) = username { use super::login::or_user_input;
username let username = or_user_input(username, "username");
} else { let email = or_user_input(email, "email");
eprint!("Please enter username: "); let password = or_user_input(password, "password");
get_input().expect("Failed to read username from input")
};
let email = if let Some(email) = email { let session =
email api_client::register(settings.sync_address.as_str(), &username, &email, &password)?;
} 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 path = settings.session_path.as_str(); let path = settings.session_path.as_str();
let mut file = File::create(path)?; let mut file = File::create(path)?;