upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/src/lib
diff options
context:
space:
mode:
authorDanConwayDev <DanConwayDev@protonmail.com>2024-11-25 12:31:50 +0000
committerDanConwayDev <DanConwayDev@protonmail.com>2024-11-25 12:31:50 +0000
commitb09cf3ef8bbb5d31e4c424ff28282f5029f95fb2 (patch)
tree7081e193a6dfa0dc076191dcd66ea974df69695b /src/lib
parent264c6dfe4229cd73bf017f55be0178d596cf06eb (diff)
fix(login): handle git config save errors
to guide the users how to login dispite the git config errors
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/login/fresh.rs83
1 files changed, 80 insertions, 3 deletions
diff --git a/src/lib/login/fresh.rs b/src/lib/login/fresh.rs
index e8cfcb8..194f638 100644
--- a/src/lib/login/fresh.rs
+++ b/src/lib/login/fresh.rs
@@ -93,7 +93,7 @@ pub async fn fresh_login_or_signup(
93 } 93 }
94 } 94 }
95 }; 95 };
96 let _ = save_to_git_config(git_repo, &signer_info, !save_local); 96 let _ = save_to_git_config(git_repo, &signer_info, !save_local).await;
97 let user_ref = get_user_details( 97 let user_ref = get_user_details(
98 &public_key, 98 &public_key,
99 client, 99 client,
@@ -483,7 +483,7 @@ fn generate_qr(data: &str) -> Result<Vec<String>> {
483 Ok(lines) 483 Ok(lines)
484} 484}
485 485
486fn save_to_git_config( 486async fn save_to_git_config(
487 git_repo: &Option<&Repo>, 487 git_repo: &Option<&Repo>,
488 signer_info: &SignerInfo, 488 signer_info: &SignerInfo,
489 global: bool, 489 global: bool,
@@ -516,7 +516,64 @@ fn save_to_git_config(
516 } 516 }
517 } 517 }
518 if global { 518 if global {
519 save_to_git_config(git_repo, signer_info, false)? 519 loop {
520 match Interactor::default().choice(
521 PromptChoiceParms::default()
522 .with_default(0)
523 .with_choices(vec![
524 "i'll update global git config manually with above values".to_string(),
525 "only log into local git repository (save to local git config)"
526 .to_string(),
527 "one time login".to_string(),
528 ]),
529 )? {
530 0 => {
531 // check
532 if let Ok((_, user_ref, _)) = load_existing_login(
533 git_repo,
534 &None,
535 &None,
536 &Some(SignerInfoSource::GitGlobal),
537 None,
538 true,
539 true,
540 )
541 .await
542 {
543 if user_ref.public_key == get_pubkey_from_signer_info(signer_info)? {
544 return Ok(());
545 } else {
546 eprintln!(
547 "global git config hasn't been updated with different npub"
548 );
549 }
550 } else {
551 eprintln!(
552 "global git config hasn't been updated with nostr login values"
553 );
554 }
555 }
556 1 => {
557 if let Err(error) =
558 silently_save_to_git_config(git_repo, signer_info, false).context(
559 format!(
560 "failed to save login details to {} git config",
561 if global { "global" } else { "local" }
562 ),
563 )
564 {
565 eprintln!("Error: {:?}", error);
566 eprintln!("login details were not saved");
567 } else {
568 eprintln!(
569 "saved login details to local git config. you are only logged in to this local repository."
570 );
571 }
572 return Ok(());
573 }
574 _ => return Ok(()),
575 }
576 }
520 } 577 }
521 Err(error) 578 Err(error)
522 } else { 579 } else {
@@ -532,6 +589,26 @@ fn save_to_git_config(
532 } 589 }
533} 590}
534 591
592fn get_pubkey_from_signer_info(signer_info: &SignerInfo) -> Result<PublicKey> {
593 let npub = match signer_info {
594 SignerInfo::Bunker {
595 bunker_uri: _,
596 bunker_app_key: _,
597 npub,
598 } => npub,
599 SignerInfo::Nsec {
600 nsec: _,
601 password: _,
602 npub,
603 } => npub,
604 };
605 if let Some(npub) = npub {
606 PublicKey::parse(npub).context("format of npub string in signer_info is invalid")
607 } else {
608 bail!("no npub in signer_info object");
609 }
610}
611
535fn silently_save_to_git_config( 612fn silently_save_to_git_config(
536 git_repo: &Option<&Repo>, 613 git_repo: &Option<&Repo>,
537 signer_info: &SignerInfo, 614 signer_info: &SignerInfo,