Skip to content
85 changes: 79 additions & 6 deletions fusequery/query/src/configs/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
//
// SPDX-License-Identifier: Apache-2.0.

use std::fmt;
use std::str::FromStr;

use common_exception::ErrorCode;
use common_exception::Result;
use lazy_static::lazy_static;
Expand Down Expand Up @@ -137,15 +140,81 @@ pub struct Config {
pub store_api_address: String,

#[structopt(long, env = STORE_API_USERNAME, default_value = "root")]
pub store_api_username: String,
pub store_api_username: User,

#[structopt(long, env = STORE_API_PASSWORD, default_value = "root")]
pub store_api_password: String,
pub store_api_password: Password,

#[structopt(long, short = "c", env = CONFIG_FILE, default_value = "")]
pub config_file: String,
}

#[derive(Clone, serde::Deserialize, PartialEq, StructOpt, StructOptToml)]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need new line.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

#[serde(default)]
pub struct Password {
pub store_api_password: String,
}

impl AsRef<String> for Password {
fn as_ref(&self) -> &String {
&self.store_api_password
}
}

impl FromStr for Password {
type Err = ErrorCode;
fn from_str(s: &str) -> common_exception::Result<Self> {
Ok(Self {
store_api_password: s.to_string(),
})
}
}

impl ToString for Password {
fn to_string(&self) -> String {
self.store_api_password.clone()
}
}

impl fmt::Debug for Password {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "******")
}
}

#[derive(Clone, serde::Deserialize, PartialEq, StructOpt, StructOptToml)]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need new line

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image
This Rust Assist extesion looks good. Do we need this format tool?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We use rust-fmt to format the code. You can use make lint to check the format

#[serde(default)]
pub struct User {
pub store_api_username: String,
}

impl ToString for User {
fn to_string(&self) -> String {
self.store_api_username.clone()
}
}

impl fmt::Debug for User {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "******")
}
}

impl AsRef<String> for User {
fn as_ref(&self) -> &String {
&self.store_api_username
}
}

impl FromStr for User {
type Err = ErrorCode;
fn from_str(s: &str) -> common_exception::Result<Self> {
Ok(Self {
store_api_username: s.to_string(),
})
}
}

impl Config {
/// Default configs.
pub fn default() -> Self {
Expand All @@ -163,8 +232,12 @@ impl Config {
http_api_address: "127.0.0.1:8080".to_string(),
metric_api_address: "127.0.0.1:7070".to_string(),
store_api_address: "127.0.0.1:9191".to_string(),
store_api_username: "root".to_string(),
store_api_password: "root".to_string(),
store_api_username: User {
store_api_username: "root".to_string(),
},
store_api_password: Password {
store_api_password: "root".to_string(),
},
config_file: "".to_string(),
}
}
Expand Down Expand Up @@ -231,8 +304,8 @@ impl Config {
env_helper!(mut_config, http_api_address, String, HTTP_API_ADDRESS);
env_helper!(mut_config, metric_api_address, String, METRICS_API_ADDRESS);
env_helper!(mut_config, store_api_address, String, STORE_API_ADDRESS);
env_helper!(mut_config, store_api_username, String, STORE_API_USERNAME);
env_helper!(mut_config, store_api_password, String, STORE_API_PASSWORD);
env_helper!(mut_config, store_api_username, User, STORE_API_USERNAME);
env_helper!(mut_config, store_api_password, Password, STORE_API_PASSWORD);

Ok(mut_config)
}
Expand Down
15 changes: 11 additions & 4 deletions fusequery/query/src/configs/config_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

use common_exception::Result;

use crate::configs::config::Password;
use crate::configs::config::User;

#[test]
fn test_config() -> Result<()> {
use pretty_assertions::assert_eq;
Expand All @@ -26,8 +29,12 @@ fn test_config() -> Result<()> {
http_api_address: "127.0.0.1:8080".to_string(),
metric_api_address: "127.0.0.1:7070".to_string(),
store_api_address: "127.0.0.1:9191".to_string(),
store_api_username: "root".to_string(),
store_api_password: "root".to_string(),
store_api_username: User {
store_api_username: "root".to_string(),
},
store_api_password: Password {
store_api_password: "root".to_string(),
},
config_file: "".to_string(),
};
let actual = Config::default();
Expand Down Expand Up @@ -95,8 +102,8 @@ fn test_config() -> Result<()> {
assert_eq!("1.2.3.4:7071", configured.metric_api_address);

assert_eq!("1.2.3.4:1234", configured.store_api_address);
assert_eq!("admin", configured.store_api_username);
assert_eq!("password!", configured.store_api_password);
assert_eq!("admin", configured.store_api_username.to_string());
assert_eq!("password!", configured.store_api_password.to_string());

// clean up
std::env::remove_var("FUSE_QUERY_LOG_LEVEL");
Expand Down
4 changes: 2 additions & 2 deletions fusequery/query/src/datasources/remote/remote_factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ impl TryGetStoreClient for ClientProvider {
async fn try_get_client(&self) -> Result<StoreClient> {
let client = StoreClient::try_create(
&self.conf.store_api_address,
&self.conf.store_api_username,
&self.conf.store_api_password,
&self.conf.store_api_username.as_ref(),
&self.conf.store_api_password.as_ref(),
)
.await
.map_err(ErrorCode::from)?;
Expand Down