From 469f28f65d26940362c85b8470ff71ab3abcfc98 Mon Sep 17 00:00:00 2001 From: kjuulh Date: Tue, 30 Apr 2024 20:18:54 +0200 Subject: [PATCH] refactor: move subcommands aside Signed-off-by: kjuulh --- crates/hyperlog/src/main.rs | 72 +++++++++++++++++++++++-------------- 1 file changed, 46 insertions(+), 26 deletions(-) diff --git a/crates/hyperlog/src/main.rs b/crates/hyperlog/src/main.rs index 2ba29c2..222d19b 100644 --- a/crates/hyperlog/src/main.rs +++ b/crates/hyperlog/src/main.rs @@ -34,6 +34,21 @@ enum Commands { host: SocketAddr, }, + Exec { + #[command(subcommand)] + commands: ExecCommands, + }, + + Query { + #[command(subcommand)] + commands: QueryCommands, + }, + + Info {}, +} + +#[derive(Subcommand)] +enum ExecCommands { CreateRoot { #[arg(long = "root")] root: String, @@ -46,7 +61,10 @@ enum Commands { #[arg(long = "path")] path: Option, }, +} +#[derive(Subcommand)] +enum QueryCommands { Get { #[arg(long = "root")] root: String, @@ -54,8 +72,6 @@ enum Commands { #[arg(long = "path")] path: Option, }, - - Info {}, } #[tokio::main] @@ -100,32 +116,36 @@ async fn main() -> anyhow::Result<()> { .await .unwrap(); } - Some(Commands::CreateRoot { root }) => state - .commander - .execute(commander::Command::CreateRoot { root })?, - Some(Commands::CreateSection { root, path }) => { - state.commander.execute(commander::Command::CreateSection { - root, - path: path - .unwrap_or_default() - .split('.') - .map(|s| s.to_string()) - .filter(|s| !s.is_empty()) - .collect::>(), - })? - } - Some(Commands::Get { root, path }) => { - let res = state.querier.get( - &root, - path.unwrap_or_default() - .split('.') - .filter(|s| !s.is_empty()), - ); + Some(Commands::Exec { commands }) => match commands { + ExecCommands::CreateRoot { root } => state + .commander + .execute(commander::Command::CreateRoot { root })?, + ExecCommands::CreateSection { root, path } => { + state.commander.execute(commander::Command::CreateSection { + root, + path: path + .unwrap_or_default() + .split('.') + .map(|s| s.to_string()) + .filter(|s| !s.is_empty()) + .collect::>(), + })? + } + }, + Some(Commands::Query { commands }) => match commands { + QueryCommands::Get { root, path } => { + let res = state.querier.get( + &root, + path.unwrap_or_default() + .split('.') + .filter(|s| !s.is_empty()), + ); - let output = serde_json::to_string_pretty(&res)?; + let output = serde_json::to_string_pretty(&res)?; - println!("{}", output); - } + println!("{}", output); + } + }, Some(Commands::Info {}) => { println!("graph stored at: {}", state.storage.info()?) }