Skip to content

Commit 61658b9

Browse files
authored
refactor(cli): extract duplicate config loading logic (#57)
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. Co-authored-by: dylankyc <[email protected]>
1 parent 7644591 commit 61658b9

File tree

1 file changed

+15
-20
lines changed

1 file changed

+15
-20
lines changed

clients/cli/src/config.rs

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,19 @@ use {
2929
std::{process::exit, rc::Rc, str::FromStr, sync::Arc, time::Duration},
3030
};
3131

32+
fn get_cli_config(matches: &ArgMatches) -> solana_cli_config::Config {
33+
if let Some(config_file) = matches.value_of("config_file") {
34+
solana_cli_config::Config::load(config_file).unwrap_or_else(|_| {
35+
eprintln!("error: Could not find config file `{}`", config_file);
36+
exit(1);
37+
})
38+
} else if let Some(config_file) = &*solana_cli_config::CONFIG_FILE {
39+
solana_cli_config::Config::load(config_file).unwrap_or_default()
40+
} else {
41+
solana_cli_config::Config::default()
42+
}
43+
}
44+
3245
type SignersOf = Vec<(Arc<dyn Signer>, Pubkey)>;
3346
fn signers_of(
3447
matches: &ArgMatches,
@@ -84,16 +97,7 @@ impl<'a> Config<'a> {
8497
bulk_signers: &mut Vec<Arc<dyn Signer>>,
8598
multisigner_ids: &'a mut Vec<Pubkey>,
8699
) -> Config<'a> {
87-
let cli_config = if let Some(config_file) = matches.value_of("config_file") {
88-
solana_cli_config::Config::load(config_file).unwrap_or_else(|_| {
89-
eprintln!("error: Could not find config file `{}`", config_file);
90-
exit(1);
91-
})
92-
} else if let Some(config_file) = &*solana_cli_config::CONFIG_FILE {
93-
solana_cli_config::Config::load(config_file).unwrap_or_default()
94-
} else {
95-
solana_cli_config::Config::default()
96-
};
100+
let cli_config = get_cli_config(matches);
97101
let json_rpc_url = normalize_to_url_if_moniker(
98102
matches
99103
.value_of("json_rpc_url")
@@ -165,16 +169,7 @@ impl<'a> Config<'a> {
165169
program_client: Arc<dyn ProgramClient<ProgramRpcClientSendTransaction>>,
166170
websocket_url: String,
167171
) -> Config<'a> {
168-
let cli_config = if let Some(config_file) = matches.value_of("config_file") {
169-
solana_cli_config::Config::load(config_file).unwrap_or_else(|_| {
170-
eprintln!("error: Could not find config file `{}`", config_file);
171-
exit(1);
172-
})
173-
} else if let Some(config_file) = &*solana_cli_config::CONFIG_FILE {
174-
solana_cli_config::Config::load(config_file).unwrap_or_default()
175-
} else {
176-
solana_cli_config::Config::default()
177-
};
172+
let cli_config = get_cli_config(matches);
178173
let multisigner_pubkeys =
179174
Self::extract_multisig_signers(matches, wallet_manager, bulk_signers, multisigner_ids);
180175

0 commit comments

Comments
 (0)