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/command/login.rs | |
| parent | Resolve clippy warnings (#187) (diff) | |
| download | atuin-27d3d81afe3021e226b80a2c7734ed9a8c595641.zip | |
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/command/login.rs')
| -rw-r--r-- | src/command/login.rs | 41 |
1 files changed, 35 insertions, 6 deletions
diff --git a/src/command/login.rs b/src/command/login.rs index 9be98e23..57d99009 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!"); |
