From d64748b810bf64638a5eb71eac054d91dae0c0f1 Mon Sep 17 00:00:00 2001 From: DanConwayDev Date: Wed, 21 May 2025 12:32:41 +0100 Subject: feat: add support for default relay overrides via git config so they can be overwritten locally and globally --- src/bin/git_remote_nostr/main.rs | 8 ++++++-- src/bin/ngit/sub_commands/init.rs | 3 ++- src/bin/ngit/sub_commands/list.rs | 6 ++++-- src/bin/ngit/sub_commands/login.rs | 11 ++++++---- src/bin/ngit/sub_commands/send.rs | 7 +++++-- src/lib/client.rs | 42 +++++++++++++++++++++++++++++++++++++- 6 files changed, 65 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/bin/git_remote_nostr/main.rs b/src/bin/git_remote_nostr/main.rs index daa924f..29731e2 100644 --- a/src/bin/git_remote_nostr/main.rs +++ b/src/bin/git_remote_nostr/main.rs @@ -14,7 +14,11 @@ use std::{ use anyhow::{Context, Result, bail}; use client::{Connect, consolidate_fetch_reports, get_repo_ref_from_cache}; use git::{RepoActions, nostr_url::NostrUrlDecoded}; -use ngit::{client, git, login::existing::load_existing_login}; +use ngit::{ + client::{self, Params}, + git, + login::existing::load_existing_login, +}; use nostr::nips::nip19::Nip19Coordinate; use utils::read_line; @@ -33,7 +37,7 @@ async fn main() -> Result<()> { let git_repo_path = git_repo.get_path()?; - let mut client = Client::default(); + let mut client = Client::new(Params::with_git_config_relay_defaults(&Some(&git_repo))); if let Ok((signer, _, _)) = load_existing_login( &Some(&git_repo), diff --git a/src/bin/ngit/sub_commands/init.rs b/src/bin/ngit/sub_commands/init.rs index 3c58a52..ee5f1ab 100644 --- a/src/bin/ngit/sub_commands/init.rs +++ b/src/bin/ngit/sub_commands/init.rs @@ -4,6 +4,7 @@ use anyhow::{Context, Result}; use console::{Style, Term}; use ngit::{ cli_interactor::PromptConfirmParms, + client::Params, git::nostr_url::{NostrUrlDecoded, save_nip05_to_git_config_cache}, }; use nostr::{ @@ -68,7 +69,7 @@ pub async fn launch(cli_args: &Cli, args: &SubCommandArgs) -> Result<()> { // TODO: check for empty repo // TODO: check for existing maintaiers file - let mut client = Client::default(); + let mut client = Client::new(Params::with_git_config_relay_defaults(&Some(&git_repo))); let repo_coordinate = (try_and_get_repo_coordinates_when_remote_unknown(&git_repo).await).ok(); diff --git a/src/bin/ngit/sub_commands/list.rs b/src/bin/ngit/sub_commands/list.rs index e8d2e97..2c91e66 100644 --- a/src/bin/ngit/sub_commands/list.rs +++ b/src/bin/ngit/sub_commands/list.rs @@ -2,7 +2,9 @@ use std::{io::Write, ops::Add}; use anyhow::{Context, Result, bail}; use ngit::{ - client::{get_all_proposal_patch_events_from_cache, get_proposals_and_revisions_from_cache}, + client::{ + Params, get_all_proposal_patch_events_from_cache, get_proposals_and_revisions_from_cache, + }, git_events::{ get_commit_id_from_patch, get_most_recent_patch_with_ancestors, status_kinds, tag_value, }, @@ -31,7 +33,7 @@ pub async fn launch() -> Result<()> { // TODO: check for existing maintaiers file // TODO: check for other claims - let client = Client::default(); + let client = Client::new(Params::with_git_config_relay_defaults(&Some(&git_repo))); let repo_coordinates = get_repo_coordinates_when_remote_unknown(&git_repo, &client).await?; diff --git a/src/bin/ngit/sub_commands/login.rs b/src/bin/ngit/sub_commands/login.rs index e76a089..ed2414a 100644 --- a/src/bin/ngit/sub_commands/login.rs +++ b/src/bin/ngit/sub_commands/login.rs @@ -2,6 +2,7 @@ use anyhow::{Context, Result}; use clap; use ngit::{ cli_interactor::{Interactor, InteractorPrompt, PromptChoiceParms}, + client::Params, git::{get_git_config_item, remove_git_config_item}, login::{SignerInfoSource, existing::load_existing_login}, }; @@ -25,15 +26,17 @@ pub struct SubCommandArgs { } pub async fn launch(args: &Cli, command_args: &SubCommandArgs) -> Result<()> { + let git_repo_result = Repo::discover().context("failed to find a git repository"); + let git_repo = { git_repo_result.ok() }; + let client = if command_args.offline { None } else { - Some(Client::default()) + Some(Client::new(Params::with_git_config_relay_defaults( + &git_repo.as_ref(), + ))) }; - let git_repo_result = Repo::discover().context("failed to find a git repository"); - let git_repo = { git_repo_result.ok() }; - let (logged_out, log_in_locally_only) = logout(git_repo.as_ref(), command_args.local).await?; if logged_out || log_in_locally_only { fresh_login_or_signup( diff --git a/src/bin/ngit/sub_commands/send.rs b/src/bin/ngit/sub_commands/send.rs index 9fc00d9..5a5acc8 100644 --- a/src/bin/ngit/sub_commands/send.rs +++ b/src/bin/ngit/sub_commands/send.rs @@ -2,7 +2,10 @@ use std::path::Path; use anyhow::{Context, Result, bail}; use console::Style; -use ngit::{client::send_events, git_events::generate_cover_letter_and_patch_events}; +use ngit::{ + client::{Params, send_events}, + git_events::generate_cover_letter_and_patch_events, +}; use nostr::{ ToBech32, nips::{nip10::Marker, nip19::Nip19Event}, @@ -52,7 +55,7 @@ pub async fn launch(cli_args: &Cli, args: &SubCommandArgs, no_fetch: bool) -> Re .get_main_or_master_branch() .context("the default branches (main or master) do not exist")?; - let mut client = Client::default(); + let mut client = Client::new(Params::with_git_config_relay_defaults(&Some(&git_repo))); let repo_coordinates = get_repo_coordinates_when_remote_unknown(&git_repo, &client).await?; diff --git a/src/lib/client.rs b/src/lib/client.rs index 6c6d81e..7cd3c46 100644 --- a/src/lib/client.rs +++ b/src/lib/client.rs @@ -44,7 +44,7 @@ use nostr_sdk::{ use crate::{ get_dirs, - git::{Repo, RepoActions}, + git::{Repo, RepoActions, get_git_config_item}, git_events::{ event_is_cover_letter, event_is_patch_set_root, event_is_revision_root, status_kinds, }, @@ -659,6 +659,46 @@ impl Default for Params { } } } +impl Params { + pub fn with_git_config_relay_defaults(git_repo: &Option<&Repo>) -> Self { + let mut params = Params::default(); + if std::env::var("NGITTEST").is_err() { + // ignore git config settings under test + if let Ok(Some(relay_defaults)) = + get_git_config_item(git_repo, "nostr.relay-default-set") + { + let new_default_relays: Vec = relay_defaults + .split(';') + .filter_map(|url| RelayUrl::parse(url).ok()) // Attempt to parse and filter out errors + .map(|relay_url| relay_url.to_string()) // Convert RelayUrl back to String + .collect(); + // elsewhere it is assumed this isn't empty + if !new_default_relays.is_empty() { + params.fallback_relays = new_default_relays; + } + } + if let Ok(Some(relay_blasters)) = + get_git_config_item(git_repo, "nostr.relay-blaster-set") + { + params.blaster_relays = relay_blasters + .split(';') + .filter_map(|url| RelayUrl::parse(url).ok()) // Attempt to parse and filter out errors + .map(|relay_url| relay_url.to_string()) // Convert RelayUrl back to String + .collect(); + } + if let Ok(Some(relay_signer)) = + get_git_config_item(git_repo, "nostr.relay-signer-fallback-set") + { + params.fallback_signer_relays = relay_signer + .split(';') + .filter_map(|url| RelayUrl::parse(url).ok()) // Attempt to parse and filter out errors + .map(|relay_url| relay_url.to_string()) // Convert RelayUrl back to String + .collect(); + } + } + params + } +} fn get_dedup_events(relay_results: Vec>>) -> Vec { let mut dedup_events: Vec = vec![]; -- cgit v1.2.3