diff options
| author | Conrad Ludgate <conrad.ludgate@truelayer.com> | 2022-04-21 10:12:56 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-04-21 09:12:56 +0000 |
| commit | d57f549855caf8ab90b5ea0ae7cc9445f3abedfc (patch) | |
| tree | 0818ff405a3b697a0ca981d215ceb4dbb30cd15a /src/command/login.rs | |
| parent | Fix SQL cache query (#318) (diff) | |
| download | atuin-d57f549855caf8ab90b5ea0ae7cc9445f3abedfc.zip | |
refactor commands for better separation (#313)
* refactor commands for better separation
* fmt
Diffstat (limited to 'src/command/login.rs')
| -rw-r--r-- | src/command/login.rs | 75 |
1 files changed, 0 insertions, 75 deletions
diff --git a/src/command/login.rs b/src/command/login.rs deleted file mode 100644 index efc9c590..00000000 --- a/src/command/login.rs +++ /dev/null @@ -1,75 +0,0 @@ -use std::io; - -use atuin_common::api::LoginRequest; -use clap::AppSettings; -use clap::Parser; -use eyre::Result; -use tokio::{fs::File, io::AsyncWriteExt}; - -use atuin_client::api_client; -use atuin_client::settings::Settings; - -#[derive(Parser)] -#[clap(setting(AppSettings::DeriveDisplayOrder))] -pub struct Cmd { - #[clap(long, short)] - pub username: Option<String>, - - #[clap(long, short)] - pub password: Option<String>, - - /// The encryption key for your account - #[clap(long, short)] - 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 { - pub async fn run(&self, settings: &Settings) -> Result<()> { - let session_path = atuin_common::utils::data_dir().join("session"); - - if session_path.exists() { - println!( - "You are already logged in! Please run 'atuin logout' if you wish to login again" - ); - - return Ok(()); - } - - let username = or_user_input(&self.username, "username"); - let password = or_user_input(&self.password, "password"); - let key = or_user_input(&self.key, "encryption key"); - - let session = api_client::login( - settings.sync_address.as_str(), - LoginRequest { username, password }, - ) - .await?; - - let session_path = settings.session_path.as_str(); - let mut file = File::create(session_path).await?; - file.write_all(session.session.as_bytes()).await?; - - let key_path = settings.key_path.as_str(); - let mut file = File::create(key_path).await?; - file.write_all(key.as_bytes()).await?; - - println!("Logged in!"); - - Ok(()) - } -} - -pub(super) fn or_user_input(value: &'_ Option<String>, name: &'static str) -> String { - value.clone().unwrap_or_else(|| read_user_input(name)) -} - -fn read_user_input(name: &'static str) -> String { - eprint!("Please enter {}: ", name); - get_input().expect("Failed to read from input") -} |
