From c33bc136b45246974496a304328caaaa8aa910bf Mon Sep 17 00:00:00 2001 From: Yilin Chen Date: Tue, 15 Jan 2019 01:51:53 +0800 Subject: [PATCH] Use command line args in examples. (#20) * Initial version of Raw Kv client Note: raw::Client::batch_scan is not implemented yet. Signed-off-by: Xiaoguang Sun * Document most public code. Signed-off-by: Ana Hobden * Reduce pub surface Signed-off-by: Ana Hobden * fmt/lint Signed-off-by: Ana Hobden * Add cf to concrete builder types Signed-off-by: Xiaoguang Sun * Fixed some comments and confusing name Signed-off-by: Xiaoguang Sun * Change Request from struct to enum Signed-off-by: Xiaoguang Sun * Change tso_tx/rx channel to bounded Signed-off-by: Xiaoguang Sun * Fix format issues and improve implementations Signed-off-by: Xiaoguang Sun * Change to dyn trait syntax Signed-off-by: Xiaoguang Sun * inline some functions Signed-off-by: Ana Hobden * Better note on KvPair Signed-off-by: Ana Hobden * Use 3 PDs in raw example Signed-off-by: Ana Hobden * Clarify documentation Signed-off-by: Ana Hobden * Get CI green Signed-off-by: Ana Hobden * Remove not useful PrivateKey type Signed-off-by: Xiaoguang Sun * Change CUSTOM_CF to "default" in examples/raw.rs Signed-off-by: Xiaoguang Sun * Use command line args in examples. Signed-off-by: Yilin Chen * Fix the wrong app name in the transactional API example. Signed-off-by: Yilin Chen * Extract duplicate code to a common mod Signed-off-by: Yilin Chen --- Cargo.toml | 1 + examples/common/mod.rs | 65 +++++++++++++++++++++++++++++++++++++++++ examples/raw.rs | 23 ++++++++------- examples/transaction.rs | 21 +++++++++---- 4 files changed, 93 insertions(+), 17 deletions(-) create mode 100644 examples/common/mod.rs diff --git a/Cargo.toml b/Cargo.toml index 078b6a0..2a46c2e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -34,3 +34,4 @@ features = ["push", "process"] [dev-dependencies] tempdir = "0.3" +clap = "2.32" diff --git a/examples/common/mod.rs b/examples/common/mod.rs new file mode 100644 index 0000000..3758059 --- /dev/null +++ b/examples/common/mod.rs @@ -0,0 +1,65 @@ +use clap::{crate_version, App, Arg}; +use std::path::PathBuf; + +pub struct CommandArgs { + pub pd: Vec, + pub ca: Option, + pub cert: Option, + pub key: Option, +} + +pub fn parse_args(app_name: &str) -> CommandArgs { + let matches = App::new(app_name) + .version(crate_version!()) + .author("The TiKV Project Authors") + .arg( + Arg::with_name("pd") + .long("pd") + .aliases(&["pd-endpoint", "pd-endpoints"]) + .value_name("PD_URL") + .help("Sets PD endpoints") + .long_help("Sets PD endpoints. Uses `,` to separate multiple PDs") + .takes_value(true) + .multiple(true) + .value_delimiter(",") + .required(true), + ) + // A cyclic dependency between CA, cert and key is made + // to ensure that no security options are missing. + .arg( + Arg::with_name("ca") + .long("ca") + .value_name("CA_PATH") + .help("Sets the CA") + .long_help("Sets the CA. Must be used with --cert and --key") + .takes_value(true) + .requires("cert"), + ) + .arg( + Arg::with_name("cert") + .long("cert") + .value_name("CERT_PATH") + .help("Sets the certificate") + .long_help("Sets the certificate. Must be used with --ca and --key") + .takes_value(true) + .requires("key"), + ) + .arg( + Arg::with_name("key") + .long("key") + .alias("private-key") + .value_name("KEY_PATH") + .help("Sets the private key") + .long_help("Sets the private key. Must be used with --ca and --cert") + .takes_value(true) + .requires("ca"), + ) + .get_matches(); + + CommandArgs { + pd: matches.values_of("pd").unwrap().map(String::from).collect(), + ca: matches.value_of("ca").map(PathBuf::from), + cert: matches.value_of("cert").map(PathBuf::from), + key: matches.value_of("key").map(PathBuf::from), + } +} diff --git a/examples/raw.rs b/examples/raw.rs index e950188..2fb2981 100644 --- a/examples/raw.rs +++ b/examples/raw.rs @@ -11,26 +11,27 @@ // See the License for the specific language governing permissions and // limitations under the License. +mod common; + +use crate::common::parse_args; use futures::future::Future; -use std::path::PathBuf; use tikv_client::{raw::Client, Config, Key, KvPair, Result, Value}; const KEY: &str = "TiKV"; const VALUE: &str = "Rust"; fn main() -> Result<()> { + // You can try running this example by passing your pd endpoints + // (and SSL options if necessary) through command line arguments. + let args = parse_args("raw"); + // Create a configuration to use for the example. // Optionally encrypt the traffic. - let config = Config::new(vec![ - "192.168.0.100:3379", // Avoid a single point of failure, - "192.168.0.101:3379", // use more than one PD endpoint. - "192.168.0.102:3379", - ]) - .with_security( - PathBuf::from("/path/to/ca.pem"), - PathBuf::from("/path/to/client.pem"), - PathBuf::from("/path/to/client-key.pem"), - ); + let config = if let (Some(ca), Some(cert), Some(key)) = (args.ca, args.cert, args.key) { + Config::new(args.pd).with_security(ca, cert, key) + } else { + Config::new(args.pd) + }; // When we first create a client we recieve a `Connect` structure which must be resolved before // the client is actually connected and usable. diff --git a/examples/transaction.rs b/examples/transaction.rs index f2e7a56..2362470 100644 --- a/examples/transaction.rs +++ b/examples/transaction.rs @@ -11,9 +11,11 @@ // See the License for the specific language governing permissions and // limitations under the License. +mod common; + +use crate::common::parse_args; use futures::{future, Future, Stream}; use std::ops::RangeBounds; -use std::path::PathBuf; use tikv_client::{ transaction::{Client, IsolationLevel}, Config, Key, KvPair, Value, @@ -70,11 +72,18 @@ fn dels(client: &Client, keys: impl IntoIterator) { } fn main() { - let config = Config::new(vec!["127.0.0.1:2379"]).with_security( - PathBuf::from("/path/to/ca.pem"), - PathBuf::from("/path/to/client.pem"), - PathBuf::from("/path/to/client-key.pem"), - ); + // You can try running this example by passing your pd endpoints + // (and SSL options if necessary) through command line arguments. + let args = parse_args("txn"); + + // Create a configuration to use for the example. + // Optionally encrypt the traffic. + let config = if let (Some(ca), Some(cert), Some(key)) = (args.ca, args.cert, args.key) { + Config::new(args.pd).with_security(ca, cert, key) + } else { + Config::new(args.pd) + }; + let txn = Client::new(&config) .wait() .expect("Could not connect to tikv");