feat: Support relays and naddrs sets
This commit introduces support for 'sets', a feature that simplifies referencing multiple relays or naddrs with a single word. Sets are stored in the config directory under `n34` as `config.toml` in `TOML` format. The config path follows the `$XDG_CONFIG_HOME` standard, defaulting to `$HOME/.config` if unset. The `n34 sets` command allows managing sets, adding, removing, updating, or displaying them. When using the main `--relays` flag, a set name can be provided to automatically expand its relays (an error occurs if the set contains no relays). Similarly, any command accepting naddrs can reference a set to extract its naddrs. Sets can also be merged by updating a set while specifying another set in `--repo` merges their naddrs, while `--set-relays` merges their relays. Removal follows the same pattern. Signed-off-by: Awiteb <a@4rs.nl>
This commit is contained in:
@@ -21,7 +21,7 @@ use futures::future;
|
||||
use nostr::{event::EventBuilder, key::PublicKey, types::Url};
|
||||
|
||||
use crate::{
|
||||
cli::{CliOptions, CommandRunner, NOSTR_ADDRESS_FILE},
|
||||
cli::{CliConfig, CliOptions, CommandRunner, NOSTR_ADDRESS_FILE, types::RelayOrSetVecExt},
|
||||
error::N34Result,
|
||||
nostr_utils::{NostrClient, traits::NewGitRepositoryAnnouncement, utils},
|
||||
};
|
||||
@@ -81,7 +81,9 @@ impl CommandRunner for AnnounceArgs {
|
||||
async fn run(mut self, options: CliOptions) -> N34Result<()> {
|
||||
options.ensure_relays()?;
|
||||
|
||||
let client = NostrClient::init(&options).await;
|
||||
let config = CliConfig::load_toml(&options.config_path)?;
|
||||
let relays = options.relays.clone().flat_relays(&config.sets)?;
|
||||
let client = NostrClient::init(&options, &relays).await;
|
||||
let user_pubk = options.pubkey().await?;
|
||||
let relays_list = client.user_relays_list(user_pubk).await?;
|
||||
|
||||
@@ -89,14 +91,14 @@ impl CommandRunner for AnnounceArgs {
|
||||
self.maintainers.insert(0, user_pubk);
|
||||
}
|
||||
|
||||
let naddr = utils::repo_naddr(&self.repo_id, user_pubk, &options.relays)?;
|
||||
let naddr = utils::repo_naddr(&self.repo_id, user_pubk, &relays)?;
|
||||
let event = EventBuilder::new_git_repo(
|
||||
self.repo_id,
|
||||
self.name.map(utils::str_trim),
|
||||
self.description.map(utils::str_trim),
|
||||
self.web,
|
||||
self.clone,
|
||||
options.relays.clone(),
|
||||
relays.clone(),
|
||||
self.maintainers.clone(),
|
||||
self.label.into_iter().map(utils::str_trim).collect(),
|
||||
self.force_id,
|
||||
@@ -125,7 +127,7 @@ impl CommandRunner for AnnounceArgs {
|
||||
}
|
||||
|
||||
let write_relays = [
|
||||
options.relays.clone(),
|
||||
relays,
|
||||
utils::add_write_relays(relays_list.as_ref()),
|
||||
// Include read relays for each maintainer (if found)
|
||||
future::join_all(
|
||||
|
||||
@@ -17,10 +17,14 @@
|
||||
use std::fmt;
|
||||
|
||||
use clap::Args;
|
||||
use nostr::nips::nip19::Nip19Coordinate;
|
||||
|
||||
use crate::{
|
||||
cli::{CliOptions, CommandRunner, parsers},
|
||||
cli::{
|
||||
CliConfig,
|
||||
CliOptions,
|
||||
CommandRunner,
|
||||
types::{NaddrOrSet, OptionNaddrOrSetVecExt, RelayOrSetVecExt},
|
||||
},
|
||||
error::N34Result,
|
||||
nostr_utils::{NostrClient, traits::NaddrsUtils, utils},
|
||||
};
|
||||
@@ -28,20 +32,25 @@ use crate::{
|
||||
/// Arguments for the `repo view` command
|
||||
#[derive(Args, Debug)]
|
||||
pub struct ViewArgs {
|
||||
/// Repository address in `naddr` format or `<nip5>/repo_id`. e.g.
|
||||
/// `4rs.nl/n34` and `_@4rs.nl/n34`
|
||||
/// Repository address in `naddr` format (`naddr1...`), NIP-05 format
|
||||
/// (`4rs.nl/n34` or `_@4rs.nl/n34`), or a set name like `kernel`.
|
||||
///
|
||||
/// If not provided, `n34` will look for the `nostr-address` file.
|
||||
#[arg(value_name = "NADDR-OR-NIP05", long = "repo", value_parser = parsers::repo_naddr)]
|
||||
naddrs: Option<Vec<Nip19Coordinate>>,
|
||||
/// If omitted, looks for a `nostr-address` file.
|
||||
#[arg(value_name = "NADDR-NIP05-OR-SET", long = "repo")]
|
||||
naddrs: Option<Vec<NaddrOrSet>>,
|
||||
}
|
||||
|
||||
impl CommandRunner for ViewArgs {
|
||||
async fn run(self, options: CliOptions) -> N34Result<()> {
|
||||
// FIXME: The signer is not required here
|
||||
|
||||
let naddrs = utils::naddrs_or_file(self.naddrs, &utils::nostr_address_path()?)?;
|
||||
let client = NostrClient::init(&options).await;
|
||||
let config = CliConfig::load_toml(&options.config_path)?;
|
||||
let naddrs = utils::naddrs_or_file(
|
||||
self.naddrs.flat_naddrs(&config.sets)?,
|
||||
&utils::nostr_address_path()?,
|
||||
)?;
|
||||
let relays = options.relays.clone().flat_relays(&config.sets)?;
|
||||
let client = NostrClient::init(&options, &relays).await;
|
||||
client.add_relays(&naddrs.extract_relays()).await;
|
||||
|
||||
let repos = client.fetch_repos(&naddrs.into_coordinates()).await?;
|
||||
|
||||
Reference in New Issue
Block a user