feat: allow input of credentials from stdin (#185)

* feat: allow credential input from stdin for registration

This changes the options for register to be optional. If arguments are
not given, the program will ask for them interactively.

* feat: allow credential input from stdin for login

* style: apply cargo fmt
This commit is contained in:
Jannik 2021-09-24 18:24:59 +02:00 committed by GitHub
parent 446ffb88c7
commit 27d3d81afe
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 79 additions and 17 deletions

View file

@ -1,3 +1,4 @@
use std::io;
use std::io::prelude::*; use std::io::prelude::*;
use std::{borrow::Cow, fs::File}; use std::{borrow::Cow, fs::File};
@ -12,13 +13,19 @@ use atuin_client::settings::Settings;
#[structopt(setting(structopt::clap::AppSettings::DeriveDisplayOrder))] #[structopt(setting(structopt::clap::AppSettings::DeriveDisplayOrder))]
pub struct Cmd { pub struct Cmd {
#[structopt(long, short)] #[structopt(long, short)]
pub username: String, pub username: Option<String>,
#[structopt(long, short)] #[structopt(long, short)]
pub password: String, pub password: Option<String>,
#[structopt(long, short, about = "the encryption key for your account")] #[structopt(long, short, about = "the encryption key for your account")]
pub key: String, pub key: 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())
} }
impl Cmd { impl Cmd {
@ -33,11 +40,33 @@ impl Cmd {
return Ok(()); 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 session = api_client::login( let session = api_client::login(
settings.sync_address.as_str(), settings.sync_address.as_str(),
LoginRequest { LoginRequest {
username: Cow::Borrowed(&self.username), username: Cow::Borrowed(&username),
password: Cow::Borrowed(&self.password), password: Cow::Borrowed(&password),
}, },
)?; )?;
@ -47,7 +76,7 @@ impl Cmd {
let key_path = settings.key_path.as_str(); let key_path = settings.key_path.as_str();
let mut file = File::create(key_path)?; let mut file = File::create(key_path)?;
file.write_all(self.key.as_bytes())?; file.write_all(key.as_bytes())?;
println!("Logged in!"); println!("Logged in!");

View file

@ -147,12 +147,7 @@ impl AtuinCmd {
logout::run(); logout::run();
Ok(()) Ok(())
} }
Self::Register(r) => register::run( Self::Register(r) => register::run(&client_settings, r.username, r.email, r.password),
&client_settings,
r.username.as_str(),
r.email.as_str(),
r.password.as_str(),
),
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,4 +1,5 @@
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;
@ -11,17 +12,54 @@ use atuin_client::settings::Settings;
#[structopt(setting(structopt::clap::AppSettings::DeriveDisplayOrder))] #[structopt(setting(structopt::clap::AppSettings::DeriveDisplayOrder))]
pub struct Cmd { pub struct Cmd {
#[structopt(long, short)] #[structopt(long, short)]
pub username: String, pub username: Option<String>,
#[structopt(long, short)] #[structopt(long, short)]
pub email: String, pub email: Option<String>,
#[structopt(long, short)] #[structopt(long, short)]
pub password: String, pub password: Option<String>,
} }
pub fn run(settings: &Settings, username: &str, email: &str, password: &str) -> Result<()> { fn get_input() -> Result<String> {
let session = api_client::register(settings.sync_address.as_str(), username, email, password)?; 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>,
) -> Result<()> {
let username = if let Some(username) = username {
username
} else {
eprint!("Please enter username: ");
get_input().expect("Failed to read username from input")
};
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 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)?;