diff options
author | Jannik <jannik.peters@posteo.de> | 2021-09-24 18:24:59 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-09-24 16:24:59 +0000 |
commit | 27d3d81afe3021e226b80a2c7734ed9a8c595641 (patch) | |
tree | 577b6cf9f1db033c3a68124d6a5ce01d0a8d7a83 /src | |
parent | 446ffb88c7b67d61d41be084fa724f84fa055e22 (diff) |
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
Diffstat (limited to 'src')
-rw-r--r-- | src/command/login.rs | 41 | ||||
-rw-r--r-- | src/command/mod.rs | 7 | ||||
-rw-r--r-- | src/command/register.rs | 48 |
3 files changed, 79 insertions, 17 deletions
diff --git a/src/command/login.rs b/src/command/login.rs index 9be98e23a..57d990097 100644 --- a/src/command/login.rs +++ b/src/command/login.rs @@ -1,3 +1,4 @@ +use std::io; use std::io::prelude::*; use std::{borrow::Cow, fs::File}; @@ -12,13 +13,19 @@ use atuin_client::settings::Settings; #[structopt(setting(structopt::clap::AppSettings::DeriveDisplayOrder))] pub struct Cmd { #[structopt(long, short)] - pub username: String, + pub username: Option<String>, #[structopt(long, short)] - pub password: String, + pub password: Option<String>, #[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 { @@ -33,11 +40,33 @@ 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 session = api_client::login( settings.sync_address.as_str(), LoginRequest { - username: Cow::Borrowed(&self.username), - password: Cow::Borrowed(&self.password), + username: Cow::Borrowed(&username), + password: Cow::Borrowed(&password), }, )?; @@ -47,7 +76,7 @@ impl Cmd { let key_path = settings.key_path.as_str(); let mut file = File::create(key_path)?; - file.write_all(self.key.as_bytes())?; + file.write_all(key.as_bytes())?; println!("Logged in!"); diff --git a/src/command/mod.rs b/src/command/mod.rs index f959fc986..5b03e3609 100644 --- a/src/command/mod.rs +++ b/src/command/mod.rs @@ -147,12 +147,7 @@ impl AtuinCmd { logout::run(); Ok(()) } - Self::Register(r) => register::run( - &client_settings, - r.username.as_str(), - r.email.as_str(), - r.password.as_str(), - ), + 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 7c38f483d..c242e8abf 100644 --- a/src/command/register.rs +++ b/src/command/register.rs @@ -1,4 +1,5 @@ use std::fs::File; +use std::io; use std::io::prelude::*; use eyre::Result; @@ -11,17 +12,54 @@ use atuin_client::settings::Settings; #[structopt(setting(structopt::clap::AppSettings::DeriveDisplayOrder))] pub struct Cmd { #[structopt(long, short)] - pub username: String, + pub username: Option<String>, #[structopt(long, short)] - pub email: String, + pub email: Option<String>, #[structopt(long, short)] - pub password: String, + pub password: Option<String>, } -pub fn run(settings: &Settings, username: &str, email: &str, password: &str) -> Result<()> { - let session = api_client::register(settings.sync_address.as_str(), username, email, password)?; +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>, +) -> 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 mut file = File::create(path)?; |