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 <sunxiaoguang@zhihu.com>

* Document most public code.

Signed-off-by: Ana Hobden <operator@hoverbear.org>

* Reduce pub surface

Signed-off-by: Ana Hobden <operator@hoverbear.org>

* fmt/lint

Signed-off-by: Ana Hobden <operator@hoverbear.org>

* Add cf to concrete builder types

Signed-off-by: Xiaoguang Sun <sunxiaoguang@zhihu.com>

* Fixed some comments and confusing name

Signed-off-by: Xiaoguang Sun <sunxiaoguang@zhihu.com>

* Change Request from struct to enum

Signed-off-by: Xiaoguang Sun <sunxiaoguang@zhihu.com>

* Change tso_tx/rx channel to bounded

Signed-off-by: Xiaoguang Sun <sunxiaoguang@zhihu.com>

* Fix format issues and improve implementations

Signed-off-by: Xiaoguang Sun <sunxiaoguang@zhihu.com>

* Change to dyn trait syntax

Signed-off-by: Xiaoguang Sun <sunxiaoguang@zhihu.com>

* inline some functions

Signed-off-by: Ana Hobden <operator@hoverbear.org>

* Better note on KvPair

Signed-off-by: Ana Hobden <operator@hoverbear.org>

* Use 3 PDs in raw example

Signed-off-by: Ana Hobden <operator@hoverbear.org>

* Clarify documentation

Signed-off-by: Ana Hobden <operator@hoverbear.org>

* Get CI green

Signed-off-by: Ana Hobden <operator@hoverbear.org>

* Remove not useful PrivateKey type

Signed-off-by: Xiaoguang Sun <sunxiaoguang@zhihu.com>

* Change CUSTOM_CF to "default" in examples/raw.rs

Signed-off-by: Xiaoguang Sun <sunxiaoguang@zhihu.com>

* Use command line args in examples.

Signed-off-by: Yilin Chen <sticnarf@gmail.com>

* Fix the wrong app name in the transactional API example.

Signed-off-by: Yilin Chen <sticnarf@gmail.com>

* Extract duplicate code to a common mod

Signed-off-by: Yilin Chen <sticnarf@gmail.com>
This commit is contained in:
Yilin Chen 2019-01-15 01:51:53 +08:00 committed by Ana Hobden
parent 573871c2c6
commit c33bc136b4
4 changed files with 93 additions and 17 deletions

View File

@ -34,3 +34,4 @@ features = ["push", "process"]
[dev-dependencies] [dev-dependencies]
tempdir = "0.3" tempdir = "0.3"
clap = "2.32"

65
examples/common/mod.rs Normal file
View File

@ -0,0 +1,65 @@
use clap::{crate_version, App, Arg};
use std::path::PathBuf;
pub struct CommandArgs {
pub pd: Vec<String>,
pub ca: Option<PathBuf>,
pub cert: Option<PathBuf>,
pub key: Option<PathBuf>,
}
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),
}
}

View File

@ -11,26 +11,27 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
mod common;
use crate::common::parse_args;
use futures::future::Future; use futures::future::Future;
use std::path::PathBuf;
use tikv_client::{raw::Client, Config, Key, KvPair, Result, Value}; use tikv_client::{raw::Client, Config, Key, KvPair, Result, Value};
const KEY: &str = "TiKV"; const KEY: &str = "TiKV";
const VALUE: &str = "Rust"; const VALUE: &str = "Rust";
fn main() -> Result<()> { 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. // Create a configuration to use for the example.
// Optionally encrypt the traffic. // Optionally encrypt the traffic.
let config = Config::new(vec![ let config = if let (Some(ca), Some(cert), Some(key)) = (args.ca, args.cert, args.key) {
"192.168.0.100:3379", // Avoid a single point of failure, Config::new(args.pd).with_security(ca, cert, key)
"192.168.0.101:3379", // use more than one PD endpoint. } else {
"192.168.0.102:3379", Config::new(args.pd)
]) };
.with_security(
PathBuf::from("/path/to/ca.pem"),
PathBuf::from("/path/to/client.pem"),
PathBuf::from("/path/to/client-key.pem"),
);
// When we first create a client we recieve a `Connect` structure which must be resolved before // When we first create a client we recieve a `Connect` structure which must be resolved before
// the client is actually connected and usable. // the client is actually connected and usable.

View File

@ -11,9 +11,11 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
mod common;
use crate::common::parse_args;
use futures::{future, Future, Stream}; use futures::{future, Future, Stream};
use std::ops::RangeBounds; use std::ops::RangeBounds;
use std::path::PathBuf;
use tikv_client::{ use tikv_client::{
transaction::{Client, IsolationLevel}, transaction::{Client, IsolationLevel},
Config, Key, KvPair, Value, Config, Key, KvPair, Value,
@ -70,11 +72,18 @@ fn dels(client: &Client, keys: impl IntoIterator<Item = Key>) {
} }
fn main() { fn main() {
let config = Config::new(vec!["127.0.0.1:2379"]).with_security( // You can try running this example by passing your pd endpoints
PathBuf::from("/path/to/ca.pem"), // (and SSL options if necessary) through command line arguments.
PathBuf::from("/path/to/client.pem"), let args = parse_args("txn");
PathBuf::from("/path/to/client-key.pem"),
); // 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) let txn = Client::new(&config)
.wait() .wait()
.expect("Could not connect to tikv"); .expect("Could not connect to tikv");