Skip to content

Commit 25a5b7d

Browse files
CLI: Read rpc_url from config file (#160)
- Similar to #156 --------- Co-authored-by: Evan B <[email protected]>
1 parent 37d6339 commit 25a5b7d

File tree

2 files changed

+17
-22
lines changed

2 files changed

+17
-22
lines changed

cli/src/args.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,8 @@ pub struct Args {
1212
#[arg(long, global = true, help = "Path to the configuration file")]
1313
pub config_file: Option<PathBuf>,
1414

15-
#[arg(
16-
long,
17-
global = true,
18-
env = "RPC_URL",
19-
default_value = "https://api.mainnet-beta.solana.com",
20-
help = "RPC URL to use"
21-
)]
22-
pub rpc_url: String,
15+
#[arg(long, global = true, env = "RPC_URL", help = "RPC URL to use")]
16+
pub rpc_url: Option<String>,
2317

2418
#[arg(
2519
long,
@@ -435,7 +429,7 @@ impl fmt::Display for Args {
435429

436430
// Network Configuration
437431
writeln!(f, "\n📡 Network Settings:")?;
438-
writeln!(f, " • RPC URL: {}", self.rpc_url)?;
432+
writeln!(f, " • RPC URL: {}", self.rpc_url.as_ref().unwrap_or(&String::new()))?;
439433
writeln!(f, " • Commitment: {}", self.commitment)?;
440434

441435
// Program IDs

cli/src/handler.rs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -66,22 +66,18 @@ pub struct CliHandler {
6666
}
6767

6868
impl CliHandler {
69+
/// Creates a new `CliHandler` instance from command-line arguments.
70+
///
71+
/// # Configuration Loading
72+
/// 1. If `args.config_file` is specified, loads from that file
73+
/// 2. Otherwise, loads from the default Solana CLI config file
6974
pub async fn from_args(args: &Args) -> Result<Self> {
70-
let rpc_url = args.rpc_url.clone();
7175
CommitmentConfig::confirmed();
72-
7376
let commitment = CommitmentConfig::from_str(&args.commitment)?;
7477

75-
let keypair = match &args.config_file {
76-
Some(config_file) => {
77-
let config = Config::load(config_file.as_os_str().to_str().unwrap())?;
78-
let keypair_path = match &args.keypair_path {
79-
Some(path) => path.as_str(),
80-
None => config.keypair_path.as_str(),
81-
};
82-
read_keypair_file(keypair_path)
83-
.map_err(|e| anyhow!("Failed to read keypair path: {e:?}"))?
84-
}
78+
// Load config - either from specified file or default
79+
let config = match &args.config_file {
80+
Some(config_file) => Config::load(config_file.as_os_str().to_str().unwrap())?,
8581
None => {
8682
let config_file = solana_cli_config::CONFIG_FILE
8783
.as_ref()
@@ -91,10 +87,15 @@ impl CliHandler {
9187
.map_err(|e| anyhow!("Failed to read keypair path: {e:?}"))?,
9288
Err(_) => read_keypair_file(args.keypair_path.clone().unwrap())
9389
.map_err(|e| anyhow!("Failed to read keypair path: {e:?}"))?,
94-
}
9590
}
9691
};
9792

93+
let keypair_path = args.keypair_path.as_deref().unwrap_or(&config.keypair_path);
94+
let keypair = read_keypair_file(keypair_path)
95+
.map_err(|e| anyhow!("Failed to read keypair path: {e:?}"))?;
96+
97+
let rpc_url = args.rpc_url.clone().unwrap_or(config.json_rpc_url);
98+
9899
let restaking_program_id = Pubkey::from_str(&args.restaking_program_id)?;
99100
let vault_program_id = Pubkey::from_str(&args.vault_program_id)?;
100101
let tip_router_program_id = Pubkey::from_str(&args.tip_router_program_id)?;

0 commit comments

Comments
 (0)