From 1b361355b04808ef998d970b91d75e9896518211 Mon Sep 17 00:00:00 2001 From: xeruf <27jf@pm.me> Date: Sat, 27 Jul 2024 22:40:52 +0300 Subject: [PATCH] feat(main): relay override by environment variable --- src/main.rs | 45 ++++++++++++++++++++++++++------------------- 1 file changed, 26 insertions(+), 19 deletions(-) diff --git a/src/main.rs b/src/main.rs index 0851472..260a61e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,4 @@ -use std::env::args; +use std::env::{args, var}; use std::fmt::Display; use std::fs; use std::fs::File; @@ -84,26 +84,33 @@ async fn main() { let client = Client::new(&keys); println!("My public key: {}", keys.public_key()); - match File::open(&relayfile).map(|f| BufReader::new(f).lines().flatten()) { - Ok(lines) => { - for line in lines { - or_print(client.add_relay(line).await); - } + match var("MOSTR_RELAY") { + Ok(relay) => { + or_print(client.add_relay(relay).await); } - Err(e) => { - eprintln!("Could not read relays file: {}", e); - if let Some(line) = prompt("Relay?") { - let url = if line.contains("://") { line } else { "wss://".to_string() + &line }; - or_print( - client - .add_relay(url.clone()) - .await, - ).map(|bool| { - if bool { - or_print(fs::write(&relayfile, url)); + _ => { + match File::open(&relayfile).map(|f| BufReader::new(f).lines().flatten()) { + Ok(lines) => { + for line in lines { + or_print(client.add_relay(line).await); } - }); - }; + } + Err(e) => { + eprintln!("Could not read relays file: {}", e); + if let Some(line) = prompt("Relay?") { + let url = if line.contains("://") { line } else { "wss://".to_string() + &line }; + or_print( + client + .add_relay(url.clone()) + .await, + ).map(|bool| { + if bool { + or_print(fs::write(&relayfile, url)); + } + }); + }; + } + } } }