1414
1515#![ allow( clippy:: large_enum_variant) ]
1616
17+ use std:: path:: Path ;
18+ use crate :: config:: WalletConfig ;
19+ use crate :: error:: BDKCliError as Error ;
1720use bdk_wallet:: bitcoin:: {
1821 bip32:: { DerivationPath , Xpriv } ,
1922 Address , Network , OutPoint , ScriptBuf ,
@@ -167,15 +170,15 @@ pub struct WalletOpts {
167170 feature = "rpc" ,
168171 feature = "cbf"
169172 ) ) ]
170- #[ arg( env = "CLIENT_TYPE" , short = 'c' , long, value_enum, required = true ) ]
171- pub client_type : ClientType ,
173+ #[ arg( env = "CLIENT_TYPE" , short = 'c' , long, value_enum) ]
174+ pub client_type : Option < ClientType > ,
172175 #[ cfg( feature = "sqlite" ) ]
173- #[ arg( env = "DATABASE_TYPE" , short = 'd' , long, value_enum, required = true ) ]
174- pub database_type : DatabaseType ,
176+ #[ arg( env = "DATABASE_TYPE" , short = 'd' , long, value_enum, default_value= "sqlite" ) ]
177+ pub database_type : Option < DatabaseType > ,
175178 /// Sets the server url.
176179 #[ cfg( any( feature = "electrum" , feature = "esplora" , feature = "rpc" ) ) ]
177- #[ arg( env = "SERVER_URL" , short = 'u' , long, required = true ) ]
178- pub url : String ,
180+ #[ arg( env = "SERVER_URL" , short = 'u' , long) ]
181+ pub url : Option < String > ,
179182 /// Electrum batch size.
180183 #[ cfg( feature = "electrum" ) ]
181184 #[ arg( env = "ELECTRUM_BATCH_SIZE" , short = 'b' , long, default_value = "10" ) ]
@@ -198,7 +201,7 @@ pub struct WalletOpts {
198201 value_parser = parse_proxy_auth,
199202 default_value = "user:password" ,
200203 ) ]
201- pub basic_auth : ( String , String ) ,
204+ pub basic_auth : Option < ( String , String ) > ,
202205 #[ cfg( feature = "rpc" ) ]
203206 /// Sets an optional cookie authentication.
204207 #[ arg( env = "COOKIE" ) ]
@@ -208,6 +211,63 @@ pub struct WalletOpts {
208211 pub compactfilter_opts : CompactFilterOpts ,
209212}
210213
214+ impl WalletOpts {
215+ /// Load configuration from TOML file and merge with CLI options
216+ pub fn load_config ( & mut self , wallet_name : & str , datadir : & Path ) -> Result < ( ) , Error > {
217+ if let Some ( config) = WalletConfig :: load ( datadir) ? {
218+ if let Ok ( config_opts) = config. get_wallet_opts ( wallet_name) {
219+ self . wallet = self . wallet . take ( ) . or ( config_opts. wallet ) ;
220+ self . verbose = self . verbose || config_opts. verbose ;
221+ self . ext_descriptor = self . ext_descriptor . take ( ) . or ( config_opts. ext_descriptor ) ;
222+ self . int_descriptor = self . int_descriptor . take ( ) . or ( config_opts. int_descriptor ) ;
223+ #[ cfg( any(
224+ feature = "electrum" ,
225+ feature = "esplora" ,
226+ feature = "rpc" ,
227+ feature = "cbf"
228+ ) ) ]
229+ {
230+ self . client_type = self . client_type . clone ( ) . or ( config_opts. client_type ) ;
231+ }
232+ #[ cfg( feature = "sqlite" ) ]
233+ {
234+ self . database_type = self . database_type . clone ( ) . or ( config_opts. database_type ) ;
235+ }
236+ #[ cfg( any( feature = "electrum" , feature = "esplora" , feature = "rpc" ) ) ]
237+ {
238+ self . url = self . url . take ( ) . or ( config_opts. url ) ;
239+ }
240+ #[ cfg( feature = "electrum" ) ]
241+ {
242+ self . batch_size = if self . batch_size != 10 {
243+ config_opts. batch_size
244+ } else {
245+ self . batch_size
246+ } ;
247+ }
248+ #[ cfg( feature = "esplora" ) ]
249+ {
250+ self . parallel_requests = if self . parallel_requests != 5 {
251+ config_opts. parallel_requests
252+ } else {
253+ self . parallel_requests
254+ } ;
255+ }
256+ #[ cfg( feature = "rpc" ) ]
257+ {
258+ self . basic_auth = self . basic_auth . take ( ) . or ( config_opts. basic_auth ) ;
259+ self . cookie = self . cookie . take ( ) . or ( config_opts. cookie ) ;
260+ }
261+ #[ cfg( feature = "cbf" ) ]
262+ {
263+ self . compactfilter_opts = config_opts. compactfilter_opts ;
264+ }
265+ }
266+ }
267+ Ok ( ( ) )
268+ }
269+ }
270+
211271/// Options to configure a SOCKS5 proxy for a blockchain client connection.
212272#[ cfg( any( feature = "electrum" , feature = "esplora" ) ) ]
213273#[ derive( Debug , Args , Clone , PartialEq , Eq ) ]
0 commit comments