refactor: move subcommands aside

Signed-off-by: kjuulh <contact@kjuulh.io>
This commit is contained in:
Kasper Juul Hermansen 2024-04-30 20:18:54 +02:00
parent 8e4b46d2d7
commit 469f28f65d
Signed by: kjuulh
GPG Key ID: 57B6E1465221F912

View File

@ -34,6 +34,21 @@ enum Commands {
host: SocketAddr, host: SocketAddr,
}, },
Exec {
#[command(subcommand)]
commands: ExecCommands,
},
Query {
#[command(subcommand)]
commands: QueryCommands,
},
Info {},
}
#[derive(Subcommand)]
enum ExecCommands {
CreateRoot { CreateRoot {
#[arg(long = "root")] #[arg(long = "root")]
root: String, root: String,
@ -46,7 +61,10 @@ enum Commands {
#[arg(long = "path")] #[arg(long = "path")]
path: Option<String>, path: Option<String>,
}, },
}
#[derive(Subcommand)]
enum QueryCommands {
Get { Get {
#[arg(long = "root")] #[arg(long = "root")]
root: String, root: String,
@ -54,8 +72,6 @@ enum Commands {
#[arg(long = "path")] #[arg(long = "path")]
path: Option<String>, path: Option<String>,
}, },
Info {},
} }
#[tokio::main] #[tokio::main]
@ -100,32 +116,36 @@ async fn main() -> anyhow::Result<()> {
.await .await
.unwrap(); .unwrap();
} }
Some(Commands::CreateRoot { root }) => state Some(Commands::Exec { commands }) => match commands {
.commander ExecCommands::CreateRoot { root } => state
.execute(commander::Command::CreateRoot { root })?, .commander
Some(Commands::CreateSection { root, path }) => { .execute(commander::Command::CreateRoot { root })?,
state.commander.execute(commander::Command::CreateSection { ExecCommands::CreateSection { root, path } => {
root, state.commander.execute(commander::Command::CreateSection {
path: path root,
.unwrap_or_default() path: path
.split('.') .unwrap_or_default()
.map(|s| s.to_string()) .split('.')
.filter(|s| !s.is_empty()) .map(|s| s.to_string())
.collect::<Vec<String>>(), .filter(|s| !s.is_empty())
})? .collect::<Vec<String>>(),
} })?
Some(Commands::Get { root, path }) => { }
let res = state.querier.get( },
&root, Some(Commands::Query { commands }) => match commands {
path.unwrap_or_default() QueryCommands::Get { root, path } => {
.split('.') let res = state.querier.get(
.filter(|s| !s.is_empty()), &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 {}) => { Some(Commands::Info {}) => {
println!("graph stored at: {}", state.storage.info()?) println!("graph stored at: {}", state.storage.info()?)
} }