Add a v0 kv store I can push to

This commit is contained in:
Ellie Huxtable 2023-06-03 13:24:54 +01:00
parent 1443178c49
commit 64303cbcf0
5 changed files with 84 additions and 1 deletions

16
atuin-client/src/kv.rs Normal file
View File

@ -0,0 +1,16 @@
use eyre::Result;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct KvRecord {
pub key: String,
pub value: String,
}
impl KvRecord {
pub fn serialize(&self) -> Result<Vec<u8>> {
let buf = rmp_serde::to_vec(self)?;
Ok(buf)
}
}

View File

@ -13,6 +13,7 @@ pub mod sync;
pub mod database; pub mod database;
pub mod history; pub mod history;
pub mod import; pub mod import;
pub mod kv;
pub mod ordering; pub mod ordering;
pub mod record; pub mod record;
pub mod settings; pub mod settings;

View File

@ -141,6 +141,7 @@ pub struct Settings {
pub sync_address: String, pub sync_address: String,
pub sync_frequency: String, pub sync_frequency: String,
pub db_path: String, pub db_path: String,
pub record_store_path: String,
pub key_path: String, pub key_path: String,
pub session_path: String, pub session_path: String,
pub search_mode: SearchMode, pub search_mode: SearchMode,
@ -337,11 +338,14 @@ impl Settings {
config_file.push("config.toml"); config_file.push("config.toml");
let db_path = data_dir.join("history.db"); let db_path = data_dir.join("history.db");
let record_store_path = data_dir.join("records.db");
let key_path = data_dir.join("key"); let key_path = data_dir.join("key");
let session_path = data_dir.join("session"); let session_path = data_dir.join("session");
let mut config_builder = Config::builder() let mut config_builder = Config::builder()
.set_default("db_path", db_path.to_str())? .set_default("db_path", db_path.to_str())?
.set_default("record_store_path", record_store_path.to_str())?
.set_default("key_path", key_path.to_str())? .set_default("key_path", key_path.to_str())?
.set_default("session_path", session_path.to_str())? .set_default("session_path", session_path.to_str())?
.set_default("dialect", "us")? .set_default("dialect", "us")?

View File

@ -3,7 +3,7 @@ use std::path::PathBuf;
use clap::Subcommand; use clap::Subcommand;
use eyre::{Result, WrapErr}; use eyre::{Result, WrapErr};
use atuin_client::{database::Sqlite, settings::Settings}; use atuin_client::{database::Sqlite, record::sqlite_store::SqliteStore, settings::Settings};
use env_logger::Builder; use env_logger::Builder;
#[cfg(feature = "sync")] #[cfg(feature = "sync")]
@ -14,6 +14,7 @@ mod account;
mod history; mod history;
mod import; mod import;
mod kv;
mod search; mod search;
mod stats; mod stats;
@ -40,6 +41,9 @@ pub enum Cmd {
#[cfg(feature = "sync")] #[cfg(feature = "sync")]
Account(account::Cmd), Account(account::Cmd),
#[command(subcommand)]
Kv(kv::Cmd),
} }
impl Cmd { impl Cmd {
@ -53,7 +57,10 @@ impl Cmd {
let mut settings = Settings::new().wrap_err("could not load client settings")?; let mut settings = Settings::new().wrap_err("could not load client settings")?;
let db_path = PathBuf::from(settings.db_path.as_str()); let db_path = PathBuf::from(settings.db_path.as_str());
let record_store_path = PathBuf::from(settings.record_store_path.as_str());
let mut db = Sqlite::new(db_path).await?; let mut db = Sqlite::new(db_path).await?;
let mut store = SqliteStore::new(record_store_path).await?;
match self { match self {
Self::History(history) => history.run(&settings, &mut db).await, Self::History(history) => history.run(&settings, &mut db).await,
@ -66,6 +73,8 @@ impl Cmd {
#[cfg(feature = "sync")] #[cfg(feature = "sync")]
Self::Account(account) => account.run(settings).await, Self::Account(account) => account.run(settings).await,
Self::Kv(kv) => kv.run(&settings, &mut store).await,
} }
} }
} }

View File

@ -0,0 +1,53 @@
use clap::Subcommand;
use eyre::Result;
use serde::{Deserialize, Serialize};
use atuin_client::{kv::KvRecord, record::store::Store, settings::Settings};
#[derive(Subcommand)]
#[command(infer_subcommands = true)]
pub enum Cmd {
// atuin kv set foo bar bar
Set {
#[arg(long, short)]
key: String,
value: String,
},
// atuin kv get foo => bar baz
Get {
key: String,
},
}
impl Cmd {
pub async fn run(&self, settings: &Settings, store: &mut impl Store) -> Result<()> {
let kv_version = "v0";
let kv_tag = "kv";
let host_id = Settings::host_id().expect("failed to get host_id");
match self {
Self::Set { key, value } => {
let record = KvRecord {
key: key.to_string(),
value: value.to_string(),
};
let bytes = record.serialize()?;
let record = atuin_common::record::Record::new(
host_id,
kv_version.to_string(),
kv_tag.to_string(),
bytes,
);
store.push(record).await?;
Ok(())
}
Self::Get { key } => Ok(()),
}
}
}