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:
parent
446ffb88c7
commit
27d3d81afe
3 changed files with 79 additions and 17 deletions
|
@ -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!");
|
||||
|
||||
|
|
|
@ -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)?);
|
||||
|
|
|
@ -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)?;
|
||||
|
|
Loading…
Reference in a new issue