diff options
Diffstat (limited to 'src/bin/ngit/sub_commands/init.rs')
| -rw-r--r-- | src/bin/ngit/sub_commands/init.rs | 81 |
1 files changed, 72 insertions, 9 deletions
diff --git a/src/bin/ngit/sub_commands/init.rs b/src/bin/ngit/sub_commands/init.rs index 80e182b..f26eea3 100644 --- a/src/bin/ngit/sub_commands/init.rs +++ b/src/bin/ngit/sub_commands/init.rs | |||
| @@ -1,9 +1,18 @@ | |||
| 1 | use std::collections::HashMap; | 1 | use std::collections::{HashMap, HashSet}; |
| 2 | 2 | ||
| 3 | use anyhow::{Context, Result}; | 3 | use anyhow::{Context, Result}; |
| 4 | use console::Style; | 4 | use console::{Style, Term}; |
| 5 | use ngit::{cli_interactor::PromptConfirmParms, git::nostr_url::NostrUrlDecoded}; | 5 | use ngit::{ |
| 6 | use nostr::{FromBech32, PublicKey, ToBech32, nips::nip01::Coordinate}; | 6 | cli_interactor::PromptConfirmParms, |
| 7 | git::nostr_url::{NostrUrlDecoded, save_nip05_to_git_config_cache}, | ||
| 8 | }; | ||
| 9 | use nostr::{ | ||
| 10 | FromBech32, PublicKey, ToBech32, | ||
| 11 | nips::{ | ||
| 12 | nip01::Coordinate, | ||
| 13 | nip05::{self}, | ||
| 14 | }, | ||
| 15 | }; | ||
| 7 | use nostr_sdk::{Kind, RelayUrl}; | 16 | use nostr_sdk::{Kind, RelayUrl}; |
| 8 | 17 | ||
| 9 | use crate::{ | 18 | use crate::{ |
| @@ -397,7 +406,7 @@ pub async fn launch(cli_args: &Cli, args: &SubCommandArgs) -> Result<()> { | |||
| 397 | 406 | ||
| 398 | println!("publishing repostory reference..."); | 407 | println!("publishing repostory reference..."); |
| 399 | 408 | ||
| 400 | let repo_ref = RepoRef { | 409 | let mut repo_ref = RepoRef { |
| 401 | identifier: identifier.clone(), | 410 | identifier: identifier.clone(), |
| 402 | name, | 411 | name, |
| 403 | description, | 412 | description, |
| @@ -408,6 +417,7 @@ pub async fn launch(cli_args: &Cli, args: &SubCommandArgs) -> Result<()> { | |||
| 408 | trusted_maintainer: user_ref.public_key, | 417 | trusted_maintainer: user_ref.public_key, |
| 409 | maintainers: maintainers.clone(), | 418 | maintainers: maintainers.clone(), |
| 410 | events: HashMap::new(), | 419 | events: HashMap::new(), |
| 420 | nostr_git_url: None, | ||
| 411 | }; | 421 | }; |
| 412 | let repo_event = repo_ref.to_event(&signer).await?; | 422 | let repo_event = repo_ref.to_event(&signer).await?; |
| 413 | 423 | ||
| @@ -437,19 +447,72 @@ pub async fn launch(cli_args: &Cli, args: &SubCommandArgs) -> Result<()> { | |||
| 437 | false, | 447 | false, |
| 438 | )?; | 448 | )?; |
| 439 | 449 | ||
| 440 | let relays = relays | 450 | // if nip05 valid, set nostr git url to use that format |
| 441 | .iter() | 451 | let hint_for_nip05_address = { |
| 442 | .map(std::string::ToString::to_string) | 452 | if let Some(nip05) = user_ref.metadata.nip05 { |
| 443 | .collect::<Vec<String>>(); | 453 | let term = Term::stdout(); |
| 454 | term.write_line(&format!("fetching nip05 details for {nip05}..."))?; | ||
| 455 | if let Ok(nprofile) = nip05::profile(nip05.clone(), None).await { | ||
| 456 | let _ = term.clear_last_lines(1); | ||
| 457 | let _ = | ||
| 458 | save_nip05_to_git_config_cache(&nip05, &nprofile.public_key, &Some(&git_repo)); | ||
| 459 | // Normalize URLs before doing the intersection. | ||
| 460 | let repo_relays: HashSet<RelayUrl> = relays | ||
| 461 | .iter() | ||
| 462 | .map(|r| RelayUrl::parse(r.as_str_without_trailing_slash()).unwrap()) | ||
| 463 | .collect(); | ||
| 464 | let nip05_relays: HashSet<RelayUrl> = nprofile | ||
| 465 | .relays | ||
| 466 | .iter() | ||
| 467 | .map(|r| RelayUrl::parse(r.as_str_without_trailing_slash()).unwrap()) | ||
| 468 | .collect(); | ||
| 469 | let mut inter = repo_relays.intersection(&nip05_relays); | ||
| 470 | |||
| 471 | repo_ref.set_nostr_git_url(NostrUrlDecoded { | ||
| 472 | original_string: String::new(), | ||
| 473 | nip05: Some(nip05.clone()), | ||
| 474 | coordinate: Coordinate { | ||
| 475 | kind: Kind::GitRepoAnnouncement, | ||
| 476 | public_key: user_ref.public_key, | ||
| 477 | identifier: repo_ref.identifier.clone(), | ||
| 478 | relays: if inter.next().is_some() || relays.is_empty() { | ||
| 479 | vec![] | ||
| 480 | } else { | ||
| 481 | vec![relays.first().unwrap().clone()] | ||
| 482 | }, | ||
| 483 | }, | ||
| 484 | protocol: None, | ||
| 485 | user: None, | ||
| 486 | }); | ||
| 487 | if inter.next().is_some() { | ||
| 488 | "note: point your NIP-05 relays to one of the repo relays for a cleaner nostr:// remote URL.".to_string() | ||
| 489 | } else { | ||
| 490 | String::new() | ||
| 491 | } | ||
| 492 | } else { | ||
| 493 | "note: could not validate your nip05 address {nip05} which could be used for a shorter nostr:// remote URL.".to_string() | ||
| 494 | } | ||
| 495 | } else { | ||
| 496 | String::new() | ||
| 497 | } | ||
| 498 | }; | ||
| 444 | 499 | ||
| 445 | prompt_to_set_nostr_url_as_origin(&repo_ref, &git_repo).await?; | 500 | prompt_to_set_nostr_url_as_origin(&repo_ref, &git_repo).await?; |
| 446 | 501 | ||
| 502 | if !hint_for_nip05_address.is_empty() { | ||
| 503 | println!("{hint_for_nip05_address}"); | ||
| 504 | } | ||
| 505 | |||
| 447 | // TODO: if no state event exists and there is currently a remote called | 506 | // TODO: if no state event exists and there is currently a remote called |
| 448 | // "origin", automtically push rather than waiting for the next commit | 507 | // "origin", automtically push rather than waiting for the next commit |
| 449 | 508 | ||
| 450 | // no longer create a new maintainers.yaml file - its too confusing for users | 509 | // no longer create a new maintainers.yaml file - its too confusing for users |
| 451 | // as it falls out of sync with data in nostr event . update if it already | 510 | // as it falls out of sync with data in nostr event . update if it already |
| 452 | // exists | 511 | // exists |
| 512 | let relays = relays | ||
| 513 | .iter() | ||
| 514 | .map(std::string::ToString::to_string) | ||
| 515 | .collect::<Vec<String>>(); | ||
| 453 | if match &repo_config_result { | 516 | if match &repo_config_result { |
| 454 | Ok(config) => { | 517 | Ok(config) => { |
| 455 | !<std::option::Option<std::string::String> as Clone>::clone(&config.identifier) | 518 | !<std::option::Option<std::string::String> as Clone>::clone(&config.identifier) |