From eadce83a573c0a0442426abccc8af83a1a10e3ce Mon Sep 17 00:00:00 2001 From: Ellie Huxtable Date: Sun, 3 Dec 2023 13:03:56 +0000 Subject: [PATCH] format, lint, add status command --- atuin-client/src/api_client.rs | 2 +- atuin-client/src/history.rs | 5 ++- atuin-client/src/history/store.rs | 3 -- atuin-client/src/record/sync.rs | 50 +++++++++++++---------------- atuin-server-database/src/lib.rs | 2 +- atuin-server-postgres/src/lib.rs | 8 ++--- atuin-server/src/handlers/record.rs | 2 +- atuin/src/command/client.rs | 2 +- atuin/src/command/client/history.rs | 2 +- atuin/src/command/client/record.rs | 23 +++++++++++-- atuin/src/command/client/sync.rs | 2 +- 11 files changed, 55 insertions(+), 46 deletions(-) diff --git a/atuin-client/src/api_client.rs b/atuin-client/src/api_client.rs index 7306546d..359b0464 100644 --- a/atuin-client/src/api_client.rs +++ b/atuin-client/src/api_client.rs @@ -8,7 +8,7 @@ use reqwest::{ StatusCode, Url, }; -use atuin_common::record::{EncryptedData, HostId, Record, RecordId, RecordIdx}; +use atuin_common::record::{EncryptedData, HostId, Record, RecordIdx}; use atuin_common::{ api::{ AddHistoryRequest, CountResponse, DeleteHistoryRequest, ErrorResponse, IndexResponse, diff --git a/atuin-client/src/history.rs b/atuin-client/src/history.rs index c75c6375..180e1c88 100644 --- a/atuin-client/src/history.rs +++ b/atuin-client/src/history.rs @@ -1,9 +1,8 @@ - -use rmp::decode::{ValueReadError}; +use rmp::decode::ValueReadError; use rmp::{decode::Bytes, Marker}; use std::env; -use atuin_common::record::{DecryptedData}; +use atuin_common::record::DecryptedData; use atuin_common::utils::uuid_v7; use eyre::{bail, eyre, Result}; diff --git a/atuin-client/src/history/store.rs b/atuin-client/src/history/store.rs index 3c96afc1..382d39d5 100644 --- a/atuin-client/src/history/store.rs +++ b/atuin-client/src/history/store.rs @@ -1,7 +1,4 @@ - - use eyre::Result; -use serde::{Serialize}; use crate::record::{encryption::PASETO_V4, sqlite_store::SqliteStore, store::Store}; use atuin_common::record::{Host, HostId, Record}; diff --git a/atuin-client/src/record/sync.rs b/atuin-client/src/record/sync.rs index dac5f5d4..bc426787 100644 --- a/atuin-client/src/record/sync.rs +++ b/atuin-client/src/record/sync.rs @@ -1,4 +1,6 @@ // do a sync :O +use std::cmp::Ordering; + use eyre::Result; use thiserror::Error; @@ -76,30 +78,24 @@ pub async fn operations( for diff in diffs { let op = match (diff.local, diff.remote) { // We both have it! Could be either. Compare. - (Some(local), Some(remote)) => { - if local == remote { - // between the diff and now, a sync has somehow occured. - // regardless, no work is needed! - Operation::Noop { - host: diff.host, - tag: diff.tag, - } - } else if local > remote { - Operation::Upload { - local, - remote: Some(remote), - host: diff.host, - tag: diff.tag, - } - } else { - Operation::Download { - local: Some(local), - remote, - host: diff.host, - tag: diff.tag, - } - } - } + (Some(local), Some(remote)) => match local.cmp(&remote) { + Ordering::Equal => Operation::Noop { + host: diff.host, + tag: diff.tag, + }, + Ordering::Greater => Operation::Upload { + local, + remote: Some(remote), + host: diff.host, + tag: diff.tag, + }, + Ordering::Less => Operation::Download { + local: Some(local), + remote, + host: diff.host, + tag: diff.tag, + }, + }, // Remote has it, we don't. Gotta be download (None, Some(remote)) => Operation::Download { @@ -162,7 +158,7 @@ async fn sync_upload( println!( "Uploading {} records to {}/{}", expected, - host.0.as_simple().to_string(), + host.0.as_simple(), tag ); @@ -173,7 +169,7 @@ async fn sync_upload( .await .map_err(|_| SyncError::LocalStoreError)?; - let _ = client + client .post_records(&page) .await .map_err(|_| SyncError::RemoteRequestError)?; @@ -206,7 +202,7 @@ async fn sync_download( println!( "Downloading {} records from {}/{}", expected, - host.0.as_simple().to_string(), + host.0.as_simple(), tag ); diff --git a/atuin-server-database/src/lib.rs b/atuin-server-database/src/lib.rs index 23ad540d..9b154ea1 100644 --- a/atuin-server-database/src/lib.rs +++ b/atuin-server-database/src/lib.rs @@ -14,7 +14,7 @@ use self::{ models::{History, NewHistory, NewSession, NewUser, Session, User}, }; use async_trait::async_trait; -use atuin_common::record::{EncryptedData, HostId, Record, RecordId, RecordIdx, RecordStatus}; +use atuin_common::record::{EncryptedData, HostId, Record, RecordIdx, RecordStatus}; use serde::{de::DeserializeOwned, Serialize}; use time::{Date, Duration, Month, OffsetDateTime, Time, UtcOffset}; use tracing::instrument; diff --git a/atuin-server-postgres/src/lib.rs b/atuin-server-postgres/src/lib.rs index b838534a..c1de4d50 100644 --- a/atuin-server-postgres/src/lib.rs +++ b/atuin-server-postgres/src/lib.rs @@ -1,7 +1,7 @@ use std::ops::Range; use async_trait::async_trait; -use atuin_common::record::{EncryptedData, HostId, Record, RecordId, RecordIdx, RecordStatus}; +use atuin_common::record::{EncryptedData, HostId, Record, RecordIdx, RecordStatus}; use atuin_server_database::models::{History, NewHistory, NewSession, NewUser, Session, User}; use atuin_server_database::{Database, DbError, DbResult}; use futures_util::TryStreamExt; @@ -398,8 +398,6 @@ impl Database for Postgres { count: u64, ) -> DbResult>> { tracing::debug!("{:?} - {:?} - {:?}", host, tag, start); - let mut ret = Vec::with_capacity(count as usize); - let mut parent = start; let start = start.unwrap_or(0); let records: Result, DbError> = sqlx::query_as( @@ -433,8 +431,8 @@ impl Database for Postgres { records } Err(DbError::NotFound) => { - tracing::debug!("hit end of store: {:?}/{}", host, tag); - return Ok(ret); + tracing::debug!("no records found in store: {:?}/{}", host, tag); + return Ok(vec![]); } Err(e) => return Err(e), }; diff --git a/atuin-server/src/handlers/record.rs b/atuin-server/src/handlers/record.rs index 473e3206..eae907fe 100644 --- a/atuin-server/src/handlers/record.rs +++ b/atuin-server/src/handlers/record.rs @@ -8,7 +8,7 @@ use super::{ErrorResponse, ErrorResponseStatus, RespExt}; use crate::router::{AppState, UserAuth}; use atuin_server_database::Database; -use atuin_common::record::{EncryptedData, HostId, Record, RecordId, RecordIdx, RecordStatus}; +use atuin_common::record::{EncryptedData, HostId, Record, RecordIdx, RecordStatus}; #[instrument(skip_all, fields(user.id = user.id))] pub async fn post( diff --git a/atuin/src/command/client.rs b/atuin/src/command/client.rs index 7a6559d2..a77fb480 100644 --- a/atuin/src/command/client.rs +++ b/atuin/src/command/client.rs @@ -87,7 +87,7 @@ impl Cmd { Self::Kv(kv) => kv.run(&settings, &mut store).await, - Self::Record(record) => record.run(&settings, &mut store).await, + Self::Record(record) => record.run(&settings, &store).await, Self::DefaultConfig => { config::run(); diff --git a/atuin/src/command/client/history.rs b/atuin/src/command/client/history.rs index 6cd73716..79aeab49 100644 --- a/atuin/src/command/client/history.rs +++ b/atuin/src/command/client/history.rs @@ -14,7 +14,7 @@ use atuin_client::{ database::{current_context, Database}, encryption, history::{store::HistoryStore, History}, - record::{sqlite_store::SqliteStore, store::Store}, + record::sqlite_store::SqliteStore, settings::Settings, }; diff --git a/atuin/src/command/client/record.rs b/atuin/src/command/client/record.rs index 7089f3d4..2250232c 100644 --- a/atuin/src/command/client/record.rs +++ b/atuin/src/command/client/record.rs @@ -12,10 +12,29 @@ pub enum Cmd { impl Cmd { pub async fn run( &self, - settings: &Settings, - store: &mut (impl Store + Send + Sync), + _settings: &Settings, + store: &(impl Store + Send + Sync), ) -> Result<()> { let host_id = Settings::host_id().expect("failed to get host_id"); + + let status = store.status().await?; + + for (host, store) in &status.hosts { + let host_string = if host == &host_id { + format!("host: {} <- CURRENT HOST", host.0.as_hyphenated()) + } else { + format!("host: {}", host.0.as_hyphenated()) + }; + + println!("{host_string}"); + + for (tag, idx) in store { + println!("\tstore: {tag} at {idx}"); + } + + println!(); + } + Ok(()) } } diff --git a/atuin/src/command/client/sync.rs b/atuin/src/command/client/sync.rs index cdb0f214..dface7ee 100644 --- a/atuin/src/command/client/sync.rs +++ b/atuin/src/command/client/sync.rs @@ -77,7 +77,7 @@ async fn run( db: &impl Database, store: &mut (impl Store + Send + Sync), ) -> Result<()> { - let (diff, remote_index) = sync::diff(settings, store).await?; + let (diff, _) = sync::diff(settings, store).await?; let operations = sync::operations(diff, store).await?; let (uploaded, downloaded) = sync::sync_remote(operations, store, settings).await?;