Skip to content

Commit

Permalink
feat: cli support reconnect (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
sundy-li authored Apr 6, 2023
1 parent fbe0a3c commit 6f15e4c
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 10 deletions.
2 changes: 1 addition & 1 deletion cli/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bendsql"
version = "0.1.2"
version = "0.1.3"
edition = "2021"
license = "Apache-2.0"
description = "Databend Native Cli tool"
Expand Down
59 changes: 50 additions & 9 deletions cli/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ use crate::helper::CliHelper;
pub struct Session {
client: FlightSqlServiceClient,
is_repl: bool,
endpoint: Endpoint,
user: String,
password: String,

config: Config,
prompt: String,
}
Expand All @@ -47,19 +51,19 @@ impl Session {
password: &str,
is_repl: bool,
) -> Result<Self, ArrowError> {
if is_repl {
println!("Welcome to databend-cli.");
println!("Connecting to {} as user {}.", endpoint.uri(), user);
println!();
}

let channel = endpoint
.connect()
.await
.map_err(|err| ArrowError::IoError(err.to_string()))?;

let mut client = FlightSqlServiceClient::new(channel);

if is_repl {
println!("Welcome to databend-cli.");
println!("Connecting to {} as user {}.", endpoint.uri(), user);
println!();
}

// enable progress
client.set_header("bendsql", "1");
let _token = client.handshake(user, password).await.unwrap();
Expand All @@ -76,6 +80,9 @@ impl Session {
client,
is_repl,
prompt,
user: user.to_owned(),
password: password.to_owned(),
endpoint,
})
}

Expand Down Expand Up @@ -127,9 +134,16 @@ impl Session {
break;
}
Ok(false) => {}
Err(e) => {
eprintln!("{}", format_error(e));
}
Err(e) => match e {
ArrowError::IoError(e) if e.contains("Unauthenticated") => {
if let Err(e) = self.reconnect().await {
eprintln!("Reconnect error: {}", format_error(e));
} else if let Err(e) = self.handle_query(true, &query).await {
eprintln!("{}", format_error(e));
}
}
e => eprintln!("{}", format_error(e)),
},
}
}
query.clear();
Expand Down Expand Up @@ -200,6 +214,33 @@ impl Session {

Ok(false)
}

async fn reconnect(&mut self) -> Result<(), ArrowError> {
if self.is_repl {
println!(
"Connecting to {} as user {}.",
self.endpoint.uri(),
self.user
);
println!();
}

let channel = self
.endpoint
.connect()
.await
.map_err(|err| ArrowError::IoError(err.to_string()))?;

self.client = FlightSqlServiceClient::new(channel);
// enable progress
self.client.set_header("bendsql", "1");
let _token = self
.client
.handshake(&self.user, &self.password)
.await
.unwrap();
Ok(())
}
}

fn get_history_path() -> String {
Expand Down

0 comments on commit 6f15e4c

Please sign in to comment.