Skip to content

Commit c33bc13

Browse files
sticnarfHoverbear
authored andcommitted
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 <[email protected]> * Document most public code. Signed-off-by: Ana Hobden <[email protected]> * Reduce pub surface Signed-off-by: Ana Hobden <[email protected]> * fmt/lint Signed-off-by: Ana Hobden <[email protected]> * Add cf to concrete builder types Signed-off-by: Xiaoguang Sun <[email protected]> * Fixed some comments and confusing name Signed-off-by: Xiaoguang Sun <[email protected]> * Change Request from struct to enum Signed-off-by: Xiaoguang Sun <[email protected]> * Change tso_tx/rx channel to bounded Signed-off-by: Xiaoguang Sun <[email protected]> * Fix format issues and improve implementations Signed-off-by: Xiaoguang Sun <[email protected]> * Change to dyn trait syntax Signed-off-by: Xiaoguang Sun <[email protected]> * inline some functions Signed-off-by: Ana Hobden <[email protected]> * Better note on KvPair Signed-off-by: Ana Hobden <[email protected]> * Use 3 PDs in raw example Signed-off-by: Ana Hobden <[email protected]> * Clarify documentation Signed-off-by: Ana Hobden <[email protected]> * Get CI green Signed-off-by: Ana Hobden <[email protected]> * Remove not useful PrivateKey type Signed-off-by: Xiaoguang Sun <[email protected]> * Change CUSTOM_CF to "default" in examples/raw.rs Signed-off-by: Xiaoguang Sun <[email protected]> * Use command line args in examples. Signed-off-by: Yilin Chen <[email protected]> * Fix the wrong app name in the transactional API example. Signed-off-by: Yilin Chen <[email protected]> * Extract duplicate code to a common mod Signed-off-by: Yilin Chen <[email protected]>
1 parent 573871c commit c33bc13

File tree

4 files changed

+93
-17
lines changed

4 files changed

+93
-17
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,4 @@ features = ["push", "process"]
3434

3535
[dev-dependencies]
3636
tempdir = "0.3"
37+
clap = "2.32"

examples/common/mod.rs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
use clap::{crate_version, App, Arg};
2+
use std::path::PathBuf;
3+
4+
pub struct CommandArgs {
5+
pub pd: Vec<String>,
6+
pub ca: Option<PathBuf>,
7+
pub cert: Option<PathBuf>,
8+
pub key: Option<PathBuf>,
9+
}
10+
11+
pub fn parse_args(app_name: &str) -> CommandArgs {
12+
let matches = App::new(app_name)
13+
.version(crate_version!())
14+
.author("The TiKV Project Authors")
15+
.arg(
16+
Arg::with_name("pd")
17+
.long("pd")
18+
.aliases(&["pd-endpoint", "pd-endpoints"])
19+
.value_name("PD_URL")
20+
.help("Sets PD endpoints")
21+
.long_help("Sets PD endpoints. Uses `,` to separate multiple PDs")
22+
.takes_value(true)
23+
.multiple(true)
24+
.value_delimiter(",")
25+
.required(true),
26+
)
27+
// A cyclic dependency between CA, cert and key is made
28+
// to ensure that no security options are missing.
29+
.arg(
30+
Arg::with_name("ca")
31+
.long("ca")
32+
.value_name("CA_PATH")
33+
.help("Sets the CA")
34+
.long_help("Sets the CA. Must be used with --cert and --key")
35+
.takes_value(true)
36+
.requires("cert"),
37+
)
38+
.arg(
39+
Arg::with_name("cert")
40+
.long("cert")
41+
.value_name("CERT_PATH")
42+
.help("Sets the certificate")
43+
.long_help("Sets the certificate. Must be used with --ca and --key")
44+
.takes_value(true)
45+
.requires("key"),
46+
)
47+
.arg(
48+
Arg::with_name("key")
49+
.long("key")
50+
.alias("private-key")
51+
.value_name("KEY_PATH")
52+
.help("Sets the private key")
53+
.long_help("Sets the private key. Must be used with --ca and --cert")
54+
.takes_value(true)
55+
.requires("ca"),
56+
)
57+
.get_matches();
58+
59+
CommandArgs {
60+
pd: matches.values_of("pd").unwrap().map(String::from).collect(),
61+
ca: matches.value_of("ca").map(PathBuf::from),
62+
cert: matches.value_of("cert").map(PathBuf::from),
63+
key: matches.value_of("key").map(PathBuf::from),
64+
}
65+
}

examples/raw.rs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,27 @@
1111
// See the License for the specific language governing permissions and
1212
// limitations under the License.
1313

14+
mod common;
15+
16+
use crate::common::parse_args;
1417
use futures::future::Future;
15-
use std::path::PathBuf;
1618
use tikv_client::{raw::Client, Config, Key, KvPair, Result, Value};
1719

1820
const KEY: &str = "TiKV";
1921
const VALUE: &str = "Rust";
2022

2123
fn main() -> Result<()> {
24+
// You can try running this example by passing your pd endpoints
25+
// (and SSL options if necessary) through command line arguments.
26+
let args = parse_args("raw");
27+
2228
// Create a configuration to use for the example.
2329
// Optionally encrypt the traffic.
24-
let config = Config::new(vec![
25-
"192.168.0.100:3379", // Avoid a single point of failure,
26-
"192.168.0.101:3379", // use more than one PD endpoint.
27-
"192.168.0.102:3379",
28-
])
29-
.with_security(
30-
PathBuf::from("/path/to/ca.pem"),
31-
PathBuf::from("/path/to/client.pem"),
32-
PathBuf::from("/path/to/client-key.pem"),
33-
);
30+
let config = if let (Some(ca), Some(cert), Some(key)) = (args.ca, args.cert, args.key) {
31+
Config::new(args.pd).with_security(ca, cert, key)
32+
} else {
33+
Config::new(args.pd)
34+
};
3435

3536
// When we first create a client we recieve a `Connect` structure which must be resolved before
3637
// the client is actually connected and usable.

examples/transaction.rs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@
1111
// See the License for the specific language governing permissions and
1212
// limitations under the License.
1313

14+
mod common;
15+
16+
use crate::common::parse_args;
1417
use futures::{future, Future, Stream};
1518
use std::ops::RangeBounds;
16-
use std::path::PathBuf;
1719
use tikv_client::{
1820
transaction::{Client, IsolationLevel},
1921
Config, Key, KvPair, Value,
@@ -70,11 +72,18 @@ fn dels(client: &Client, keys: impl IntoIterator<Item = Key>) {
7072
}
7173

7274
fn main() {
73-
let config = Config::new(vec!["127.0.0.1:2379"]).with_security(
74-
PathBuf::from("/path/to/ca.pem"),
75-
PathBuf::from("/path/to/client.pem"),
76-
PathBuf::from("/path/to/client-key.pem"),
77-
);
75+
// You can try running this example by passing your pd endpoints
76+
// (and SSL options if necessary) through command line arguments.
77+
let args = parse_args("txn");
78+
79+
// Create a configuration to use for the example.
80+
// Optionally encrypt the traffic.
81+
let config = if let (Some(ca), Some(cert), Some(key)) = (args.ca, args.cert, args.key) {
82+
Config::new(args.pd).with_security(ca, cert, key)
83+
} else {
84+
Config::new(args.pd)
85+
};
86+
7887
let txn = Client::new(&config)
7988
.wait()
8089
.expect("Could not connect to tikv");

0 commit comments

Comments
 (0)