-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathclient.rs
More file actions
107 lines (95 loc) · 3.36 KB
/
client.rs
File metadata and controls
107 lines (95 loc) · 3.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
use crate::{
interpreter::{ContextArc, options::ChDigViews},
utils::TerminalRawModeGuard,
view::ViewProvider,
};
use cursive::{Cursive, views::Dialog};
use percent_encoding::percent_decode;
use std::process::Command;
use std::str::FromStr;
pub struct ClientViewProvider;
impl ViewProvider for ClientViewProvider {
fn name(&self) -> &'static str {
"Client"
}
fn view_type(&self) -> ChDigViews {
ChDigViews::Client
}
fn show(&self, siv: &mut Cursive, context: ContextArc) {
let options = context.lock().unwrap().options.clickhouse.clone();
let mut cmd = Command::new("clickhouse");
cmd.arg("client");
if let Some(config) = &options.config {
cmd.arg("--config").arg(config);
}
if let Some(url) = &options.url
&& let Ok(url) = url::Url::parse(url)
{
if let Some(host) = &url.host() {
cmd.arg("--host").arg(host.to_string());
}
if let Some(port) = &url.port() {
cmd.arg("--port").arg(port.to_string());
}
if !url.username().is_empty() {
cmd.arg("--user").arg(url.username());
}
if let Some(password) = &url.password() {
cmd.arg("--password").arg(
percent_decode(password.as_bytes())
.decode_utf8_lossy()
.to_string(),
);
}
let pairs: std::collections::HashMap<_, _> = url.query_pairs().into_owned().collect();
if let Some(skip_verify) = pairs
.get("skip_verify")
.and_then(|v| bool::from_str(v).ok())
&& skip_verify
{
cmd.arg("--accept-invalid-certificate");
}
if pairs
.get("secure")
.and_then(|v| bool::from_str(v).ok())
.unwrap_or_default()
{
cmd.arg("--secure");
}
}
let cb_sink = siv.cb_sink().clone();
let cmd_line = format!("{:?}", cmd);
log::info!("Spawning client: {}", cmd_line);
let result = {
let _guard = TerminalRawModeGuard::leave();
eprintln!("\n--- chdig: launching clickhouse client ---\n");
cmd.status()
};
match result {
Ok(status) => {
cb_sink
.send(Box::new(move |siv| {
siv.complete_clear();
if !status.success() {
siv.add_layer(Dialog::info(format!(
"clickhouse client exited with status: {}\n\nCommand: {}",
status, cmd_line
)));
}
}))
.ok();
}
Err(err) => {
cb_sink.send(Box::new(move |siv| {
siv.complete_clear();
siv.add_layer(Dialog::info(format!(
"Failed to spawn clickhouse client: {}\n\nCommand: {}\n\nMake sure clickhouse is installed and in PATH",
err, cmd_line
)));
})).ok();
}
}
siv.complete_clear();
log::info!("Client terminated.");
}
}