From 1c07f418fde01be040924a96f1fe061f55c3c9d2 Mon Sep 17 00:00:00 2001 From: taichong Date: Wed, 3 Jul 2024 23:15:31 +0800 Subject: [PATCH 1/5] fix: config support parse tls --- README.md | 5 ++++- cli/src/config.rs | 5 ++++- cli/src/main.rs | 33 ++++++++++++++++++++++++++------- 3 files changed, 34 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 44c346ac..2242bbe1 100644 --- a/README.md +++ b/README.md @@ -105,8 +105,11 @@ sequentially if exists. ``` ❯ cat ~/.bendsql/config.toml [connection] -connect_timeout = "30s" +host = "127.0.0.1" +[connection.args] +tls = "false" +connect_timeout = "30" [settings] display_pretty_sql = true diff --git a/cli/src/config.rs b/cli/src/config.rs index 312659ec..5562cd7b 100644 --- a/cli/src/config.rs +++ b/cli/src/config.rs @@ -223,7 +223,10 @@ impl Config { fn load_from_file(path: &str) -> Self { match toml::from_str(&std::fs::read_to_string(path).unwrap()) { - Ok(config) => config, + Ok(config) => { + println!("path is {:?}", path); + config + } Err(e) => { eprintln!("failed to load config file {}: {}, using defaults", path, e); Self::default() diff --git a/cli/src/main.rs b/cli/src/main.rs index eea23fcc..7caece52 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -113,7 +113,7 @@ struct Args { flight: bool, #[clap(long, help = "Enable TLS, ignored when --dsn is set")] - tls: bool, + tls: Option, #[clap( short = 'h', @@ -254,8 +254,16 @@ pub async fn main() -> Result<()> { if !args.set.is_empty() { eprintln!("warning: --set is ignored when --dsn is set"); } - if args.tls { - eprintln!("warning: --tls is ignored when --dsn is set"); + if let Some(tls) = args.tls { + if tls { + eprintln!("warning: --tls is ignored when --dsn is set"); + } + } else { + if let Some(tls) = config.connection.args.get("tls") { + if tls.to_lowercase() == "true" { + eprintln!("warning: --tls is ignored when --dsn is set") + } + } } if args.flight { eprintln!("warning: --flight is ignored when --dsn is set"); @@ -292,10 +300,20 @@ pub async fn main() -> Result<()> { // override only if args.dsn is none if args.dsn.is_none() { - if !args.tls { - conn_args - .args - .insert("sslmode".to_string(), "disable".to_string()); + if let Some(tls) = args.tls { + if !tls { + conn_args + .args + .insert("sslmode".to_string(), "disable".to_string()); + } + } else { + if let Some(tls) = conn_args.args.get("tls") { + if tls.to_lowercase() != "true" { + conn_args + .args + .insert("sslmode".to_string(), "disable".to_string()); + } + } } // override args if specified in command line @@ -311,6 +329,7 @@ pub async fn main() -> Result<()> { } let dsn = conn_args.get_dsn()?; + println!("not set dsn : {}", dsn.clone()); let mut settings = Settings::default(); let is_terminal = stdin().is_terminal(); let is_repl = is_terminal && !args.non_interactive && !args.check && args.query.is_none(); From a31b02791bd2baf4171c78d13c710ba05f50485b Mon Sep 17 00:00:00 2001 From: taichong Date: Wed, 3 Jul 2024 23:26:01 +0800 Subject: [PATCH 2/5] fix clippy --- cli/src/config.rs | 5 +---- cli/src/main.rs | 30 +++++++++++++++++------------- 2 files changed, 18 insertions(+), 17 deletions(-) diff --git a/cli/src/config.rs b/cli/src/config.rs index 5562cd7b..312659ec 100644 --- a/cli/src/config.rs +++ b/cli/src/config.rs @@ -223,10 +223,7 @@ impl Config { fn load_from_file(path: &str) -> Self { match toml::from_str(&std::fs::read_to_string(path).unwrap()) { - Ok(config) => { - println!("path is {:?}", path); - config - } + Ok(config) => config, Err(e) => { eprintln!("failed to load config file {}: {}, using defaults", path, e); Self::default() diff --git a/cli/src/main.rs b/cli/src/main.rs index 7caece52..14535e3e 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -258,13 +258,15 @@ pub async fn main() -> Result<()> { if tls { eprintln!("warning: --tls is ignored when --dsn is set"); } - } else { - if let Some(tls) = config.connection.args.get("tls") { - if tls.to_lowercase() == "true" { - eprintln!("warning: --tls is ignored when --dsn is set") - } + } else if let Some(tls) = config.connection.args.get("tls") { + if tls.to_lowercase() == "true" { + eprintln!("warning: --tls is ignored when --dsn is set") } + } else if config.connection.args.get("tls").is_none() { + // args.tls is none and not config in config.toml + eprintln!("warning: --tls is ignored when --dsn is set"); } + if args.flight { eprintln!("warning: --flight is ignored when --dsn is set"); } @@ -306,14 +308,17 @@ pub async fn main() -> Result<()> { .args .insert("sslmode".to_string(), "disable".to_string()); } - } else { - if let Some(tls) = conn_args.args.get("tls") { - if tls.to_lowercase() != "true" { - conn_args - .args - .insert("sslmode".to_string(), "disable".to_string()); - } + } else if let Some(tls) = conn_args.args.get("tls") { + if tls.to_lowercase() == "false" { + conn_args + .args + .insert("sslmode".to_string(), "disable".to_string()); } + } else if conn_args.args.get("tls").is_none() { + // means arg.tls is none and not config in config.toml + conn_args + .args + .insert("sslmode".to_string(), "disable".to_string()); } // override args if specified in command line @@ -329,7 +334,6 @@ pub async fn main() -> Result<()> { } let dsn = conn_args.get_dsn()?; - println!("not set dsn : {}", dsn.clone()); let mut settings = Settings::default(); let is_terminal = stdin().is_terminal(); let is_repl = is_terminal && !args.non_interactive && !args.check && args.query.is_none(); From a8688736d98077f3da8c9d2dded6f571c4587915 Mon Sep 17 00:00:00 2001 From: taichong Date: Thu, 4 Jul 2024 00:18:30 +0800 Subject: [PATCH 3/5] fix check --- cli/src/main.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cli/src/main.rs b/cli/src/main.rs index 14535e3e..48344d17 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -262,7 +262,7 @@ pub async fn main() -> Result<()> { if tls.to_lowercase() == "true" { eprintln!("warning: --tls is ignored when --dsn is set") } - } else if config.connection.args.get("tls").is_none() { + } else if !config.connection.args.contains_key("tls") { // args.tls is none and not config in config.toml eprintln!("warning: --tls is ignored when --dsn is set"); } @@ -314,7 +314,7 @@ pub async fn main() -> Result<()> { .args .insert("sslmode".to_string(), "disable".to_string()); } - } else if conn_args.args.get("tls").is_none() { + } else if !conn_args.args.contains_key("tls") { // means arg.tls is none and not config in config.toml conn_args .args From 6113205808f4f0ce8118e7083dcd1382ff755fe3 Mon Sep 17 00:00:00 2001 From: taichong Date: Thu, 4 Jul 2024 09:53:24 +0800 Subject: [PATCH 4/5] remove eprint --- cli/src/main.rs | 3 --- 1 file changed, 3 deletions(-) diff --git a/cli/src/main.rs b/cli/src/main.rs index 48344d17..f158ce91 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -262,9 +262,6 @@ pub async fn main() -> Result<()> { if tls.to_lowercase() == "true" { eprintln!("warning: --tls is ignored when --dsn is set") } - } else if !config.connection.args.contains_key("tls") { - // args.tls is none and not config in config.toml - eprintln!("warning: --tls is ignored when --dsn is set"); } if args.flight { From 096027a1f409770a13072fb639321b7e44df7129 Mon Sep 17 00:00:00 2001 From: taichong Date: Thu, 4 Jul 2024 12:50:14 +0800 Subject: [PATCH 5/5] add ConnectionConfig::tls --- README.md | 2 +- cli/src/config.rs | 2 ++ cli/src/main.rs | 10 +++++----- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 2242bbe1..388649a6 100644 --- a/README.md +++ b/README.md @@ -106,9 +106,9 @@ sequentially if exists. ❯ cat ~/.bendsql/config.toml [connection] host = "127.0.0.1" +tls = false [connection.args] -tls = "false" connect_timeout = "30" [settings] diff --git a/cli/src/config.rs b/cli/src/config.rs index 312659ec..44f80975 100644 --- a/cli/src/config.rs +++ b/cli/src/config.rs @@ -199,6 +199,7 @@ pub struct ConnectionConfig { pub port: Option, pub user: String, pub database: Option, + pub tls: Option, pub args: BTreeMap, } @@ -261,6 +262,7 @@ impl Default for ConnectionConfig { port: Some(8000), user: "root".to_string(), database: None, + tls: None, args: BTreeMap::new(), } } diff --git a/cli/src/main.rs b/cli/src/main.rs index f158ce91..310b7377 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -258,8 +258,8 @@ pub async fn main() -> Result<()> { if tls { eprintln!("warning: --tls is ignored when --dsn is set"); } - } else if let Some(tls) = config.connection.args.get("tls") { - if tls.to_lowercase() == "true" { + } else if let Some(tls) = config.connection.tls { + if tls { eprintln!("warning: --tls is ignored when --dsn is set") } } @@ -305,13 +305,13 @@ pub async fn main() -> Result<()> { .args .insert("sslmode".to_string(), "disable".to_string()); } - } else if let Some(tls) = conn_args.args.get("tls") { - if tls.to_lowercase() == "false" { + } else if let Some(tls) = config.connection.tls { + if !tls { conn_args .args .insert("sslmode".to_string(), "disable".to_string()); } - } else if !conn_args.args.contains_key("tls") { + } else if config.connection.tls.is_none() { // means arg.tls is none and not config in config.toml conn_args .args