Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@ features = ["push", "process"]

[dev-dependencies]
tempdir = "0.3"
clap = "2.32"
65 changes: 65 additions & 0 deletions examples/common/mod.rs
Original file line number Diff line number Diff line change
@@ -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),
}
}
23 changes: 12 additions & 11 deletions examples/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
21 changes: 15 additions & 6 deletions examples/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -70,11 +72,18 @@ fn dels(client: &Client, keys: impl IntoIterator<Item = Key>) {
}

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");
Expand Down