From b7ee7a5678a6181ea4b6dcf8825ab2b63688b249 Mon Sep 17 00:00:00 2001 From: dylan Date: Thu, 9 Jan 2025 16:11:00 +0800 Subject: [PATCH] refactor(token/cli): extract duplicate config loading logic Extract duplicated config loading code into a new `get_cli_config` function to improve code maintainability and reduce duplication. The function handles loading config from file path or default locations. - Move common config loading logic into standalone function - Update new() and new_with_clients_and_ws_url to use shared function - No functional changes --- token/cli/src/config.rs | 35 +++++++++++++++-------------------- 1 file changed, 15 insertions(+), 20 deletions(-) diff --git a/token/cli/src/config.rs b/token/cli/src/config.rs index 74116217121..fe05b47ce3e 100644 --- a/token/cli/src/config.rs +++ b/token/cli/src/config.rs @@ -29,6 +29,19 @@ use { std::{process::exit, rc::Rc, str::FromStr, sync::Arc, time::Duration}, }; +fn get_cli_config(matches: &ArgMatches) -> solana_cli_config::Config { + if let Some(config_file) = matches.value_of("config_file") { + solana_cli_config::Config::load(config_file).unwrap_or_else(|_| { + eprintln!("error: Could not find config file `{}`", config_file); + exit(1); + }) + } else if let Some(config_file) = &*solana_cli_config::CONFIG_FILE { + solana_cli_config::Config::load(config_file).unwrap_or_default() + } else { + solana_cli_config::Config::default() + } +} + type SignersOf = Vec<(Arc, Pubkey)>; fn signers_of( matches: &ArgMatches, @@ -84,16 +97,7 @@ impl<'a> Config<'a> { bulk_signers: &mut Vec>, multisigner_ids: &'a mut Vec, ) -> Config<'a> { - let cli_config = if let Some(config_file) = matches.value_of("config_file") { - solana_cli_config::Config::load(config_file).unwrap_or_else(|_| { - eprintln!("error: Could not find config file `{}`", config_file); - exit(1); - }) - } else if let Some(config_file) = &*solana_cli_config::CONFIG_FILE { - solana_cli_config::Config::load(config_file).unwrap_or_default() - } else { - solana_cli_config::Config::default() - }; + let cli_config = get_cli_config(matches); let json_rpc_url = normalize_to_url_if_moniker( matches .value_of("json_rpc_url") @@ -165,16 +169,7 @@ impl<'a> Config<'a> { program_client: Arc>, websocket_url: String, ) -> Config<'a> { - let cli_config = if let Some(config_file) = matches.value_of("config_file") { - solana_cli_config::Config::load(config_file).unwrap_or_else(|_| { - eprintln!("error: Could not find config file `{}`", config_file); - exit(1); - }) - } else if let Some(config_file) = &*solana_cli_config::CONFIG_FILE { - solana_cli_config::Config::load(config_file).unwrap_or_default() - } else { - solana_cli_config::Config::default() - }; + let cli_config = get_cli_config(matches); let multisigner_pubkeys = Self::extract_multisig_signers(matches, wallet_manager, bulk_signers, multisigner_ids);