From 142fee58b0449b3fe3f436986339c318de66b33f Mon Sep 17 00:00:00 2001 From: DanConwayDev Date: Mon, 15 Jul 2024 17:14:09 +0100 Subject: feat(fetch): fetch events and save to cache enabler to add simplicity, efficency and offline capability to other functions improve repo announcement selection --- src/sub_commands/fetch.rs | 46 ++++++++++++++++++++++++++++++++++++++++++++++ src/sub_commands/init.rs | 12 ++++++++++-- src/sub_commands/list.rs | 2 +- src/sub_commands/mod.rs | 1 + 4 files changed, 58 insertions(+), 3 deletions(-) create mode 100644 src/sub_commands/fetch.rs (limited to 'src/sub_commands') diff --git a/src/sub_commands/fetch.rs b/src/sub_commands/fetch.rs new file mode 100644 index 0000000..07fd6f9 --- /dev/null +++ b/src/sub_commands/fetch.rs @@ -0,0 +1,46 @@ +use std::collections::HashSet; + +use anyhow::{Context, Result}; +use clap; +use nostr::nips::nip01::Coordinate; + +#[cfg(not(test))] +use crate::client::Client; +#[cfg(test)] +use crate::client::MockConnect; +use crate::{ + client::Connect, + git::{Repo, RepoActions}, + repo_ref::get_repo_coordinates, + Cli, +}; + +#[derive(clap::Args)] +pub struct SubCommandArgs { + /// address pointer to repo announcement + #[arg(long, action)] + repo: Vec, +} + +pub async fn launch(args: &Cli, command_args: &SubCommandArgs) -> Result<()> { + let _ = args; + let git_repo = Repo::discover().context("cannot find a git repository")?; + #[cfg(not(test))] + let client = Client::default(); + #[cfg(test)] + let client = ::default(); + let repo_coordinates = if command_args.repo.is_empty() { + get_repo_coordinates(&git_repo, &client).await? + } else { + let mut repo_coordinates = HashSet::new(); + for repo in &command_args.repo { + repo_coordinates.insert(Coordinate::parse(repo.clone())?); + } + repo_coordinates + }; + client + .fetch_all(git_repo.get_path()?, &repo_coordinates) + .await?; + client.disconnect().await?; + Ok(()) +} diff --git a/src/sub_commands/init.rs b/src/sub_commands/init.rs index 57785db..db90acd 100644 --- a/src/sub_commands/init.rs +++ b/src/sub_commands/init.rs @@ -1,3 +1,5 @@ +use std::collections::HashMap; + use anyhow::{Context, Result}; use nostr::{FromBech32, PublicKey, ToBech32}; @@ -291,7 +293,7 @@ pub async fn launch(cli_args: &Cli, args: &SubCommandArgs) -> Result<()> { println!("publishing repostory reference..."); let repo_event = RepoRef { - identifier, + identifier: identifier.clone(), name, description, root_commit: earliest_unique_commit, @@ -299,6 +301,7 @@ pub async fn launch(cli_args: &Cli, args: &SubCommandArgs) -> Result<()> { web, relays: relays.clone(), maintainers: maintainers.clone(), + events: HashMap::new(), } .to_event(&signer) .await?; @@ -322,7 +325,12 @@ pub async fn launch(cli_args: &Cli, args: &SubCommandArgs) -> Result<()> { } Err(_) => true, } { - save_repo_config_to_yaml(&git_repo, maintainers.clone(), relays.clone())?; + save_repo_config_to_yaml( + &git_repo, + identifier.clone(), + maintainers.clone(), + relays.clone(), + )?; println!( "maintainers.yaml {}. commit and push.", if repo_config_result.is_err() { diff --git a/src/sub_commands/list.rs b/src/sub_commands/list.rs index 5dc868c..d3f583f 100644 --- a/src/sub_commands/list.rs +++ b/src/sub_commands/list.rs @@ -820,7 +820,7 @@ pub static STATUS_KIND_APPLIED: u16 = 1631; pub static STATUS_KIND_CLOSED: u16 = 1632; pub static STATUS_KIND_DRAFT: u16 = 1633; -fn status_kinds() -> Vec { +pub fn status_kinds() -> Vec { vec![ nostr::Kind::Custom(STATUS_KIND_OPEN), nostr::Kind::Custom(STATUS_KIND_APPLIED), diff --git a/src/sub_commands/mod.rs b/src/sub_commands/mod.rs index 9f97b7e..29a60f9 100644 --- a/src/sub_commands/mod.rs +++ b/src/sub_commands/mod.rs @@ -1,3 +1,4 @@ +pub mod fetch; pub mod init; pub mod list; pub mod login; -- cgit v1.2.3