Skip to content

Commit abeaef7

Browse files
committed
cli: resolve MOSTRO_PUBKEY from CLI flag or env without panic
1 parent a2f0716 commit abeaef7

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

src/cli.rs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,7 @@ use clap::{Parser, Subcommand};
4040
use mostro_core::prelude::*;
4141
use nostr_sdk::prelude::*;
4242
use sqlx::SqlitePool;
43-
use std::{
44-
env::{set_var, var},
45-
str::FromStr,
46-
};
43+
use std::{env::set_var, str::FromStr};
4744
use take_dispute::*;
4845
use uuid::Uuid;
4946

@@ -315,7 +312,6 @@ fn get_env_var(cli: &Cli) {
315312
if let Some(ref mostro_pubkey) = cli.mostropubkey {
316313
set_var("MOSTRO_PUBKEY", mostro_pubkey.clone());
317314
}
318-
let _pubkey = var("MOSTRO_PUBKEY").expect("$MOSTRO_PUBKEY env var needs to be set");
319315

320316
if let Some(ref relays) = cli.relays {
321317
set_var("RELAYS", relays.clone());
@@ -374,6 +370,20 @@ pub async fn run() -> Result<()> {
374370
Ok(())
375371
}
376372

373+
fn resolve_mostro_pubkey(cli: &Cli) -> Result<String> {
374+
cli.mostropubkey
375+
.clone()
376+
.or_else(|| std::env::var("MOSTRO_PUBKEY").ok())
377+
.ok_or_else(|| {
378+
anyhow::anyhow!(
379+
"MOSTRO_PUBKEY not set.\n\
380+
Provide it using one of the following methods:\n\
381+
1) --mostropubkey <npub>\n\
382+
2) export MOSTRO_PUBKEY=<npub>"
383+
)
384+
})
385+
}
386+
377387
async fn init_context(cli: &Cli) -> Result<Context> {
378388
// Get environment variables
379389
get_env_var(cli);
@@ -407,10 +417,8 @@ async fn init_context(cli: &Cli) -> Result<Context> {
407417
};
408418

409419
// Resolve Mostro pubkey from env (required for all flows)
410-
let mostro_pubkey = PublicKey::from_str(
411-
&std::env::var("MOSTRO_PUBKEY")
412-
.map_err(|e| anyhow::anyhow!("Failed to get MOSTRO_PUBKEY: {}", e))?,
413-
)?;
420+
421+
let mostro_pubkey = PublicKey::from_str(&resolve_mostro_pubkey(cli)?)?;
414422

415423
// Connect to Nostr relays
416424
let client = util::connect_nostr().await?;

0 commit comments

Comments
 (0)