diff options
| author | DanConwayDev <DanConwayDev@protonmail.com> | 2024-07-15 17:14:09 +0100 |
|---|---|---|
| committer | DanConwayDev <DanConwayDev@protonmail.com> | 2024-07-15 17:18:36 +0100 |
| commit | 142fee58b0449b3fe3f436986339c318de66b33f (patch) | |
| tree | 61d4c78e4fed0024d426dd444424b58c36fecf5b /src/sub_commands/fetch.rs | |
| parent | ba82a894fad645757c49242c11573b6c5dd8d1e6 (diff) | |
feat(fetch): fetch events and save to cache
enabler to add simplicity, efficency and offline
capability to other functions
improve repo announcement selection
Diffstat (limited to 'src/sub_commands/fetch.rs')
| -rw-r--r-- | src/sub_commands/fetch.rs | 46 |
1 files changed, 46 insertions, 0 deletions
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 @@ | |||
| 1 | use std::collections::HashSet; | ||
| 2 | |||
| 3 | use anyhow::{Context, Result}; | ||
| 4 | use clap; | ||
| 5 | use nostr::nips::nip01::Coordinate; | ||
| 6 | |||
| 7 | #[cfg(not(test))] | ||
| 8 | use crate::client::Client; | ||
| 9 | #[cfg(test)] | ||
| 10 | use crate::client::MockConnect; | ||
| 11 | use crate::{ | ||
| 12 | client::Connect, | ||
| 13 | git::{Repo, RepoActions}, | ||
| 14 | repo_ref::get_repo_coordinates, | ||
| 15 | Cli, | ||
| 16 | }; | ||
| 17 | |||
| 18 | #[derive(clap::Args)] | ||
| 19 | pub struct SubCommandArgs { | ||
| 20 | /// address pointer to repo announcement | ||
| 21 | #[arg(long, action)] | ||
| 22 | repo: Vec<String>, | ||
| 23 | } | ||
| 24 | |||
| 25 | pub async fn launch(args: &Cli, command_args: &SubCommandArgs) -> Result<()> { | ||
| 26 | let _ = args; | ||
| 27 | let git_repo = Repo::discover().context("cannot find a git repository")?; | ||
| 28 | #[cfg(not(test))] | ||
| 29 | let client = Client::default(); | ||
| 30 | #[cfg(test)] | ||
| 31 | let client = <MockConnect as std::default::Default>::default(); | ||
| 32 | let repo_coordinates = if command_args.repo.is_empty() { | ||
| 33 | get_repo_coordinates(&git_repo, &client).await? | ||
| 34 | } else { | ||
| 35 | let mut repo_coordinates = HashSet::new(); | ||
| 36 | for repo in &command_args.repo { | ||
| 37 | repo_coordinates.insert(Coordinate::parse(repo.clone())?); | ||
| 38 | } | ||
| 39 | repo_coordinates | ||
| 40 | }; | ||
| 41 | client | ||
| 42 | .fetch_all(git_repo.get_path()?, &repo_coordinates) | ||
| 43 | .await?; | ||
| 44 | client.disconnect().await?; | ||
| 45 | Ok(()) | ||
| 46 | } | ||