diff --git a/bayard-cli/src/cli/bulk_delete.rs b/bayard-cli/src/cli/bulk_delete.rs deleted file mode 100644 index 65ff7e5..0000000 --- a/bayard-cli/src/cli/bulk_delete.rs +++ /dev/null @@ -1,12 +0,0 @@ -use clap::ArgMatches; - -use bayard_client::index::client::IndexClient; - -pub fn run_bulk_delete_cli(matches: &ArgMatches) -> Result<(), std::io::Error> { - let server = matches.value_of("SERVER").unwrap(); - let docs = matches.value_of("DOCS").unwrap(); - - let mut index_client = IndexClient::new(server); - - index_client.bulk_delete(docs.to_string()) -} diff --git a/bayard-cli/src/cli/bulk_set.rs b/bayard-cli/src/cli/bulk_set.rs deleted file mode 100644 index fd046fd..0000000 --- a/bayard-cli/src/cli/bulk_set.rs +++ /dev/null @@ -1,12 +0,0 @@ -use clap::ArgMatches; - -use bayard_client::index::client::IndexClient; - -pub fn run_bulk_set_cli(matches: &ArgMatches) -> Result<(), std::io::Error> { - let server = matches.value_of("SERVER").unwrap(); - let docs = matches.value_of("DOCS").unwrap(); - - let mut index_client = IndexClient::new(server); - - index_client.bulk_set(docs.to_string()) -} diff --git a/bayard-cli/src/cli/root.rs b/bayard-cli/src/cli/command.rs similarity index 93% rename from bayard-cli/src/cli/root.rs rename to bayard-cli/src/cli/command.rs index 70a5f49..204d2ef 100644 --- a/bayard-cli/src/cli/root.rs +++ b/bayard-cli/src/cli/command.rs @@ -1,19 +1,10 @@ use clap::{App, AppSettings, Arg, SubCommand}; -use crate::cli::bulk_delete::run_bulk_delete_cli; -use crate::cli::bulk_set::run_bulk_set_cli; -use crate::cli::commit::run_commit_cli; -use crate::cli::delete::run_delete_cli; -use crate::cli::get::run_get_cli; -use crate::cli::leave::run_leave_cli; -use crate::cli::merge::run_merge_cli; -use crate::cli::rollback::run_rollback_cli; -use crate::cli::schema::run_schema_cli; -use crate::cli::search::run_search_cli; -use crate::cli::set::run_set_cli; -use crate::cli::status::run_status_cli; +use crate::cli::subcommand::{ + bulk_delete, bulk_set, commit, delete, get, leave, merge, rollback, schema, search, set, status, +}; -pub fn run_root_cli() -> Result<(), std::io::Error> { +pub fn root() -> Result<(), std::io::Error> { let app = App::new(crate_name!()) .setting(AppSettings::DeriveDisplayOrder) .setting(AppSettings::SubcommandRequiredElseHelp) @@ -366,18 +357,18 @@ pub fn run_root_cli() -> Result<(), std::io::Error> { let (subcommand, some_options) = app.subcommand(); let options = some_options.unwrap(); let run_cli = match subcommand { - "leave" => run_leave_cli, - "search" => run_search_cli, - "get" => run_get_cli, - "set" => run_set_cli, - "delete" => run_delete_cli, - "bulk-set" => run_bulk_set_cli, - "bulk-delete" => run_bulk_delete_cli, - "commit" => run_commit_cli, - "rollback" => run_rollback_cli, - "merge" => run_merge_cli, - "schema" => run_schema_cli, - "status" => run_status_cli, + "leave" => leave, + "search" => search, + "get" => get, + "set" => set, + "delete" => delete, + "bulk-set" => bulk_set, + "bulk-delete" => bulk_delete, + "commit" => commit, + "rollback" => rollback, + "merge" => merge, + "schema" => schema, + "status" => status, _ => panic!("Subcommand {} is unknown", subcommand), }; diff --git a/bayard-cli/src/cli/commit.rs b/bayard-cli/src/cli/commit.rs deleted file mode 100644 index 2d04964..0000000 --- a/bayard-cli/src/cli/commit.rs +++ /dev/null @@ -1,11 +0,0 @@ -use clap::ArgMatches; - -use bayard_client::index::client::IndexClient; - -pub fn run_commit_cli(matches: &ArgMatches) -> Result<(), std::io::Error> { - let server = matches.value_of("SERVER").unwrap(); - - let mut index_client = IndexClient::new(server); - - index_client.commit() -} diff --git a/bayard-cli/src/cli/delete.rs b/bayard-cli/src/cli/delete.rs deleted file mode 100644 index 7a486b9..0000000 --- a/bayard-cli/src/cli/delete.rs +++ /dev/null @@ -1,12 +0,0 @@ -use clap::ArgMatches; - -use bayard_client::index::client::IndexClient; - -pub fn run_delete_cli(matches: &ArgMatches) -> Result<(), std::io::Error> { - let server = matches.value_of("SERVER").unwrap(); - let id = matches.value_of("ID").unwrap(); - - let mut index_client = IndexClient::new(server); - - index_client.delete(id.to_string()) -} diff --git a/bayard-cli/src/cli/get.rs b/bayard-cli/src/cli/get.rs deleted file mode 100644 index aca55d5..0000000 --- a/bayard-cli/src/cli/get.rs +++ /dev/null @@ -1,21 +0,0 @@ -use clap::ArgMatches; - -use bayard_client::index::client::IndexClient; - -pub fn run_get_cli(matches: &ArgMatches) -> Result<(), std::io::Error> { - let server = matches.value_of("SERVER").unwrap(); - let id = matches.value_of("ID").unwrap(); - - let mut index_client = IndexClient::new(server); - - match index_client.get(id.to_string()) { - Ok(v) => { - println!("{}", v); - Ok(()) - } - Err(e) => { - println!("{}", e); - Err(e) - } - } -} diff --git a/bayard-cli/src/cli/leave.rs b/bayard-cli/src/cli/leave.rs deleted file mode 100644 index a2e1de1..0000000 --- a/bayard-cli/src/cli/leave.rs +++ /dev/null @@ -1,21 +0,0 @@ -use clap::ArgMatches; - -use bayard_client::raft::client::RaftClient; - -pub fn run_leave_cli(matches: &ArgMatches) -> Result<(), std::io::Error> { - let server = matches.value_of("SERVER").unwrap(); - let id = matches.value_of("ID").unwrap().parse::().unwrap(); - - let mut raft_client = RaftClient::new(server); - - match raft_client.leave(id) { - Ok(v) => { - println!("{}", serde_json::to_string(&v).unwrap()); - Ok(()) - } - Err(e) => { - println!("{}", e); - Err(e) - } - } -} diff --git a/bayard-cli/src/cli/merge.rs b/bayard-cli/src/cli/merge.rs deleted file mode 100644 index d5f62cf..0000000 --- a/bayard-cli/src/cli/merge.rs +++ /dev/null @@ -1,11 +0,0 @@ -use clap::ArgMatches; - -use bayard_client::index::client::IndexClient; - -pub fn run_merge_cli(matches: &ArgMatches) -> Result<(), std::io::Error> { - let server = matches.value_of("SERVER").unwrap(); - - let mut index_client = IndexClient::new(server); - - index_client.merge() -} diff --git a/bayard-cli/src/cli/mod.rs b/bayard-cli/src/cli/mod.rs index dfdf9a9..54ae6e0 100644 --- a/bayard-cli/src/cli/mod.rs +++ b/bayard-cli/src/cli/mod.rs @@ -1,13 +1,2 @@ -pub mod bulk_delete; -pub mod bulk_set; -pub mod commit; -pub mod delete; -pub mod get; -pub mod leave; -pub mod merge; -pub mod rollback; -pub mod root; -pub mod schema; -pub mod search; -pub mod set; -pub mod status; +pub mod command; +pub mod subcommand; diff --git a/bayard-cli/src/cli/rollback.rs b/bayard-cli/src/cli/rollback.rs deleted file mode 100644 index c4f5d4d..0000000 --- a/bayard-cli/src/cli/rollback.rs +++ /dev/null @@ -1,11 +0,0 @@ -use clap::ArgMatches; - -use bayard_client::index::client::IndexClient; - -pub fn run_rollback_cli(matches: &ArgMatches) -> Result<(), std::io::Error> { - let server = matches.value_of("SERVER").unwrap(); - - let mut index_client = IndexClient::new(server); - - index_client.rollback() -} diff --git a/bayard-cli/src/cli/schema.rs b/bayard-cli/src/cli/schema.rs deleted file mode 100644 index b9be895..0000000 --- a/bayard-cli/src/cli/schema.rs +++ /dev/null @@ -1,20 +0,0 @@ -use clap::ArgMatches; - -use bayard_client::index::client::IndexClient; - -pub fn run_schema_cli(matches: &ArgMatches) -> Result<(), std::io::Error> { - let server = matches.value_of("SERVER").unwrap(); - - let mut index_client = IndexClient::new(server); - - match index_client.schema() { - Ok(v) => { - println!("{}", v); - Ok(()) - } - Err(e) => { - println!("{}", e); - Err(e) - } - } -} diff --git a/bayard-cli/src/cli/search.rs b/bayard-cli/src/cli/search.rs deleted file mode 100644 index b1ee2d0..0000000 --- a/bayard-cli/src/cli/search.rs +++ /dev/null @@ -1,40 +0,0 @@ -use clap::ArgMatches; - -use bayard_client::index::client::IndexClient; - -pub fn run_search_cli(matches: &ArgMatches) -> Result<(), std::io::Error> { - let server = matches.value_of("SERVER").unwrap(); - let from = matches.value_of("FROM").unwrap().parse::().unwrap(); - let limit = matches.value_of("LIMIT").unwrap().parse::().unwrap(); - let exclude_count = matches.is_present("EXCLUDE_COUNT"); - let exclude_docs = matches.is_present("EXCLUDE_DOCS"); - let facet_field = matches.value_of("FACET_FIELD").unwrap(); - let mut facet_prefixes: Vec = Vec::new(); - if let Some(_facet_prefixes) = matches.values_of("FACET_PREFIX") { - _facet_prefixes - .map(|s| facet_prefixes.push(s.to_string())) - .count(); - } - let query = matches.value_of("QUERY").unwrap(); - - let mut index_client = IndexClient::new(server); - - match index_client.search( - query, - from, - limit, - exclude_count, - exclude_docs, - facet_field, - facet_prefixes, - ) { - Ok(v) => { - println!("{}", v); - Ok(()) - } - Err(e) => { - println!("{}", e); - Err(e) - } - } -} diff --git a/bayard-cli/src/cli/set.rs b/bayard-cli/src/cli/set.rs deleted file mode 100644 index 5d06583..0000000 --- a/bayard-cli/src/cli/set.rs +++ /dev/null @@ -1,18 +0,0 @@ -use clap::ArgMatches; -use serde_json::Value; - -use bayard_client::index::client::IndexClient; - -pub fn run_set_cli(matches: &ArgMatches) -> Result<(), std::io::Error> { - let server = matches.value_of("SERVER").unwrap(); - let id = matches.value_of("ID").unwrap(); - let fields = matches.value_of("FIELDS").unwrap(); - - let mut doc_json: Value = serde_json::from_str(fields).unwrap(); - doc_json["_id"] = Value::String(id.to_string()); - let doc = serde_json::to_string(&doc_json).unwrap(); - - let mut index_client = IndexClient::new(server); - - index_client.set(doc) -} diff --git a/bayard-cli/src/cli/status.rs b/bayard-cli/src/cli/status.rs deleted file mode 100644 index 550d803..0000000 --- a/bayard-cli/src/cli/status.rs +++ /dev/null @@ -1,20 +0,0 @@ -use clap::ArgMatches; - -use bayard_client::index::client::IndexClient; - -pub fn run_status_cli(matches: &ArgMatches) -> Result<(), std::io::Error> { - let server = matches.value_of("SERVER").unwrap(); - - let mut index_client = IndexClient::new(server); - - match index_client.status() { - Ok(v) => { - println!("{}", v); - Ok(()) - } - Err(e) => { - println!("{}", e); - Err(e) - } - } -} diff --git a/bayard-cli/src/cli/subcommand.rs b/bayard-cli/src/cli/subcommand.rs new file mode 100644 index 0000000..d16deb4 --- /dev/null +++ b/bayard-cli/src/cli/subcommand.rs @@ -0,0 +1,177 @@ +use clap::ArgMatches; +use serde_json::Value; + +use bayard_client::index::client::IndexClient; +use bayard_client::raft::client::RaftClient; + +pub fn leave(matches: &ArgMatches) -> Result<(), std::io::Error> { + let server = matches.value_of("SERVER").unwrap(); + let id = matches.value_of("ID").unwrap().parse::().unwrap(); + + let mut raft_client = RaftClient::new(server); + + match raft_client.leave(id) { + Ok(v) => { + println!("{}", serde_json::to_string(&v).unwrap()); + Ok(()) + } + Err(e) => { + println!("{}", e); + Err(e) + } + } +} + +pub fn status(matches: &ArgMatches) -> Result<(), std::io::Error> { + let server = matches.value_of("SERVER").unwrap(); + + let mut index_client = IndexClient::new(server); + + match index_client.status() { + Ok(v) => { + println!("{}", v); + Ok(()) + } + Err(e) => { + println!("{}", e); + Err(e) + } + } +} + +pub fn schema(matches: &ArgMatches) -> Result<(), std::io::Error> { + let server = matches.value_of("SERVER").unwrap(); + + let mut index_client = IndexClient::new(server); + + match index_client.schema() { + Ok(v) => { + println!("{}", v); + Ok(()) + } + Err(e) => { + println!("{}", e); + Err(e) + } + } +} + +pub fn get(matches: &ArgMatches) -> Result<(), std::io::Error> { + let server = matches.value_of("SERVER").unwrap(); + let id = matches.value_of("ID").unwrap(); + + let mut index_client = IndexClient::new(server); + + match index_client.get(id.to_string()) { + Ok(v) => { + println!("{}", v); + Ok(()) + } + Err(e) => { + println!("{}", e); + Err(e) + } + } +} + +pub fn search(matches: &ArgMatches) -> Result<(), std::io::Error> { + let server = matches.value_of("SERVER").unwrap(); + let from = matches.value_of("FROM").unwrap().parse::().unwrap(); + let limit = matches.value_of("LIMIT").unwrap().parse::().unwrap(); + let exclude_count = matches.is_present("EXCLUDE_COUNT"); + let exclude_docs = matches.is_present("EXCLUDE_DOCS"); + let facet_field = matches.value_of("FACET_FIELD").unwrap(); + let mut facet_prefixes: Vec = Vec::new(); + if let Some(_facet_prefixes) = matches.values_of("FACET_PREFIX") { + _facet_prefixes + .map(|s| facet_prefixes.push(s.to_string())) + .count(); + } + let query = matches.value_of("QUERY").unwrap(); + + let mut index_client = IndexClient::new(server); + + match index_client.search( + query, + from, + limit, + exclude_count, + exclude_docs, + facet_field, + facet_prefixes, + ) { + Ok(v) => { + println!("{}", v); + Ok(()) + } + Err(e) => { + println!("{}", e); + Err(e) + } + } +} + +pub fn set(matches: &ArgMatches) -> Result<(), std::io::Error> { + let server = matches.value_of("SERVER").unwrap(); + let id = matches.value_of("ID").unwrap(); + let fields = matches.value_of("FIELDS").unwrap(); + + let mut doc_json: Value = serde_json::from_str(fields).unwrap(); + doc_json["_id"] = Value::String(id.to_string()); + let doc = serde_json::to_string(&doc_json).unwrap(); + + let mut index_client = IndexClient::new(server); + + index_client.set(doc) +} + +pub fn delete(matches: &ArgMatches) -> Result<(), std::io::Error> { + let server = matches.value_of("SERVER").unwrap(); + let id = matches.value_of("ID").unwrap(); + + let mut index_client = IndexClient::new(server); + + index_client.delete(id.to_string()) +} + +pub fn bulk_set(matches: &ArgMatches) -> Result<(), std::io::Error> { + let server = matches.value_of("SERVER").unwrap(); + let docs = matches.value_of("DOCS").unwrap(); + + let mut index_client = IndexClient::new(server); + + index_client.bulk_set(docs.to_string()) +} + +pub fn bulk_delete(matches: &ArgMatches) -> Result<(), std::io::Error> { + let server = matches.value_of("SERVER").unwrap(); + let docs = matches.value_of("DOCS").unwrap(); + + let mut index_client = IndexClient::new(server); + + index_client.bulk_delete(docs.to_string()) +} + +pub fn commit(matches: &ArgMatches) -> Result<(), std::io::Error> { + let server = matches.value_of("SERVER").unwrap(); + + let mut index_client = IndexClient::new(server); + + index_client.commit() +} + +pub fn rollback(matches: &ArgMatches) -> Result<(), std::io::Error> { + let server = matches.value_of("SERVER").unwrap(); + + let mut index_client = IndexClient::new(server); + + index_client.rollback() +} + +pub fn merge(matches: &ArgMatches) -> Result<(), std::io::Error> { + let server = matches.value_of("SERVER").unwrap(); + + let mut index_client = IndexClient::new(server); + + index_client.merge() +} diff --git a/bayard-cli/src/main.rs b/bayard-cli/src/main.rs index 9547191..6856e62 100644 --- a/bayard-cli/src/main.rs +++ b/bayard-cli/src/main.rs @@ -1,8 +1,8 @@ -use bayard_cli::cli::root::run_root_cli; +use bayard_cli::cli::command::root; use bayard_common::log::set_logger; fn main() -> Result<(), std::io::Error> { set_logger(); - run_root_cli() + root() }