diff --git a/crates/hyperlog-tui/src/commands/update_graph.rs b/crates/hyperlog-tui/src/commands/update_graph.rs index 06c6379..93074de 100644 --- a/crates/hyperlog-tui/src/commands/update_graph.rs +++ b/crates/hyperlog-tui/src/commands/update_graph.rs @@ -24,13 +24,8 @@ impl UpdateGraphCommand { let now = std::time::SystemTime::now(); dispatch.send(Msg::GraphUpdated(GraphUpdatedEvent::Initiated)); - match self - .querier - .get_async(&root, path) - .await - .ok_or(anyhow::anyhow!("failed to find path")) - { - Ok(graph) => { + match self.querier.get_async(&root, path).await { + Ok(Some(graph)) => { dispatch.send(Msg::GraphUpdated(GraphUpdatedEvent::Optimistic( graph.clone(), ))); @@ -42,6 +37,9 @@ impl UpdateGraphCommand { dispatch.send(Msg::GraphUpdated(GraphUpdatedEvent::Success(graph))) } + Ok(None) => dispatch.send(Msg::GraphUpdated(GraphUpdatedEvent::Failure( + "graph was not found user root".into(), + ))), Err(e) => dispatch.send(Msg::GraphUpdated(GraphUpdatedEvent::Failure( format!("{e}"), ))), diff --git a/crates/hyperlog-tui/src/components/graph_explorer.rs b/crates/hyperlog-tui/src/components/graph_explorer.rs index f2967ce..81f18d1 100644 --- a/crates/hyperlog-tui/src/components/graph_explorer.rs +++ b/crates/hyperlog-tui/src/components/graph_explorer.rs @@ -114,7 +114,7 @@ impl<'a> GraphExplorer<'a> { .map(|p| p.split('.').collect::>()) .unwrap_or_default(), ) - .await + .await? .ok_or(anyhow::anyhow!("graph should've had an item"))?; self.inner.graph = Some(graph); diff --git a/crates/hyperlog-tui/src/core_state.rs b/crates/hyperlog-tui/src/core_state.rs index 55734c3..a879326 100644 --- a/crates/hyperlog-tui/src/core_state.rs +++ b/crates/hyperlog-tui/src/core_state.rs @@ -13,12 +13,21 @@ pub struct State { pub querier: Querier, } +pub enum Backend { + Local, + Remote, +} + impl State { - pub fn new() -> anyhow::Result { + pub async fn new(backend: Backend) -> anyhow::Result { let storage = Storage::new(); let engine = storage.load()?; let events = Events::default(); let engine = SharedEngine::from(engine); + let querier = match backend { + Backend::Local => Querier::local(&engine), + Backend::Remote => Querier::remote().await?, + }; Ok(Self { engine: engine.clone(), @@ -26,7 +35,7 @@ impl State { events: events.clone(), commander: Commander::new(engine.clone(), storage, events)?, - querier: Querier::local(&engine), + querier, }) } } diff --git a/crates/hyperlog-tui/src/querier.rs b/crates/hyperlog-tui/src/querier.rs index 0eb297b..32cdbab 100644 --- a/crates/hyperlog-tui/src/querier.rs +++ b/crates/hyperlog-tui/src/querier.rs @@ -8,6 +8,7 @@ mod remote; #[derive(Clone)] enum QuerierVariant { Local(local::Querier), + Remote(remote::Querier), } #[derive(Clone)] @@ -22,6 +23,12 @@ impl Querier { } } + pub async fn remote() -> anyhow::Result { + Ok(Self { + variant: QuerierVariant::Remote(remote::Querier::new().await?), + }) + } + pub fn get( &self, root: &str, @@ -29,6 +36,7 @@ impl Querier { ) -> Option { match &self.variant { QuerierVariant::Local(querier) => querier.get(root, path), + QuerierVariant::Remote(_) => todo!(), } } @@ -36,15 +44,24 @@ impl Querier { &self, root: &str, path: impl IntoIterator>, - ) -> Option { + ) -> anyhow::Result> { match &self.variant { - QuerierVariant::Local(querier) => querier.get(root, path), + QuerierVariant::Local(querier) => Ok(querier.get(root, path)), + QuerierVariant::Remote(querier) => querier.get(root, path).await, } } pub fn get_available_roots(&self) -> Option> { match &self.variant { QuerierVariant::Local(querier) => querier.get_available_roots(), + QuerierVariant::Remote(_) => todo!(), + } + } + + pub async fn get_available_roots_async(&self) -> anyhow::Result>> { + match &self.variant { + QuerierVariant::Local(querier) => Ok(querier.get_available_roots()), + QuerierVariant::Remote(querier) => Ok(querier.get_available_roots().await), } } } diff --git a/crates/hyperlog-tui/src/querier/remote.rs b/crates/hyperlog-tui/src/querier/remote.rs index 89a7764..a951a38 100644 --- a/crates/hyperlog-tui/src/querier/remote.rs +++ b/crates/hyperlog-tui/src/querier/remote.rs @@ -6,6 +6,7 @@ use itertools::Itertools; use tonic::transport::Channel; #[allow(dead_code)] +#[derive(Clone)] pub struct Querier { channel: Channel, } diff --git a/crates/hyperlog/src/cli.rs b/crates/hyperlog/src/cli.rs index 52841bb..f668d8d 100644 --- a/crates/hyperlog/src/cli.rs +++ b/crates/hyperlog/src/cli.rs @@ -1,13 +1,34 @@ use std::net::SocketAddr; -use clap::{Parser, Subcommand}; -use hyperlog_tui::{commander, core_state::State}; +use clap::{Parser, Subcommand, ValueEnum}; +use hyperlog_tui::{ + commander, + core_state::{Backend, State}, +}; #[derive(Parser)] #[command(author, version, about, long_about = None)] struct Command { #[command(subcommand)] command: Option, + + #[arg(long, default_value = "local")] + backend: BackendArg, +} + +#[derive(ValueEnum, Clone)] +enum BackendArg { + Local, + Remote, +} + +impl From for Backend { + fn from(value: BackendArg) -> Self { + match value { + BackendArg::Local => Backend::Local, + BackendArg::Remote => Backend::Remote, + } + } } #[derive(Subcommand)] @@ -73,6 +94,8 @@ pub async fn execute() -> anyhow::Result<()> { tracing_subscriber::fmt::init(); } + let backend = cli.backend; + match cli.command { #[cfg(feature = "include_server")] Some(Commands::Serve { @@ -90,7 +113,7 @@ pub async fn execute() -> anyhow::Result<()> { .await?; } Some(Commands::Exec { commands }) => { - let state = State::new()?; + let state = State::new(backend.into()).await?; match commands { ExecCommands::CreateRoot { root } => state .commander @@ -109,7 +132,7 @@ pub async fn execute() -> anyhow::Result<()> { } } Some(Commands::Query { commands }) => { - let state = State::new()?; + let state = State::new(backend.into()).await?; match commands { QueryCommands::Get { root, path } => { let res = state.querier.get( @@ -126,23 +149,23 @@ pub async fn execute() -> anyhow::Result<()> { } } Some(Commands::CreateRoot { name }) => { - let state = State::new()?; + let state = State::new(backend.into()).await?; state .commander .execute(commander::Command::CreateRoot { root: name })?; println!("Root was successfully created, now run:\n\n$ hyperlog"); } Some(Commands::Info {}) => { - let state = State::new()?; + let state = State::new(backend.into()).await?; println!("graph stored at: {}", state.storage.info()?) } Some(Commands::ClearLock {}) => { - let state = State::new()?; + let state = State::new(backend.into()).await?; state.storage.clear_lock_file(); println!("cleared lock file"); } None => { - let state = State::new()?; + let state = State::new(backend.into()).await?; hyperlog_tui::execute(state).await?; } }