diff options
| author | Vladislav Stepanov <8uk.8ak@gmail.com> | 2023-04-14 23:18:58 +0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-04-14 20:18:58 +0100 |
| commit | c05d2850420a2c163b8f62c33a6cef7c0ae1ad8d (patch) | |
| tree | 2c44a44eda7e76fa74e78ac1fd02f55c1ed4d804 /src/command/client/sync | |
| parent | Switch to uuidv7 (#864) (diff) | |
| download | atuin-c05d2850420a2c163b8f62c33a6cef7c0ae1ad8d.zip | |
Workspace reorder (#868)
* Try different workspace structure
Move main crate (atuin) to be on the same level with other crates in
this workspace
* extract common dependencies to the workspace definition
* fix base64 v0.21 deprecation warning
* questionable: update deps & fix chrono deprecations
possible panic sites are unchanged, they're just more visible now
* Revert "questionable: update deps & fix chrono deprecations"
This reverts commit 993e60f8dea81a1625a04285a617959ad09a0866.
Diffstat (limited to 'src/command/client/sync')
| -rw-r--r-- | src/command/client/sync/login.rs | 147 | ||||
| -rw-r--r-- | src/command/client/sync/logout.rs | 19 | ||||
| -rw-r--r-- | src/command/client/sync/register.rs | 49 | ||||
| -rw-r--r-- | src/command/client/sync/status.rs | 35 |
4 files changed, 0 insertions, 250 deletions
diff --git a/src/command/client/sync/login.rs b/src/command/client/sync/login.rs deleted file mode 100644 index 6aa2d847..00000000 --- a/src/command/client/sync/login.rs +++ /dev/null @@ -1,147 +0,0 @@ -use std::{io, path::PathBuf}; - -use clap::Parser; -use eyre::{bail, Context, ContextCompat, Result}; -use tokio::{fs::File, io::AsyncWriteExt}; - -use atuin_client::{ - api_client, - encryption::{decode_key, encode_key, new_key, Key}, - settings::Settings, -}; -use atuin_common::api::LoginRequest; -use rpassword::prompt_password; - -#[derive(Parser)] -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 = settings.session_path.as_str(); - - if PathBuf::from(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 key = or_user_input(&self.key, "encryption key [blank to use existing key file]"); - let password = self.password.clone().unwrap_or_else(read_user_password); - - let key_path = settings.key_path.as_str(); - if key.is_empty() { - if PathBuf::from(key_path).exists() { - let bytes = fs_err::read_to_string(key_path) - .context("existing key file couldn't be read")?; - if decode_key(bytes).is_err() { - bail!("the key in existing key file was invalid"); - } - } else { - println!("No key file exists, creating a new"); - let _key = new_key(settings)?; - } - } else { - // try parse the key as a mnemonic... - let key = match bip39::Mnemonic::from_phrase(&key, bip39::Language::English) { - Ok(mnemonic) => encode_key( - Key::from_slice(mnemonic.entropy()) - .context("key was not the correct length")?, - )?, - Err(err) => { - if let Some(err) = err.downcast_ref::<bip39::ErrorKind>() { - match err { - // assume they copied in the base64 key - bip39::ErrorKind::InvalidWord => key, - bip39::ErrorKind::InvalidChecksum => { - bail!("key mnemonic was not valid") - } - bip39::ErrorKind::InvalidKeysize(_) - | bip39::ErrorKind::InvalidWordLength(_) - | bip39::ErrorKind::InvalidEntropyLength(_, _) => { - bail!("key was not the correct length") - } - } - } else { - // unknown error. assume they copied the base64 key - key - } - } - }; - - if decode_key(key.clone()).is_err() { - bail!("the specified key was invalid"); - } - - let mut file = File::create(key_path).await?; - file.write_all(key.as_bytes()).await?; - } - - 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?; - - 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)) -} - -pub(super) fn read_user_password() -> String { - let password = prompt_password("Please enter password: "); - password.expect("Failed to read from input") -} - -fn read_user_input(name: &'static str) -> String { - eprint!("Please enter {name}: "); - get_input().expect("Failed to read from input") -} - -#[cfg(test)] -mod tests { - use atuin_client::encryption::Key; - - #[test] - fn mnemonic_round_trip() { - let key = Key { - 0: [ - 3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 9, 3, 2, 3, 8, 4, 6, 2, 6, 4, 3, 3, 8, 3, - 2, 7, 9, 5, - ], - }; - let phrase = bip39::Mnemonic::from_entropy(&key.0, bip39::Language::English) - .unwrap() - .into_phrase(); - let mnemonic = bip39::Mnemonic::from_phrase(&phrase, bip39::Language::English).unwrap(); - assert_eq!(mnemonic.entropy(), &key.0); - assert_eq!(phrase, "adapt amused able anxiety mother adapt beef gaze amount else seat alcohol cage lottery avoid scare alcohol cactus school avoid coral adjust catch pink"); - } -} diff --git a/src/command/client/sync/logout.rs b/src/command/client/sync/logout.rs deleted file mode 100644 index 90b49d6d..00000000 --- a/src/command/client/sync/logout.rs +++ /dev/null @@ -1,19 +0,0 @@ -use std::path::PathBuf; - -use eyre::{Context, Result}; -use fs_err::remove_file; - -use atuin_client::settings::Settings; - -pub fn run(settings: &Settings) -> Result<()> { - let session_path = settings.session_path.as_str(); - - if PathBuf::from(session_path).exists() { - remove_file(session_path).context("Failed to remove session file")?; - println!("You have logged out!"); - } else { - println!("You are not logged in"); - } - - Ok(()) -} diff --git a/src/command/client/sync/register.rs b/src/command/client/sync/register.rs deleted file mode 100644 index 6b51fac8..00000000 --- a/src/command/client/sync/register.rs +++ /dev/null @@ -1,49 +0,0 @@ -use clap::Parser; -use eyre::Result; -use tokio::{fs::File, io::AsyncWriteExt}; - -use atuin_client::{api_client, settings::Settings}; - -#[derive(Parser)] -pub struct Cmd { - #[clap(long, short)] - pub username: Option<String>, - - #[clap(long, short)] - pub password: Option<String>, - - #[clap(long, short)] - pub email: Option<String>, -} - -impl Cmd { - pub async fn run(self, settings: &Settings) -> Result<()> { - run(settings, &self.username, &self.email, &self.password).await - } -} - -pub async fn run( - settings: &Settings, - username: &Option<String>, - email: &Option<String>, - password: &Option<String>, -) -> Result<()> { - use super::login::or_user_input; - let username = or_user_input(username, "username"); - let email = or_user_input(email, "email"); - let password = password - .clone() - .unwrap_or_else(super::login::read_user_password); - - let session = - api_client::register(settings.sync_address.as_str(), &username, &email, &password).await?; - - let path = settings.session_path.as_str(); - let mut file = File::create(path).await?; - file.write_all(session.session.as_bytes()).await?; - - // Create a new key, and save it to disk - let _key = atuin_client::encryption::new_key(settings)?; - - Ok(()) -} diff --git a/src/command/client/sync/status.rs b/src/command/client/sync/status.rs deleted file mode 100644 index b3e73e8e..00000000 --- a/src/command/client/sync/status.rs +++ /dev/null @@ -1,35 +0,0 @@ -use atuin_client::{ - api_client, database::Database, encryption::load_encoded_key, settings::Settings, -}; -use colored::Colorize; -use eyre::Result; - -pub async fn run(settings: &Settings, db: &impl Database) -> Result<()> { - let client = api_client::Client::new( - &settings.sync_address, - &settings.session_token, - load_encoded_key(settings)?, - )?; - - let status = client.status().await?; - let last_sync = Settings::last_sync()?; - let local_count = db.history_count().await?; - - println!("{}", "[Local]".green()); - - if settings.auto_sync { - println!("Sync frequency: {}", settings.sync_frequency); - println!("Last sync: {last_sync}"); - } - - println!("History count: {local_count}\n"); - - if settings.auto_sync { - println!("{}", "[Remote]".green()); - println!("Address: {}", settings.sync_address); - println!("Username: {}", status.username); - println!("History count: {}", status.count); - } - - Ok(()) -} |
