mirror of https://github.com/tikv/client-rust.git
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:
parent
573871c2c6
commit
c33bc136b4
|
@ -34,3 +34,4 @@ features = ["push", "process"]
|
|||
|
||||
[dev-dependencies]
|
||||
tempdir = "0.3"
|
||||
clap = "2.32"
|
||||
|
|
|
@ -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),
|
||||
}
|
||||
}
|
|
@ -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.
|
||||
|
|
|
@ -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<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");
|
||||
|
|
Loading…
Reference in New Issue