Skip to content

Commit

Permalink
feat(cli): add --check for server status check
Browse files Browse the repository at this point in the history
  • Loading branch information
everpcpc committed Jan 10, 2024
1 parent 451915a commit 595aaa7
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 1 deletion.
10 changes: 9 additions & 1 deletion cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,9 @@ struct Args {
#[clap(short = 'n', long, help = "Force non-interactive mode")]
non_interactive: bool,

#[clap(long, help = "Check for server status and exit")]
check: bool,

#[clap(long, require_equals = true, help = "Query to execute")]
query: Option<String>,

Expand Down Expand Up @@ -280,7 +283,7 @@ pub async fn main() -> Result<()> {

let mut settings = Settings::default();
let is_terminal = stdin().is_terminal();
let is_repl = is_terminal && !args.non_interactive && args.query.is_none();
let is_repl = is_terminal && !args.non_interactive && !args.check && args.query.is_none();
if is_repl {
settings.display_pretty_sql = true;
settings.show_progress = true;
Expand Down Expand Up @@ -319,6 +322,11 @@ pub async fn main() -> Result<()> {
let _guards = trace::init_logging(&log_dir, &args.log_level, is_repl).await?;
info!("-> bendsql version: {}", VERSION.as_str());

if args.check {
session.check().await?;
return Ok(());
}

if is_repl {
session.handle_repl().await;
return Ok(());
Expand Down
83 changes: 83 additions & 0 deletions cli/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use std::sync::Arc;

use anyhow::anyhow;
use anyhow::Result;
use chrono::NaiveDateTime;
use databend_driver::ServerStats;
use databend_driver::{Client, Connection};
use rustyline::config::Builder;
Expand Down Expand Up @@ -115,6 +116,88 @@ impl Session {
}
}

pub async fn check(&mut self) -> Result<()> {
// bendsql version
{
println!("BendSQL {}", VERSION.as_str());
}

// basic connection info
{
let info = self.conn.info().await;
println!(
"Checking Databend Query server via {} at {}:{} as user {}.",
info.handler, info.host, info.port, info.user
);
if let Some(warehouse) = &info.warehouse {
println!("Using Databend Cloud warehouse: {}", warehouse);
}
if let Some(database) = &info.database {
println!("Current database: {}", database);
} else {
println!("Current database: default");
}
}

// server version
{
let version = self.conn.version().await?;
println!("Server version: {}", version);
}

// license info
match self.conn.query_iter("call admin$license_info()").await {
Ok(mut rows) => {
let row = rows.next().await.unwrap()?;
let linfo: (String, String, String, NaiveDateTime, NaiveDateTime, String) = row
.try_into()
.map_err(|e| anyhow!("parse license info failed: {}", e))?;
if chrono::Utc::now().naive_utc() > linfo.4 {
eprintln!("-> WARN: License expired at {}", linfo.4);
} else {
println!(
"License({}) issued by {} for {} from {} to {}",
linfo.1, linfo.0, linfo.2, linfo.3, linfo.4
);
}
}
Err(_) => {
eprintln!("-> WARN: License not available, only community features enabled.");
}
}

// backend storage
{
let stage_file = "@~/bendsql/.check";
match self.conn.get_presigned_url("UPLOAD", stage_file).await {
Err(_) => {
eprintln!("-> WARN: Backend storage dose not support presigned url.");
eprintln!(" Loading data from local file may not work as expected.");
eprintln!(" Be aware of data transfer cost with argument `presigned_url_disabled=1`.");
}
Ok(resp) => {
let now_utc = chrono::Utc::now();
let data = now_utc.to_rfc3339().as_bytes().to_vec();
let size = data.len() as u64;
let reader = Box::new(std::io::Cursor::new(data));
match self.conn.upload_to_stage(stage_file, reader, size).await {
Err(e) => {
eprintln!("-> ERR: Backend storage upload not working as expected.");
eprintln!(" {}", e);
}
Ok(()) => {
let u = url::Url::parse(&resp.url)?;
let host = u.host_str().unwrap_or("unknown");
println!("Backend storage OK: {}", host);
}
};
}
}
}

Ok(())
}

pub async fn handle_repl(&mut self) {
let config = Builder::new()
.completion_prompt_limit(5)
Expand Down

0 comments on commit 595aaa7

Please sign in to comment.