@@ -204,6 +204,77 @@ impl Config {
204204 Ok ( config)
205205 }
206206
207+ #[ cfg( feature = "v2" ) ]
208+ pub fn save_config ( cli : & Cli ) -> Result < ( ) , anyhow:: Error > {
209+ let version = Self :: determine_version ( cli)
210+ . map_err ( |e| anyhow:: anyhow!( "Failed to determine version: {e}" ) ) ?;
211+
212+ if version != Version :: Two {
213+ return Err ( anyhow:: anyhow!( "--set-config is only available for bip77 (v2) mode" ) ) ;
214+ }
215+
216+ // Ensure all required parameters are present
217+ if cli. rpcuser . is_none ( )
218+ || cli. rpcpassword . is_none ( )
219+ || cli. rpchost . is_none ( )
220+ || cli. pj_directory . is_none ( )
221+ || cli. ohttp_relays . is_none ( )
222+ {
223+ return Err ( anyhow:: anyhow!(
224+ "--set-config requires ALL of: \
225+ --rpcuser, --rpcpassword, --rpchost, \
226+ --pj-directory, --ohttp-relays"
227+ ) ) ;
228+ }
229+
230+ // Build the TOML map , this feels a bit hacky but it works
231+ let mut toml_map = toml:: map:: Map :: new ( ) ;
232+ let mut bitcoind_map = toml:: map:: Map :: new ( ) ;
233+
234+ bitcoind_map. insert ( "rpcuser" . into ( ) , toml:: Value :: String ( cli. rpcuser . clone ( ) . unwrap ( ) ) ) ;
235+ bitcoind_map
236+ . insert ( "rpcpassword" . into ( ) , toml:: Value :: String ( cli. rpcpassword . clone ( ) . unwrap ( ) ) ) ;
237+ bitcoind_map. insert (
238+ "rpchost" . into ( ) ,
239+ toml:: Value :: String ( cli. rpchost . clone ( ) . unwrap ( ) . to_string ( ) ) ,
240+ ) ;
241+ toml_map. insert ( "bitcoind" . into ( ) , toml:: Value :: Table ( bitcoind_map) ) ;
242+
243+ let mut v2_map = toml:: map:: Map :: new ( ) ;
244+ v2_map. insert (
245+ "pj_directory" . into ( ) ,
246+ toml:: Value :: String ( cli. pj_directory . as_ref ( ) . unwrap ( ) . to_string ( ) ) ,
247+ ) ;
248+ let relay_list: Vec < toml:: Value > = cli
249+ . ohttp_relays
250+ . as_ref ( )
251+ . unwrap ( )
252+ . iter ( )
253+ . map ( |url| toml:: Value :: String ( url. to_string ( ) ) )
254+ . collect ( ) ;
255+ v2_map. insert ( "ohttp_relays" . into ( ) , toml:: Value :: Array ( relay_list) ) ;
256+ toml_map. insert ( "v2" . into ( ) , toml:: Value :: Table ( v2_map) ) ;
257+
258+ let toml_content = toml:: Value :: Table ( toml_map) ;
259+ let toml_str = toml:: to_string_pretty ( & toml_content) ?;
260+
261+ let config_path = std:: env:: current_dir ( ) ?. join ( "config.toml" ) ;
262+ let final_path = if !config_path. exists ( ) {
263+ if let Some ( config_dir) = dirs:: config_dir ( ) {
264+ let global_dir = config_dir. join ( CONFIG_DIR ) ;
265+ std:: fs:: create_dir_all ( & global_dir) ?;
266+ global_dir. join ( "config.toml" )
267+ } else {
268+ config_path
269+ }
270+ } else {
271+ config_path
272+ } ;
273+
274+ std:: fs:: write ( final_path, toml_str) ?;
275+ Ok ( ( ) )
276+ }
277+
207278 #[ cfg( feature = "v1" ) ]
208279 pub fn v1 ( & self ) -> Result < & V1Config , anyhow:: Error > {
209280 match & self . version {
0 commit comments