22//
33// SPDX-License-Identifier: Apache-2.0.
44
5+ use std:: fmt;
6+ use std:: str:: FromStr ;
7+
58use common_exception:: ErrorCode ;
69use common_exception:: Result ;
710use lazy_static:: lazy_static;
@@ -137,15 +140,81 @@ pub struct Config {
137140 pub store_api_address : String ,
138141
139142 #[ structopt( long, env = STORE_API_USERNAME , default_value = "root" ) ]
140- pub store_api_username : String ,
143+ pub store_api_username : User ,
141144
142145 #[ structopt( long, env = STORE_API_PASSWORD , default_value = "root" ) ]
143- pub store_api_password : String ,
146+ pub store_api_password : Password ,
144147
145148 #[ structopt( long, short = "c" , env = CONFIG_FILE , default_value = "" ) ]
146149 pub config_file : String ,
147150}
148151
152+ #[ derive( Clone , serde:: Deserialize , PartialEq , StructOpt , StructOptToml ) ]
153+ #[ serde( default ) ]
154+ pub struct Password {
155+ pub store_api_password : String ,
156+ }
157+
158+ impl AsRef < String > for Password {
159+ fn as_ref ( & self ) -> & String {
160+ & self . store_api_password
161+ }
162+ }
163+
164+ impl FromStr for Password {
165+ type Err = ErrorCode ;
166+ fn from_str ( s : & str ) -> common_exception:: Result < Self > {
167+ Ok ( Self {
168+ store_api_password : s. to_string ( ) ,
169+ } )
170+ }
171+ }
172+
173+ impl ToString for Password {
174+ fn to_string ( & self ) -> String {
175+ self . store_api_password . clone ( )
176+ }
177+ }
178+
179+ impl fmt:: Debug for Password {
180+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
181+ write ! ( f, "******" )
182+ }
183+ }
184+
185+ #[ derive( Clone , serde:: Deserialize , PartialEq , StructOpt , StructOptToml ) ]
186+ #[ serde( default ) ]
187+ pub struct User {
188+ pub store_api_username : String ,
189+ }
190+
191+ impl ToString for User {
192+ fn to_string ( & self ) -> String {
193+ self . store_api_username . clone ( )
194+ }
195+ }
196+
197+ impl fmt:: Debug for User {
198+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
199+ write ! ( f, "******" )
200+ }
201+ }
202+
203+ impl AsRef < String > for User {
204+ fn as_ref ( & self ) -> & String {
205+ & self . store_api_username
206+ }
207+ }
208+
209+ impl FromStr for User {
210+ type Err = ErrorCode ;
211+ fn from_str ( s : & str ) -> common_exception:: Result < Self > {
212+ Ok ( Self {
213+ store_api_username : s. to_string ( ) ,
214+ } )
215+ }
216+ }
217+
149218impl Config {
150219 /// Default configs.
151220 pub fn default ( ) -> Self {
@@ -163,8 +232,12 @@ impl Config {
163232 http_api_address : "127.0.0.1:8080" . to_string ( ) ,
164233 metric_api_address : "127.0.0.1:7070" . to_string ( ) ,
165234 store_api_address : "127.0.0.1:9191" . to_string ( ) ,
166- store_api_username : "root" . to_string ( ) ,
167- store_api_password : "root" . to_string ( ) ,
235+ store_api_username : User {
236+ store_api_username : "root" . to_string ( ) ,
237+ } ,
238+ store_api_password : Password {
239+ store_api_password : "root" . to_string ( ) ,
240+ } ,
168241 config_file : "" . to_string ( ) ,
169242 }
170243 }
@@ -231,8 +304,8 @@ impl Config {
231304 env_helper ! ( mut_config, http_api_address, String , HTTP_API_ADDRESS ) ;
232305 env_helper ! ( mut_config, metric_api_address, String , METRICS_API_ADDRESS ) ;
233306 env_helper ! ( mut_config, store_api_address, String , STORE_API_ADDRESS ) ;
234- env_helper ! ( mut_config, store_api_username, String , STORE_API_USERNAME ) ;
235- env_helper ! ( mut_config, store_api_password, String , STORE_API_PASSWORD ) ;
307+ env_helper ! ( mut_config, store_api_username, User , STORE_API_USERNAME ) ;
308+ env_helper ! ( mut_config, store_api_password, Password , STORE_API_PASSWORD ) ;
236309
237310 Ok ( mut_config)
238311 }
0 commit comments